Documentation ¶
Overview ¶
Package option provides a way to represent optional values. It basically is a struct with a pointer to the value, but with a lot of little helper methods. It supports JSON marshaling and unmarshaling.
Index ¶
- type Option
- func (option *Option[T]) Apply(f func(T) T)
- func (option Option[T]) GetValue() (T, bool)
- func (option Option[T]) IsAbsent() bool
- func (option Option[T]) IsPresent() bool
- func (option Option[T]) MarshalJSON() ([]byte, error)
- func (option Option[T]) MustValue() T
- func (t *Option[T]) Scan(v interface{}) error
- func (option Option[T]) String() string
- func (option *Option[T]) UnmarshalJSON(data []byte) error
- func (option *Option[T]) Update(value T)
- func (t Option[T]) Value() (driver.Value, error)
- func (option Option[T]) ValueOrDefault(fallback T) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option is a container for an optional value of type T. It uses nil to represent the absence of a value.
func TupleToOption ¶
TupleToOption converts a tuple to an Option.
func (*Option[T]) Apply ¶
func (option *Option[T]) Apply(f func(T) T)
Apply applies the given function to the value of the Option.
func (Option[T]) GetValue ¶ added in v1.4.0
GetValue returns value and presence. If value is absent, returns zero value and false.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Option[T]) MustValue ¶
func (option Option[T]) MustValue() T
MustValue returns value if present or panics instead.
func (*Option[T]) Scan ¶ added in v1.4.0
Scan implements the sql.Scanner interface. It means that the Option can be scanned from a database value.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Option[T]) Update ¶
func (option *Option[T]) Update(value T)
Update updates the value of the Option.
func (Option[T]) Value ¶
Value implements the driver.Valuer interface. To safely get the value of an Option, use GetValue().
func (Option[T]) ValueOrDefault ¶
func (option Option[T]) ValueOrDefault(fallback T) T
ValueOrDefault returns value if present or the default value given instead.