option

package
v1.0.0-alpha8 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: Apache-2.0 Imports: 10 Imported by: 14

Documentation

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 an equality of l and r then returns true if they are equal, false otherwise

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 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.

func MapOr

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

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

func MapOrElse

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

MapOrElse returns value o's value applied by f if o is some. Otherwise it returns a defaultFn result.

func UndValidate

func UndValidate[T validate.UndValidator](o Option[T]) error

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 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 GetMap, GetSlice 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 GetMap

func GetMap[M ~map[K]V, K comparable, V any](m M, key K) Option[V]

GetMap gets a value associated with key. If key has a value, the Option is some wrapping the value. Otherwise it returns none Option.

func GetSlice

func GetSlice[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 Map

func Map[T, U any](o Option[T], 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 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 Option[T]) Option[T]

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

func (Option[T]) AndThen

func (o Option[T]) AndThen(f func(x T) Option[T]) Option[T]

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.

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

func (o Option[T]) Get() (T, bool)

func (Option[T]) IsNone

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

func (Option[T]) IsSome

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

func (Option[T]) IsSomeAnd

func (o Option[T]) IsSomeAnd(f 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(f func(v T) T) Option[T]

Map returns Option[T] whose inner value is o's value mapped by f if o is some. Otherwise it returns None[T].

func (Option[T]) MapOr

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

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

func (Option[T]) MapOrElse

func (o Option[T]) MapOrElse(defaultFn func() T, f func(T) T) T

MapOrElse returns value o's value applied by f if o is some. Otherwise it returns a defaultFn result.

func (Option[T]) MapOrElseOpt

func (o Option[T]) MapOrElseOpt(defaultFn func() T, f func(T) T) Option[T]

MapOrElseOpt is like Option.MapOrElse but wraps the returned value into some Option.

func (Option[T]) MapOrOpt

func (o Option[T]) MapOrOpt(defaultValue T, f func(T) T) Option[T]

MapOrOpt is like Option.MapOr but wraps the returned value into some Option.

func (Option[T]) MarshalJSON

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

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]) Plain deprecated

func (o Option[T]) Plain() *T

Plain transforms o to *T, the plain conventional Go representation of an optional value. The value is copied by assignment before returned from Plain.

Deprecated: use Pointer instead.

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 value is copied by assignment before returned from Pointer.

func (Option[T]) SqlNull

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

func (Option[T]) UndCheck

func (o Option[T]) UndCheck() error

func (Option[T]) UndValidate

func (o Option[T]) UndValidate() error

func (*Option[T]) UnmarshalJSON

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

func (*Option[T]) UnmarshalXML

func (o *Option[T]) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

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.

func (Options[T]) UndCheck

func (o Options[T]) UndCheck() error

func (Options[T]) UndValidate

func (o Options[T]) UndValidate() error

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