nilo

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2025 License: MIT Imports: 0 Imported by: 13

README

nilo

Go Optionals library for handling nil values and some errors (partially inspired in Java Optionals)

Caveats

  • This library requires Go 1.23+

Intallation

go get -u github.com/javiorfo/nilo@latest

Example

Examples here
package main

import (
  "errors"
  "fmt"
  "slices"

  "github.com/javiorfo/nilo"
)

func main() {
  var numbers []int

  evenNumbersOpt := nilo.Of(numbers).Filter(func(n []int) bool {
    return slices.Contains(n, 1)
  })

  if evenNumbersOpt.IsPresent() {
    fmt.Println("One is present")
  } 

  nilo.FromTuple(getUser(true)).AndThen(getUser2).IfPresent(print)

  _, err := test(false).OrError(errors.New("some err"))
  fmt.Println(err.Error())

  fmt.Println(test(true).MapToString(func(v string) string { return v + ", World" }).OrElse("another string"))
}

func test(b bool) nilo.Optional[string] {
  if b {
    return nilo.Of("Hello")
  }
  return nilo.Empty[string]()
}

func print[T any](v *T) {
  fmt.Printf("Value: %#v\n", *v)
}

func getUser(b bool) (*int, error) {
  if b {
    i := 1
    return &i, nil
  }
  return nil, errors.New("error")
}

func getUser2(p *int) (*int, error) {
  if *p == 0 {
    return 0, errors.New("error")
  }
  r := *p + 2
  return &r, nil
}

Donate
  • Bitcoin (QR) 1GqdJ63RDPE4eJKujHi166FAyigvHu5R7v
  • Paypal

Documentation

Overview

Package nilo provides a generic Optional type that can be used to represent a value that may or may not be present.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

type Optional[T any] struct {
	// contains filtered or unexported fields
}

Optional is a generic type that encapsulates a value that may or may not be present. It provides methods to work with the value safely.

func Empty

func Empty[T any]() Optional[T]

Empty returns an empty Optional.

func FromTuple

func FromTuple[T any](value T, err error) Optional[T]

FromTuple takes a value and error tuple creates an Optional containing the provided value if err is nil.

func Map

func Map[T, R any](o Optional[T], mapper func(T) R) Optional[R]

Map applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped value; otherwise, it returns an empty Optional.

func Of

func Of[T any](value T) Optional[T]

Of creates an Optional containing the provided value.

func OfPointer added in v1.0.1

func OfPointer[T any](value *T) Optional[T]

OfPointer creates an Optional from a pointer to a value. If the pointer is nil, it returns an empty Optional.

func (Optional[T]) AndThen

func (o Optional[T]) AndThen(apply func(T) (T, error)) Optional[T]

AndThen takes a function with optional type parameter and returns the same type or an error creates an Optional containing the provided value if error is nil.

func (Optional[T]) Filter

func (o Optional[T]) Filter(filter func(T) bool) Optional[T]

Filter returns an Optional containing the value if it is present and satisfies the provided filter function; otherwise, it returns an empty Optional.

func (Optional[T]) Get

func (o Optional[T]) Get() T

Get retrieves the value contained in the Optional. It panics if the value is not present (i.e., if IsEmpty() returns true).

func (Optional[T]) IfPresent

func (o Optional[T]) IfPresent(consumer func(T))

IfPresent executes the provided consumer function with the value contained in the Optional if present.

func (Optional[T]) IfPresentOrElse

func (o Optional[T]) IfPresentOrElse(consumer func(T), or func())

IfPresentOrElse executes the provided consumer function with the value if present; otherwise, it executes the provided alternative function.

func (Optional[T]) IsEmpty

func (o Optional[T]) IsEmpty() bool

IsEmpty returns true if the Optional does not contain a value; otherwise, it returns false.

func (Optional[T]) IsPresent

func (o Optional[T]) IsPresent() bool

IsPresent returns true if the Optional contains a value; otherwise, it returns false.

func (Optional[T]) MapToAny

func (o Optional[T]) MapToAny(mapper func(T) any) Optional[any]

MapToAny applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped value; otherwise, it returns an empty Optional.

func (Optional[T]) MapToInt

func (o Optional[T]) MapToInt(mapper func(T) int) Optional[int]

MapToInt applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped int value; otherwise, it returns an empty Optional.

func (Optional[T]) MapToString

func (o Optional[T]) MapToString(mapper func(T) string) Optional[string]

MapToString applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped string value; otherwise, it returns an empty Optional.

func (Optional[T]) Or

func (o Optional[T]) Or(supplier func() Optional[T]) Optional[T]

Or returns the value contained in the Optional if present; otherwise, it invokes the provided supplier function to obtain a new Optional.

func (Optional[T]) OrElse

func (o Optional[T]) OrElse(other T) T

OrElse returns the value contained in the Optional if present; otherwise, it returns the provided alternative value.

func (Optional[T]) OrElseGet

func (o Optional[T]) OrElseGet(supplier func() T) T

OrElseGet returns the value contained in the Optional if present; otherwise, it invokes the provided supplier function to obtain the value.

func (Optional[T]) OrError

func (o Optional[T]) OrError(err error) (*T, error)

OrError returns the value contained in the Optional if present; otherwise, it returns the provided error. This returns pointer type or an error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL