package optionext
import "github.com/go-playground/pkg/v5/values/option"
Index ¶
- type Option
- func None[T any]() Option[T]
- func Some[T any](value T) Option[T]
- func (o Option[T]) And(fn func(T) T) Option[T]
- func (o Option[T]) AndThen(fn func(T) Option[T]) Option[T]
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o *Option[T]) Scan(value any) error
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- func (o Option[T]) Unwrap() T
- func (o Option[T]) UnwrapOr(value T) T
- func (o Option[T]) UnwrapOrDefault() T
- func (o Option[T]) UnwrapOrElse(fn func() T) T
- func (o Option[T]) Value() (driver.Value, error)
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option represents a values that represents a values existence.
nil is usually used on Go however this has two problems: 1. Checking if the return values is nil is NOT enforced and can lead to panics. 2. Using nil is not good enough when nil itself is a valid value.
This implements the sql.Scanner interface and can be used as a sql value for reading and writing. It supports: - String - Bool - Uint8 - Float64 - Int16 - Int32 - Int64 - interface{}/any - time.Time - Struct - when type is convertable to []byte and assumes JSON. - Slice - when type is convertable to []byte and assumes JSON. - Map types - when type is convertable to []byte and assumes JSON.
This also implements the `json.Marshaler` and `json.Unmarshaler` interfaces. The only caveat is a None value will result in a JSON `null` value. there is no way to hook into the std library to make `omitempty` not produce any value at this time.
func None ¶
None creates an empty Option that represents no values.
func Some ¶
Some creates a new Option with the given values.
func (Option[T]) And ¶
And calls the provided function with the contained value if the option is Some, returns the None value otherwise.
func (Option[T]) AndThen ¶
AndThen calls the provided function with the contained value if the option is Some, returns the None value otherwise.
This differs from `And` in that the provided function returns an Option[T] allowing changing of the Option value itself.
func (Option[T]) IsNone ¶
IsNone returns true if the option is empty.
func (Option[T]) IsSome ¶
IsSome returns true if the option is not empty.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements the `json.Marshaler` interface.
func (*Option[T]) Scan ¶
Scan implements the sql.Scanner interface.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements the `json.Unmarshaler` interface.
func (Option[T]) Unwrap ¶
func (o Option[T]) Unwrap() T
Unwrap returns the values if the option is not empty or panics.
func (Option[T]) UnwrapOr ¶
func (o Option[T]) UnwrapOr(value T) T
UnwrapOr returns the contained `Some` value or provided default value.
Arguments passed to `UnwrapOr` are eagerly evaluated; if you are passing the result of a function call, look to use `UnwrapOrElse`, which can be lazily evaluated.
func (Option[T]) UnwrapOrDefault ¶
func (o Option[T]) UnwrapOrDefault() T
UnwrapOrDefault returns the contained `Some` value or the default value of the type T.
func (Option[T]) UnwrapOrElse ¶
func (o Option[T]) UnwrapOrElse(fn func() T) T
UnwrapOrElse returns the contained `Some` value or computes it from a provided function.
func (Option[T]) Value ¶
Value implements the driver.Valuer interface.
This honours the `driver.Valuer` interface if the value implements it. It also supports custom types of the std types and treats all else as []byte
Source Files ¶
option_common.go option_sql_go1.22.go
- Version
- v5.30.0 (latest)
- Published
- Jun 1, 2024
- Platform
- linux/amd64
- Imports
- 7 packages
- Last checked
- 9 hours ago –
Tools for package owners.