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 ¶
- func Compare[T cmp.Ordered](a, b Optional[T]) int
- func CompareFunc[T any](a, b Optional[T], comparer func(T, T) int) int
- func Equal[T comparable](a, b Optional[T]) bool
- func Equal2[T equaler[T]](a, b Optional[T]) bool
- func EqualFunc[T any](a, b Optional[T], eq func(a2, b2 T) bool) bool
- func Map[X, Y any](o Optional[X], fallback Y, mapper func(v X) Y) Y
- func ToNullBool(o Optional[bool]) sql.NullBool
- func ToNullFloat64[T constraints.Float](o Optional[T]) sql.NullFloat64
- func ToNullInt64[T constraints.Integer](o Optional[T]) sql.NullInt64
- func ToNullString(o Optional[string]) sql.NullString
- func ToNullTime(o Optional[time.Time]) sql.NullTime
- type Optional
- func ConvertNumeric[X numeric, Y numeric](o Optional[X]) Optional[Y]
- func FlatMap[X, Y any](o Optional[X], mapper func(v X) Optional[Y]) Optional[Y]
- func FromNullBool(v sql.NullBool) Optional[bool]
- func FromNullFloat64(v sql.NullFloat64) Optional[float64]
- func FromNullFloat64ToFloat32(v sql.NullFloat64) Optional[float32]
- func FromNullInt64(v sql.NullInt64) Optional[int64]
- func FromNullInt64ToInteger[T constraints.Integer](v sql.NullInt64) Optional[T]
- func FromNullString(v sql.NullString) Optional[string]
- func FromNullTime(v sql.NullTime) Optional[time.Time]
- func FromPtr[T any](x *T) Optional[T]
- func FromZeroValue[T comparable](v T) Optional[T]
- func New[T any](v T) Optional[T]
- func Sum[T numeric](v ...Optional[T]) Optional[T]
- func SumNonEmpty[T numeric](v ...Optional[T]) Optional[T]
- func (o *Optional[T]) Clear()
- func (o Optional[T]) IsEmpty() bool
- func (o Optional[T]) MarshalJSON() ([]byte, error)
- func (o Optional[T]) MustValue() T
- func (o Optional[T]) Ptr() *T
- func (o *Optional[T]) Set(v T)
- func (o *Optional[T]) SetWhenEmpty(v T)
- func (o Optional[T]) String() string
- func (o Optional[T]) StringFunc(fallback string, mapper func(v T) string) string
- func (o *Optional[T]) UnmarshalJSON(b []byte) error
- func (o Optional[T]) Value() (T, bool)
- func (o Optional[T]) ValueOrFallback(fallback T) T
- func (o Optional[T]) ValueOrZero() T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶ added in v0.61.0
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
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
Equal2 reports whether two optionals with values that satisfy the Equaler interface are equal.
func EqualFunc ¶ added in v0.59.0
EqualFunc reports whether two optionals are equal using an equality function on each pair of elements.
func Map ¶ added in v0.59.0
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
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.
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 ¶
ConvertNumeric converts between numeric optionals.
func FlatMap ¶ added in v0.61.0
FlatMap returns another optional with is the result of applying mapper on o.
func FromNullBool ¶ added in v0.63.0
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 ¶
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 ¶
FromNullTime converts a sql.Null variable to it's Optional equivalent and returns it.
func FromPtr ¶ added in v0.59.0
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 Sum ¶ added in v0.57.0
Sum returns the sum of values v. When any value is empty it returns an empty value.
func SumNonEmpty ¶ added in v0.65.0
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]) MarshalJSON ¶ added in v0.59.0
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]) 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]) StringFunc ¶ added in v0.23.0
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
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 ¶
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.