Documentation ¶
Index ¶
- func And[T comparable, O Optional[T]](left, right O) O
- func ClearIfMatch[T comparable](opt MutableOptional[T], probe T)
- func Equal[T comparable, O Optional[T]](left, right O) bool
- func GetOr[T comparable](opt Optional[T], val T) T
- func GetOrElse[T comparable](opt Optional[T], f func() T) T
- func GetOrInsert[T comparable](opt MutableOptional[T], val T) (T, error)
- func IsSomeAnd[T comparable](opt Option[T], f func(T) bool) bool
- func Must[T comparable](opt Optional[T]) T
- func Or[T comparable, O Optional[T]](left, right O) O
- func TransformOr[T comparable](opt MutableOptional[T], t Transformer[T], backup T) error
- type MutableOptional
- type Option
- func (o *Option[T]) Clear()
- func (o Option[T]) Clone() Optional[T]
- func (o Option[T]) Get() (T, error)
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) Match(probe T) bool
- func (o Option[T]) MutableClone() MutableOptional[T]
- func (o *Option[T]) Replace(value T) (Optional[T], error)
- func (o *Option[T]) Transform(t Transformer[T]) error
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- type Optional
- type OptionalError
- type Transformer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func And ¶
func And[T comparable, O Optional[T]](left, right O) O
And returns None if the first Optional is None, and the second Optional otherwise. Conceptually similar to left && right. This is a convenience function for Option selection. Convenient for merging configs, implementing builder patterns, etc.
func ClearIfMatch ¶
func ClearIfMatch[T comparable](opt MutableOptional[T], probe T)
ClearIfMatch calls clear if Optional.Match(probe) == true. This is a convenience for situations where you need to convert from a value of T with known "magic value" which implies None. A good example of this is if you have an int loaded from command line flags and you know that any flag omitted by the user will be assigned to 0. This can be done like this: o := Some(x) o.ClearIfMatch(0)
func Equal ¶
func Equal[T comparable, O Optional[T]](left, right O) bool
Equal is a convenience function for checking if the contents of two Optional types are equivilent. Note that Get() and Match() may be overridden by more complex types which wrap a vanilla Option. In these situations, the writer is responsible for making sure that the invariant Some(x).Match(Some(x).Get()) is always true.
func GetOr ¶
func GetOr[T comparable](opt Optional[T], val T) T
GetOr is the same as Get, but will return the passed value instead of an error if the Option is None. Another convenience function
func GetOrElse ¶
func GetOrElse[T comparable](opt Optional[T], f func() T) T
GetOrElse calls Get(), but run the passed function and return the result instead of producing an error if the option is None.
func GetOrInsert ¶
func GetOrInsert[T comparable](opt MutableOptional[T], val T) (T, error)
GetOrInsert calls Get, but will call Replace on the passed value then return it if the Option is None
func IsSomeAnd ¶
func IsSomeAnd[T comparable](opt Option[T], f func(T) bool) bool
IsSomeAnd returns true if the Option has a value of Some(x) and f(x) == true
func Must ¶
func Must[T comparable](opt Optional[T]) T
Must just calls Get, but panic instead of producing an error.
func Or ¶
func Or[T comparable, O Optional[T]](left, right O) O
Or returns the first Optional if it contains a value. Otherwise, return the second Optional. This is conceptually similar to left || right. This is a convenience function for situations like merging configs or implementing builder patterns.
func TransformOr ¶
func TransformOr[T comparable](opt MutableOptional[T], t Transformer[T], backup T) error
TransformOr just calls Transform(), except None values are mapped to backup before being transformed.
Types ¶
type MutableOptional ¶
type MutableOptional[T comparable] interface { Optional[T] MutableClone() MutableOptional[T] Clear() Replace(T) (Optional[T], error) // Transform only applies the func to the values of Some valued Optionals. Any mapping of None is None. Transform(f Transformer[T]) error // Satisfies encoding.json.UnMarshaler UnmarshalJSON([]byte) error }
MutableOptional is a superset of Optional which allows mutating and transforming the wrapped value.
type Option ¶
type Option[T comparable] struct { // contains filtered or unexported fields }
Option is a generic way to make a field or parameter optional. Instantiating an Optional value through the Some() or None() methods are prefered since it is easier for the reader of your code to see what the expected value of the Option is, but to avoid leaking options across api boundries a FromPointer() method is given. This allows you to accept a parameter as a pointer and immediately convert it into an Option value without having to do the nil check yourself.
While this can be useful on its own, this can also be used to create a specialized option for your use case. See the config package for a comprehensive example of this.
Special care should be taken when creating Options of pointer types. All the same concerns around passing structs with pointer fields apply, since copying the pointer by value will create many copies of the same pointer. There is nothing to stop you from doing this, but I'm not sure what they use case is and it may lead to less understandable code (which is what this library was created to avoid in the first place!)
func FromPointer ¶
func FromPointer[T comparable](p *T) Option[T]
FromPointer creates an option with an inferred type from a pointer. nil pointers are mapped to a None value and non-nil pointers have their value copied into a new option. The pointer itself is not retained and can be modified later without affecting the Option value.
func None ¶
func None[T comparable]() Option[T]
None returns an Option with no value loaded. Note that since there is no value to infer type from, None must be instanciated with the desired type like optional.None[string]().
func Some ¶
func Some[T comparable](value T) Option[T]
Some returns an Option with an inferred type and specified value.
func (*Option[T]) Clear ¶
func (o *Option[T]) Clear()
Clear converts a Some(x) or None type Option into a None value.
func (Option[T]) Clone ¶
Clone creates a copy of the Option by value. This means the new Option may be unwrapped or modified without affecting the old Option. NOTE: the exception to this would be if you create an Option for a pointer type. Because the pointer is copied by value, it will still refer to the same value.
func (Option[T]) Get ¶
Get returns the current wrapped value of a Some value Option and returns an error if the Option is None.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements the encoding.json.Marshaler interface. None values are marshaled to json null, while Some values are passed into json.Marshal directly.
func (Option[T]) MutableClone ¶
func (o Option[T]) MutableClone() MutableOptional[T]
MutableClone creates a copy of the Option by value the same as Clone. The only differnce is that the returned type is a pointer cast as a MutableOptional so that the returned value can be further modified.
func (*Option[T]) Replace ¶
Replace converts a Some(x) or None type Option into a Some(value) value. The base Option struct can never return an error from Replace, so it is generally safe to ignore the returned values from this, e.g. calling o.Replace() instead of _, _ = o.Replace().
func (*Option[T]) Transform ¶
func (o *Option[T]) Transform(t Transformer[T]) error
Transform applies function f(T) to the inner value of the Option. If the Option is None, then the Option will remain None.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements the encoding.json.Unmarshaller interface. Json nulls are unmarshaled into None values, while any other value is attempted to unmarshal as normal. Any error encountered is returned without modification. There are some protections included to make sure that unmarshaling an uninitialized Option[T] does not break the Option invariants.
type Optional ¶
type Optional[T comparable] interface { IsSome() bool IsNone() bool Clone() Optional[T] Get() (T, error) Match(T) bool // Satisfies encoding.json.Marshaler MarshalJSON() ([]byte, error) }
Optional defines the functionality needed to provide good ergonimics around optional fields and values. In general, code should not declare variables or parameters as Optionals and instead prefer using concrete types like Option. This interface is meant to ensure compatablility between different concrete option types and for the rare cases where the abstraction is actually necessary.
type OptionalError ¶
type OptionalError struct {
// contains filtered or unexported fields
}
func (OptionalError) Error ¶
func (e OptionalError) Error() string
type Transformer ¶
type Transformer[T comparable] func(T) (T, error)