option

package
v1.0.0-alpha4 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: Apache-2.0 Imports: 12 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapOrElseOption

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

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

func MapOrOption

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

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

Types

type Cloner

type Cloner[T any] interface {
	Clone() T
}

type Equality

type Equality[T any] interface {
	Equal(T) bool
}

type Option

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

Option represents an optional value.

func FlattenOption

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

FlattenOption converts Option[Option[T]] into Option[T].

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]

func GetSlice

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

func MapOption

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

MapOption 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 (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]) CheckUnd

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

func (Option[T]) Clone

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

func (Option[T]) Equal

func (o Option[T]) Equal(other Option[T]) bool

Equal implements Equality[Option[T]].

Equal tests o and other if both are Some or None. If both have value, it tests equality of their values.

Equal panics If T or dynamic type of T is not comparable.

Option is comparable if T is comparable. Equal only exists for cases where T needs a special Equal method (e.g. time.Time, slice based types)

Equal first checks if T implements Equality[T], then also for *T. If it does not, then Equal compares by the `==` comparison operator.

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

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

func (Option[T]) MarshalJSONV2

func (o Option[T]) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) 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]) UnmarshalJSON

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

func (*Option[T]) UnmarshalJSONV2

func (o *Option[T]) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error

func (*Option[T]) UnmarshalXML

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

func (Option[T]) ValidateUnd

func (o Option[T]) ValidateUnd() 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]) CheckUnd

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

func (Options[T]) Clone

func (o Options[T]) Clone() Options[T]

func (Options[T]) Equal

func (o Options[T]) Equal(opts Options[T]) bool

func (Options[T]) EqualFunc

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

func (Options[T]) ValidateUnd

func (o Options[T]) ValidateUnd() 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