opt

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package opt defines 2-state value of T | null.

Index

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

func EqualEqualer[T interface{ Equal(t T) bool }](l, r Option[T]) bool

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

func EqualOptionsEqualer[T interface{ Equal(t T) bool }, Opts ~[]Option[T]](l, r Opts) bool

EqualEqualer tests equality of l and r by calling Equal method implemented on l.

func EqualOptionsFunc

func EqualOptionsFunc[T any, Opts ~[]Option[T]](l, r Opts, cmp func(i, j T) bool) bool

EqualOptionsFunc tests equality of l and r using cmp then returns true if they are equal, false otherwise.

Types

type Option

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

Option represents an optional value.

func Assert

func Assert[T any](v any) Option[T]

Assert type-asserts v into T. If v's internal value is T then returns Some of that value, None otherwise.

func CloneCloner

func CloneCloner[T interface{ Clone() T }](o Option[T]) Option[T]

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 Flatten

func Flatten[T any](o Option[Option[T]]) Option[T]

Flatten converts Option[Option[T]] into Option[T].

func FromOk

func FromOk[T any](t T, ok bool) Option[T]

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

func FromPointer[T any](t *T) Option[T]

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 FromSqlNull

func FromSqlNull[T any](v sql.Null[T]) Option[T]

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

func IndexSlice[S ~[]T, T any](s S, idx int) Option[T]

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 None

func None[T any]() Option[T]

func Some

func Some[T any](v T) Option[T]

func WrapPointer

func WrapPointer[T any](t *T) Option[*T]

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]) And

func (o Option[T]) And[U any](u Option[U]) Option[U]

And returns u if o is some, otherwise None[T].

func (Option[T]) AndThen

func (o Option[T]) AndThen[U any](f func(t T) Option[U]) Option[U]

AndThen calls f with value of o if o is some, otherwise returns None[T].

func (Option[T]) CloneFunc

func (o Option[T]) CloneFunc(cloneT func(T) T) Option[T]

CloneFunc clones o using the cloneT function.

func (Option[T]) EqualFunc

func (o Option[T]) EqualFunc(other Option[T], cmp func(i, j T) bool) bool

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]) Expect

func (o Option[T]) Expect(msg string) T

Expect returns o's value if o is some. Otherwise it panics with msg.

func (Option[T]) Filter

func (o Option[T]) Filter(pred func(t T) bool) Option[T]

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]) Inspect

func (o Option[T]) Inspect(fn func(t T)) Option[T]

Inspect calls fn with o's value if o is some, then returns o unchanged.

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

func (Option[T]) IsNoneOr

func (o Option[T]) IsNoneOr(fn func(T) bool) bool

IsNoneOr returns true if o is none, or if o is some and calling fn with o's value returns true.

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

func (Option[T]) IsSomeAnd

func (o Option[T]) IsSomeAnd(fn func(T) bool) bool

IsSomeAnd returns true if o is some and calling f with value of o returns true. Otherwise it returns false.

func (Option[T]) IsZero

func (o Option[T]) IsZero() bool

func (Option[T]) Iter

func (o Option[T]) Iter() iter.Seq[T]

Iter returns an iterator over the internal value. If o is some, the iterator yields the Option.Value(), otherwise nothing.

func (Option[T]) LogValue

func (o Option[T]) LogValue() slog.Value

LogValue implements slog.LogValuer

func (Option[T]) Map

func (o Option[T]) Map[U any](f func(T) U) Option[U]

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

func (o Option[T]) MapOr[U any](defaultValue U, f func(T) U) U

MapOr returns o's value applied by f if o is some. Otherwise it returns defaultValue.

func (Option[T]) MapOrElse

func (o Option[T]) MapOrElse[U any](defaultFn func() U, fn func(T) U) U

MapOrElse returns o's value applied by fn if o is some. Otherwise it returns the result of defaultFn.

func (Option[T]) MarshalJSON

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

func (Option[T]) MarshalJSONTo

func (o Option[T]) MarshalJSONTo(enc *jsontext.Encoder) error

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 (o Option[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (Option[T]) Or

func (o Option[T]) Or(u Option[T]) Option[T]

Or returns o if o is some, otherwise u.

func (Option[T]) OrElse

func (o Option[T]) OrElse(f func() Option[T]) Option[T]

OrElse returns o if o is some, otherwise calls f and returns the result.

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]) SqlNull

func (o Option[T]) SqlNull() sql.Null[T]

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(data []byte) error

func (*Option[T]) UnmarshalJSONFrom

func (o *Option[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error

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 (o *Option[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

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.

func (Option[T]) Value

func (o Option[T]) Value() T

Value returns its internal as T. T would be zero value if o is None.

func (Option[T]) Xor

func (o Option[T]) Xor(u Option[T]) Option[T]

Xor returns o or u if either is some. If both are some or both none, it returns None[T].

type Options

type Options[T any] []Option[T]

func (Options[T]) CloneFunc

func (o Options[T]) CloneFunc(cloneT func(T) T) Options[T]

func (Options[T]) EqualFunc

func (o Options[T]) EqualFunc(opts Options[T], cmp func(i, j T) bool) bool

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.

func (Options[T]) Iter

func (o Options[T]) Iter() iter.Seq[Option[T]]

type SqlNull

type SqlNull[T any] struct {
	Option[T]
}

SqlNull[T] adapts Option[T] to sql.Scanner and driver.Valuer.

func (*SqlNull[T]) Scan

func (n *SqlNull[T]) Scan(src any) error

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.

func (SqlNull[T]) Value

func (n SqlNull[T]) Value() (driver.Value, error)

Value implements driver.Valuer.

If T or *T implements driver.Valuer, the implementation is used. In this respect, T should not be a pointer type or Option[T] should not store nil value. Otherwise, SqlNull[T] falls back to sql.Null[T] as driver.Valuer.

Jump to

Keyboard shortcuts

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