optional

package
v0.76.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package optional provides the generic type Optional, which represents an optional value.

It also includes helpers to convert variables of sql.NullX type to Optional type and vice versa.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v0.61.0

func Compare[T cmp.Ordered](a, b Optional[T]) int

Compare compares the optional a with b. If a is less then b, it returns -1; if a is greater then b, it returns +1; if they're the same, it returns 0; An empty optional is less then a non-empty optional.

func CompareFunc added in v0.61.0

func CompareFunc[T any](a, b Optional[T], comparer func(T, T) int) int

CompareFunc compares the optional a with b by applying the compare function.

func Equal added in v0.59.0

func Equal[T comparable](a, b Optional[T]) bool

Equal reports whether two optionals with comparable values are equal.

func Equal2 added in v0.59.0

func Equal2[T equaler[T]](a, b Optional[T]) bool

Equal2 reports whether two optionals with values that satisfy the Equaler interface are equal.

func EqualFunc added in v0.59.0

func EqualFunc[T any](a, b Optional[T], eq func(a2, b2 T) bool) bool

EqualFunc reports whether two optionals are equal using an equality function on each pair of elements.

func Map added in v0.59.0

func Map[X, Y any](o Optional[X], fallback Y, mapper func(v X) Y) Y

Map returns the result of applying mapper on the value of o or fallback if o is empty.

func ToNullBool added in v0.63.0

func ToNullBool(o Optional[bool]) sql.NullBool

ToNullBool converts an Optional variable to it's sql.Null equivalent and returns it.

func ToNullFloat64

func ToNullFloat64[T constraints.Float](o Optional[T]) sql.NullFloat64

ToNullFloat64 converts an Optional variable to it's sql.Null equivalent and returns it.

func ToNullInt64

func ToNullInt64[T constraints.Integer](o Optional[T]) sql.NullInt64

ToNullInt64 converts an Optional variable to it's sql.Null equivalent and returns it.

func ToNullString

func ToNullString(o Optional[string]) sql.NullString

ToNullString converts an Optional variable to it's sql.Null equivalent and returns it.

func ToNullTime

func ToNullTime(o Optional[time.Time]) sql.NullTime

ToNullTime converts an Optional variable to it's sql.Null equivalent and returns it.

Types

type Optional

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

Optional represents a variable that may contain a value or not.

The zero value for an Optional is an empty optional.

func ConvertNumeric

func ConvertNumeric[X numeric, Y numeric](o Optional[X]) Optional[Y]

ConvertNumeric converts between numeric optionals.

func FlatMap added in v0.61.0

func FlatMap[X, Y any](o Optional[X], mapper func(v X) Optional[Y]) Optional[Y]

FlatMap returns another optional with is the result of applying mapper on o.

func FromNullBool added in v0.63.0

func FromNullBool(v sql.NullBool) Optional[bool]

FromNullBool converts a sql.Null variable to it's Optional equivalent and returns it.

func FromNullFloat64

func FromNullFloat64(v sql.NullFloat64) Optional[float64]

FromNullFloat64 converts a sql.Null variable to it's Optional equivalent and returns it.

func FromNullFloat64ToFloat32 added in v0.23.0

func FromNullFloat64ToFloat32(v sql.NullFloat64) Optional[float32]

FromNullFloat64ToFloat32 converts a sql.Null variable to it's Optional equivalent and returns it.

func FromNullInt64

func FromNullInt64(v sql.NullInt64) Optional[int64]

FromNullInt64 converts a sql.Null variable to it's Optional equivalent and returns it.

func FromNullInt64ToInteger

func FromNullInt64ToInteger[T constraints.Integer](v sql.NullInt64) Optional[T]

FromNullInt64ToInteger converts an sql.Null variable to an Optional of a different integer type and returns it.

func FromNullString

func FromNullString(v sql.NullString) Optional[string]

FromNullString converts a sql.Null variable to it's Optional equivalent and returns it.

func FromNullTime

func FromNullTime(v sql.NullTime) Optional[time.Time]

FromNullTime converts a sql.Null variable to it's Optional equivalent and returns it.

func FromPtr added in v0.59.0

func FromPtr[T any](x *T) Optional[T]

FromPtr returns a new optional from a pointer optional. A nil pointer is interpreted as not present.

func FromZeroValue added in v0.59.0

func FromZeroValue[T comparable](v T) Optional[T]

FromZeroValue returns an optional from a value where it's zero value is interpreted as empty.

TODO: Zero is ambiguous with "no data" for callers whose underlying storage can't distinguish the two (e.g. NOT NULL DB columns backing a genuinely optional upstream field). Affected callers should migrate to nullable storage + FromPtr/FromNullX instead of FromZeroValue.

func New

func New[T any](v T) Optional[T]

New returns an optional with the value v.

func Sum added in v0.57.0

func Sum[T numeric](v ...Optional[T]) Optional[T]

Sum returns the sum of values v. When any value is empty it returns an empty value.

func SumNonEmpty added in v0.65.0

func SumNonEmpty[T numeric](v ...Optional[T]) Optional[T]

SumNonEmpty returns the sum of non-empty values v, ignoring empty ones. When all values are empty it returns an empty value. The behavior is modeled after SQL's SUM of nullable values.

func (*Optional[T]) Clear

func (o *Optional[T]) Clear()

Clear removes any value.

func (Optional[T]) IsEmpty

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

IsEmpty reports whether an Optional is empty.

func (Optional[T]) MarshalJSON added in v0.59.0

func (o Optional[T]) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the optional.

func (Optional[T]) MustValue

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

MustValue returns the value of an Optional or panics if it is empty.

func (Optional[T]) Ptr added in v0.59.0

func (o Optional[T]) Ptr() *T

Ptr returns the optional as pointer optional.

func (*Optional[T]) Set

func (o *Optional[T]) Set(v T)

Set sets a new value.

func (*Optional[T]) SetWhenEmpty added in v0.59.0

func (o *Optional[T]) SetWhenEmpty(v T)

SetWhenEmpty sets a new value when the optional is empty.

func (Optional[T]) String

func (o Optional[T]) String() string

String returns a string representation of an Optional.

func (Optional[T]) StringFunc added in v0.23.0

func (o Optional[T]) StringFunc(fallback string, mapper func(v T) string) string

StringFunc returns the result of applying convert when the optional has a value. Or it returns the provided fallback, when the optional is empty,

func (*Optional[T]) UnmarshalJSON added in v0.59.0

func (o *Optional[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON parses the JSON-encoded data b and replaces the current optional. JSON null values will be unmarshaled into an empty optional.

func (Optional[T]) Value

func (o Optional[T]) Value() (T, bool)

Value returns the value of an Optional and reports whether the value exists.

func (Optional[T]) ValueOrFallback

func (o Optional[T]) ValueOrFallback(fallback T) T

ValueOrFallback returns the value of an Optional or a fallback if it is empty.

func (Optional[T]) ValueOrZero

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

ValueOrZero returns the value of an Optional or it's type's zero value if it is empty.

Jump to

Keyboard shortcuts

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