Documentation
¶
Overview ¶
Package option implements option types in Go. It takes inspiration from samber/mo but also works with BSON and exposes a (hopefully) more refined interface.
Typical Go code uses pointers to represent such values. That’s problematic for 2 reasons: - If you accidentally dereference a nil pointer, your program panics. - The pointer can increase GC pressure.
This type solves both of those.
A couple special notes:
- nil values inside the Option, like `Some([]int(nil))`, are forbidden. (A runtime panic will happen if you try.) See functions like FromPointer and IfNotZero instead.
- Option’s BSON marshaling/unmarshaling interoperates with the bson package’s handling of nilable pointers. So any code that uses nilable pointers to represent optional values can switch to Option and should continue working with existing persisted data.
- Because encoding/json provides no equivalent to bsoncodec.Zeroer, Option always marshals to JSON null if empty.
Prefer Option to nilable pointers in all new code, and consider changing existing code to use it.
Index ¶
- type Option
- func (o Option[T]) Get() (T, bool)
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) IsZero() bool
- func (o Option[T]) MarshalBSONValue() (byte, []byte, error)
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) MustGet() T
- func (o Option[T]) MustGetf(pattern string, args ...any) T
- func (o Option[T]) OrElse(fallback T) T
- func (o Option[T]) OrZero() T
- func (o Option[T]) ToPointer() *T
- func (o *Option[T]) UnmarshalBSONValue(bType byte, raw []byte) error
- func (o *Option[T]) UnmarshalJSON(b []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 { // contains filtered or unexported fields }
Option represents a possibly-empty value. Its zero value is the empty case.
func FromPointer ¶
FromPointer will convert a nilable pointer into its equivalent Option.
func IfNotZero ¶
IfNotZero returns an Option that’s populated if & only if the given value is a non-zero value. (NB: The zero value for slices & maps is nil, not empty!)
This is useful, e.g., to interface with code that uses nil to indicate a missing slice or map.
func Map ¶
Map transforms `opt` by applying the given function to its internal value, if it exists. If `opt` is empty, this returns empty.
func None ¶
None creates an Option with no value.
Note that `None[T]()` is interchangeable with `Option[T]{}`.
func (Option[T]) Get ¶
Get “unboxes” the Option’s internal value. The boolean indicates whether the value exists.
func (Option[T]) IsNone ¶
IsNone returns a boolean indicating whether or not the option is a None value.
func (Option[T]) IsSome ¶
IsSome returns a boolean indicating whether or not the option is a Some value.
func (Option[T]) MarshalBSONValue ¶
MarshalBSONValue implements bson.ValueMarshaler.
func (Option[T]) MarshalJSON ¶
MarshalJSON encodes Option into json.
func (Option[T]) MustGet ¶
func (o Option[T]) MustGet() T
MustGet is like Get but panics if the Option is empty.
func (Option[T]) OrElse ¶
func (o Option[T]) OrElse(fallback T) T
OrElse returns either the Option’s internal value or the given `fallback`.
func (Option[T]) OrZero ¶
func (o Option[T]) OrZero() T
OrZero returns either the Option’s internal value or the type’s zero value.
func (Option[T]) ToPointer ¶
func (o Option[T]) ToPointer() *T
ToPointer converts the Option to a nilable pointer. The internal value (if it exists) is (shallow-)copied.
func (*Option[T]) UnmarshalBSONValue ¶
UnmarshalBSONValue implements bson.ValueUnmarshaler.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON decodes Option from json.