Documentation
¶
Overview ¶
Package gooptional is similar to Java Optional types SPDX-License-Identifier: Apache-2.0
Index ¶
- type Optional
- func (o Optional[T]) Filter(predicate func(T) bool) Optional[T]
- func (o Optional[T]) Get() (T, bool)
- func (o Optional[T]) IfEmpty(f func())
- func (o Optional[T]) IfPresent(consumer func(T))
- func (o Optional[T]) IfPresentOrElse(consumer func(T), f func())
- func (o Optional[T]) IsEmpty() bool
- func (o Optional[T]) IsPresent() bool
- func (o Optional[T]) MustGet() T
- func (o Optional[T]) OrElse(elseValue T) T
- func (o Optional[T]) OrElseGet(supplier func() T) interface{}
- func (o *Optional[T]) Scan(src any) error
- func (o Optional[T]) String() string
- func (o Optional[T]) Value() (driver.Value, 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 mostly immutable generic wrapper for any kind of value with a present flag. The only mutable operation is the implementation of the sql.Scanner interface. The zero value is ready to use.
func FlatMap ¶
FlatMap operates like Map, except that the mapping function already returns an Optional, which is returned as is.
func Map ¶
Map the wrapped value with the given mapping function, which may return a different type. An empty Optional is returned if this optional is not present. Otherwise, an Optional wrapping the mapped value is returned.
func Of ¶
Of returns a present Optional. If no value or a nil value is provided, a new empty Optional is returned. Otherwise a new Optional that wraps the value is returned.
func (Optional[T]) Filter ¶
Filter applies the predicate to the value of this Optional. Returns this Optional only if this Optional is present and the filter returns true for the value. Otherwise an empty Optional is returned.
func (Optional[T]) Get ¶
Get returns the wrapped value and whether or not it is present. The wrapped value is only valid if the boolean is true.
func (Optional[T]) IfEmpty ¶
func (o Optional[T]) IfEmpty(f func())
IfEmpty executes the function only if the value is not present.
func (Optional[T]) IfPresent ¶
func (o Optional[T]) IfPresent(consumer func(T))
IfPresent executes the consumer function with the wrapped value only if the value is present.
func (Optional[T]) IfPresentOrElse ¶
func (o Optional[T]) IfPresentOrElse(consumer func(T), f func())
IfPresentOrElse executes the consumer function with the wrapped value if the value is present, otherwise executes the function of no args.
func (Optional[T]) MustGet ¶
func (o Optional[T]) MustGet() T
MustGet returns the unwrapped value and panics if it is not present.
func (Optional[T]) OrElse ¶
func (o Optional[T]) OrElse(elseValue T) T
OrElse returns the wrapped value if it is present, else it returns the given value.
func (Optional[T]) OrElseGet ¶
func (o Optional[T]) OrElseGet(supplier func() T) interface{}
OrElseGet returns the wrapped value if it is present, else it returns the result of the given function.
func (*Optional[T]) Scan ¶
Scan is database/sql/Scanner interface, allowing users to read null query columns into an Optional. This is the only method that modifies an Optional. If src is null, the Optional will be empty even if T is not a nillable type. If src is not null: - Panics if src is not convertible to T - If T is a reference type, the referenced value must be copied before the next call to Scan
func (Optional[T]) String ¶
String returns fmt.Sprintf("Optional (%v)", wrapped value) if present, else "Optional" if it is empty.