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 ¶
- type Optional
- func (o Optional[T]) AndThen(other T, err error) Optional[T]
- func (o Optional[T]) AndThenPtr(other *T, err error) Optional[T]
- func (o Optional[T]) Filter(filter func(T) bool) Optional[T]
- func (o Optional[T]) Get() T
- func (o Optional[T]) IfPresent(consumer func(T))
- func (o Optional[T]) IfPresentOrElse(consumer func(T), or func())
- func (o Optional[T]) IsEmpty() bool
- func (o Optional[T]) IsPresent() bool
- func (o Optional[T]) MapToAny(mapper func(T) any) Optional[any]
- func (o Optional[T]) MapToInt(mapper func(T) int) Optional[int]
- func (o Optional[T]) MapToString(mapper func(T) string) Optional[string]
- func (o Optional[T]) Or(supplier func() Optional[T]) Optional[T]
- func (o Optional[T]) OrElse(other T) T
- func (o Optional[T]) OrElseGet(supplier func() T) T
- func (o Optional[T]) OrError(err error) (*T, error)
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 FromTuple ¶
FromTuple takes a value and error tuple creates an Optional containing the provided value if err is nil.
func FromTuplePtr ¶
FromTuplePtr takes a pointer value and error tuple creates an Optional containing the provided value if err is nil.
func Map ¶
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 OfPtr ¶
OfPtr creates an Optional from a pointer to a value. If the pointer is nil, it returns an empty Optional.
func (Optional[T]) AndThen ¶
AndThen takes a value and error tuple creates an Optional containing the provided value if error is nil.
func (Optional[T]) AndThenPtr ¶
AndThenPtr takes a pointer value and error tuple creates an Optional containing the provided value if error is nil.
func (Optional[T]) Filter ¶
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 ¶
IsEmpty returns true if the Optional does not contain a value; otherwise, it returns false.
func (Optional[T]) IsPresent ¶
IsPresent returns true if the Optional contains a value; otherwise, it returns false.
func (Optional[T]) MapToAny ¶
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 ¶
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 ¶
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 ¶
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.