Documentation
¶
Overview ¶
Package opt defines 2-state value of T | null.
Index ¶
- func CloneOptions[T comparable, Opts ~[]Option[T]](o Opts) Opts
- func Equal[T comparable](l, r Option[T]) bool
- func EqualEqualer[T interface{ ... }](l, r Option[T]) bool
- func EqualOptions[T comparable, Opts ~[]Option[T]](l, r Opts) bool
- func EqualOptionsEqualer[T interface{ ... }, Opts ~[]Option[T]](l, r Opts) bool
- func EqualOptionsFunc[T any, Opts ~[]Option[T]](l, r Opts, cmp func(i, j T) bool) bool
- type Option
- func Assert[T any](v any) Option[T]
- func CloneCloner[T interface{ ... }](o Option[T]) Option[T]
- func Flatten[T any](o Option[Option[T]]) Option[T]
- func FromOk[T any](t T, ok bool) Option[T]
- func FromPointer[T any](t *T) Option[T]
- func FromSqlNull[T any](v sql.Null[T]) Option[T]
- func IndexMap[M ~map[K]V, K comparable, V any](m M, key K) Option[V]
- func IndexSlice[S ~[]T, T any](s S, idx int) Option[T]
- func None[T any]() Option[T]
- func Some[T any](v T) Option[T]
- func WrapPointer[T any](t *T) Option[*T]
- func (o Option[T]) And[U any](u Option[U]) Option[U]
- func (o Option[T]) AndThen[U any](f func(t T) Option[U]) Option[U]
- func (o Option[T]) CloneFunc(cloneT func(T) T) Option[T]
- func (o Option[T]) EqualFunc(other Option[T], cmp func(i, j T) bool) bool
- func (o Option[T]) Expect(msg string) T
- func (o Option[T]) Filter(pred func(t T) bool) Option[T]
- func (o *Option[T]) GetOrInsert(t T) *T
- func (o *Option[T]) GetOrInsertDefault() *T
- func (o *Option[T]) GetOrInsertFunc(fn func() T) *T
- func (o *Option[T]) Insert(t T) *T
- func (o Option[T]) Inspect(fn func(t T)) Option[T]
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsNoneOr(fn func(T) bool) bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) IsSomeAnd(fn func(T) bool) bool
- func (o Option[T]) IsZero() bool
- func (o Option[T]) Iter() iter.Seq[T]
- func (o Option[T]) LogValue() slog.Value
- func (o Option[T]) Map[U any](f func(T) U) Option[U]
- func (o Option[T]) MapOr[U any](defaultValue U, f func(T) U) U
- func (o Option[T]) MapOrElse[U any](defaultFn func() U, fn func(T) U) U
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) MarshalJSONTo(enc *jsontext.Encoder) error
- func (o Option[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (o Option[T]) Or(u Option[T]) Option[T]
- func (o Option[T]) OrElse(f func() Option[T]) Option[T]
- func (o *Option[T]) Pointer() *T
- func (o Option[T]) SqlNull() sql.Null[T]
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- func (o *Option[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error
- func (o *Option[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (o Option[T]) Unwrap() T
- func (o Option[T]) UnwrapOr(t T) T
- func (o Option[T]) UnwrapOrDefault() T
- func (o Option[T]) UnwrapOrElse(fn func() T) T
- func (o Option[T]) Value() T
- func (o Option[T]) Xor(u Option[T]) Option[T]
- type Options
- type SqlNull
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneOptions ¶
func CloneOptions[T comparable, Opts ~[]Option[T]](o Opts) Opts
func Equal ¶
func Equal[T comparable](l, r Option[T]) bool
Equal tests equality of l and r then returns true if they are equal, false otherwise
func EqualEqualer ¶
EqualEqualer tests equality of l and r by calling Equal method implemented on l.
func EqualOptions ¶
func EqualOptions[T comparable, Opts ~[]Option[T]](l, r Opts) bool
EqualOptions tests equality of l and r then returns true if they are equal, false otherwise
func EqualOptionsEqualer ¶
EqualEqualer tests equality of l and r by calling Equal method implemented on l.
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option represents an optional value.
func Assert ¶
Assert type-asserts v into T. If v's internal value is T then returns Some of that value, None otherwise.
func CloneCloner ¶
CloneCloner clones o by calling the Clone method on its inner value if o is some.
For a custom clone function, use Option.CloneFunc. An Option[T] whose T only needs a shallow copy can be copied by plain assignment.
func FromOk ¶
FromOk converts conventional (t T, ok bool) into an option. The options is some if ok is true, none otherwise.
For getting values from maps or slices, instead you may want to use IndexMap, IndexSlice respectively.
func FromPointer ¶
FromPointer converts *T into Option[T]. If v is nil, it returns a none Option. Otherwise, it returns some Option whose value is the dereferenced v.
If you need to keep t as pointer, use WrapPointer instead.
func IndexMap ¶
func IndexMap[M ~map[K]V, K comparable, V any](m M, key K) Option[V]
IndexMap gets a value associated with key. If key has a value, the Option is some wrapping the value. Otherwise it returns none Option.
func IndexSlice ¶
GetMap gets a value associated with idx. If idx is within interval [0, len(s)), then the Option is some wrapping a value associated to the idx. Otherwise it returns none Option.
func WrapPointer ¶
WrapPointer converts *T into Option[*T]. The option is some if t is non nil, none otherwise.
If you want t to be dereferenced, use FromPointer instead.
func (Option[T]) EqualFunc ¶
EqualFunc tests o and other if both are Some or None. If their state does not match, it returns false immediately. If both have value, it tests equality of their values by cmp.
If T is just a comparable type, use Equal. If T is an implementor of interface { Equal(t T) bool }, e.g time.Time, use EqualEqualer.
func (Option[T]) Filter ¶
Filter returns o if o is some and calling pred against o's value returns true. Otherwise it returns None[T].
func (*Option[T]) GetOrInsert ¶
func (o *Option[T]) GetOrInsert(t T) *T
GetOrInsert inserts t into o if o is none, then returns a pointer to o's value. If o is already some, the existing value is kept and t is discarded.
o must be addressable; the returned pointer aliases o's internal value.
func (*Option[T]) GetOrInsertDefault ¶
func (o *Option[T]) GetOrInsertDefault() *T
GetOrInsertDefault inserts the zero value of T into o if o is none, then returns a pointer to o's value. If o is already some, the existing value is kept.
o must be addressable; the returned pointer aliases o's internal value.
func (*Option[T]) GetOrInsertFunc ¶
func (o *Option[T]) GetOrInsertFunc(fn func() T) *T
GetOrInsertFunc inserts the result of fn into o if o is none, then returns a pointer to o's value. If o is already some, the existing value is kept and fn is not called.
o must be addressable; the returned pointer aliases o's internal value.
func (*Option[T]) Insert ¶
func (o *Option[T]) Insert(t T) *T
Insert sets o's value to t, overwriting any existing value, and returns a pointer to it.
See Option.GetOrInsert which only inserts when o is none.
o must be addressable; the returned pointer aliases o's internal value.
func (Option[T]) IsNoneOr ¶
IsNoneOr returns true if o is none, or if o is some and calling fn with o's value returns true.
func (Option[T]) IsSomeAnd ¶
IsSomeAnd returns true if o is some and calling f with value of o returns true. Otherwise it returns false.
func (Option[T]) Iter ¶
Iter returns an iterator over the internal value. If o is some, the iterator yields the Option.Value(), otherwise nothing.
func (Option[T]) Map ¶
Map returns Some[U] whose inner value is o's value mapped by f if o is some. Otherwise it returns None[U].
func (Option[T]) MapOr ¶
MapOr returns o's value applied by f if o is some. Otherwise it returns defaultValue.
func (Option[T]) MapOrElse ¶
MapOrElse returns o's value applied by fn if o is some. Otherwise it returns the result of defaultFn.
func (Option[T]) MarshalJSON ¶
func (Option[T]) MarshalJSONTo ¶
MarshalJSONTo implements encoding/json/v2.MarshalerTo.
It encodes a none Option as the JSON null and a some Option as its inner value.
func (Option[T]) MarshalXML ¶
func (*Option[T]) Pointer ¶
func (o *Option[T]) Pointer() *T
Pointer transforms o to *T, the plain conventional Go representation of an optional value. The pointer is reference to internal value. Use lock mechanism if multiple goroutines need to acccess it.
func (*Option[T]) UnmarshalJSON ¶
func (*Option[T]) UnmarshalJSONFrom ¶
UnmarshalJSONFrom implements encoding/json/v2.UnmarshalerFrom.
It decodes the JSON null as a none Option and any other JSON value as a some Option.
func (*Option[T]) UnmarshalXML ¶
func (Option[T]) Unwrap ¶
func (o Option[T]) Unwrap() T
Unwrap returns o's value if o is some. Otherwise it panics.
func (Option[T]) UnwrapOr ¶
func (o Option[T]) UnwrapOr(t T) T
UnwrapOr returns o's value if o is some, otherwise t.
func (Option[T]) UnwrapOrDefault ¶
func (o Option[T]) UnwrapOrDefault() T
UnwrapOrDefault returns o's value if o is some, otherwise the zero value of T.
func (Option[T]) UnwrapOrElse ¶
func (o Option[T]) UnwrapOrElse(fn func() T) T
UnwrapOrElse returns o's value if o is some, otherwise the result of fn.
type Options ¶
func (Options[T]) EqualFunc ¶
EqualFunc tests equality of l and r using an equality function cmp.
If T is just a comparable type, use EqualOptions. If T is an implementor of interface { Equal(t T) bool }, e.g time.Time, use EqualOptionsEqualer.
type SqlNull ¶
SqlNull[T] adapts Option[T] to sql.Scanner and driver.Valuer.
func (*SqlNull[T]) Scan ¶
Scan implements sql.Scanner.
If T or *T implements sql.Scanner, the implementation is used. Otherwise, SqlNull[T] falls back to sql.Null[T] as sql.Scanner.