Documentation ¶
Overview ¶
Package opt provides Option, a container that either has a value or is empty.
Index ¶
- type Option
- func (option *Option[T]) Clear()
- func (option Option[T]) Get() (value T, ok bool)
- func (option Option[T]) GetOrDefault(defaultValue T) T
- func (option Option[T]) HasValue() bool
- func (option Option[T]) IsEmpty() bool
- func (option Option[T]) MarshalJSON() ([]byte, error)
- func (option *Option[T]) Put(value T)
- func (option Option[T]) String() string
- func (option Option[T]) ToPointer() *T
- func (option Option[T]) ToSQL() sql.Null[T]
- func (option *Option[T]) UnmarshalJSON(jsonValue []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T any] struct { // Before accessing Value, you should check if it is present with [Option.HasValue]. Value T // contains filtered or unexported fields }
Option is a container that either has a value, or is empty. You construct an option with Value, Empty or FromPointer. You can check if an option contains a value or is empty with Option.HasValue or Option.IsEmpty, and access the value through the [Option.Value] field.
The zero value of Option is an empty option.
An empty option marshals to `null` in JSON, and a `null` JSON value unmarshals to an empty option.
func FromPointer ¶
FromPointer creates an Option with the value pointed to by the given pointer, dereferencing it. If the pointer is nil, an empty option is returned.
func FromSQL ¶ added in v0.1.2
FromSQL creates an Option from the given sql.Null value. A null SQL value becomes an empty option, and a non-null SQL value becomes an option containing the value.
func (*Option[T]) Clear ¶
func (option *Option[T]) Clear()
Clear removes the current value of the option, if any. After this call, Option.IsEmpty will return true.
func (Option[T]) Get ¶
Get returns the value of the option, and an `ok` flag that is true if the option contained a value, and false if it is empty. You should only use the returned value if `ok` is true.
func (Option[T]) GetOrDefault ¶ added in v0.1.2
func (option Option[T]) GetOrDefault(defaultValue T) T
GetOrDefault returns the option's value if present, or the given default value if the option is empty.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for Option. If the option contains a value, it marshals that value. If the option is empty, it marshals to `null`.
func (*Option[T]) Put ¶
func (option *Option[T]) Put(value T)
Put replaces the current value of the option, if any, with the given value. After this call, Option.HasValue will return true.
func (Option[T]) String ¶
String returns the string representation of the option's value. If the option is empty, it returns the string `<empty>` (similar to the string representation `<nil>` for nil pointers).
func (Option[T]) ToPointer ¶ added in v0.1.1
func (option Option[T]) ToPointer() *T
ToPointer returns nil if the option is empty, otherwise it returns a pointer to the option's value.
It is meant to be used for compatibility with libraries that use pointers for optional values.
func (Option[T]) ToSQL ¶ added in v0.1.2
ToSQL converts the option to an sql.Null. An empty option becomes null.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for Option. If the given JSON value is `null`, it unmarshals to an empty option. Otherwise, it tries to unmarshal to the value contained by the option.