Documentation ¶
Index ¶
- type Optional
- func (v Optional[T]) Get() (T, bool)
- func (v Optional[T]) GetOr(value T) T
- func (v Optional[T]) GetOrFn(fn func() T) T
- func (v Optional[T]) GetOrZero() T
- func (v Optional[T]) GetValueRef() *T
- func (v Optional[T]) GetValueRefOrNil() *T
- func (v Optional[T]) GoString() string
- func (v Optional[T]) IsNone() bool
- func (v Optional[T]) IsNoneOr(fn func(T) bool) bool
- func (v Optional[T]) IsSome() bool
- func (v Optional[T]) IsSomeAnd(fn func(T) bool) bool
- func (v Optional[T]) MustGet(msg string) T
- func (v Optional[T]) OrFn(fn func() Optional[T]) Optional[T]
- func (v *Optional[T]) SetOrChoose(value T, preferRight func(T, T) bool)
- func (v *Optional[T]) SetOrFn(value T, fn func(base, increment T) T)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
A value that may not be present.
Wrapping optional values with this struct is preferred over pointers for better type safety, avoid accidental mutation and self-explanatory explicitness.
An Optional value is always either `Some(value)` or `None`. When it is `None`, the internal `value` field is always the zero value.
func FromPtr ¶
Dereferences a pointer if it is non nil. Converts from the K8s pointer-optional convention.
func GetMap ¶
func GetMap[K comparable, V any](map_ map[K]V, key K) Optional[V]
Returns `Some(map_[key])` if `key` exists in `map_`, None otherwise.
func (Optional[T]) Get ¶
Converts from `Optional` form to the (value, isPresent) form, which is consistent with Go syntax with map access and type cast.
func (Optional[T]) GetOr ¶
func (v Optional[T]) GetOr(value T) T
Gets the value or falls back to a default value.
func (Optional[T]) GetOrFn ¶
func (v Optional[T]) GetOrFn(fn func() T) T
Gets the value or falls back to a lazily computed value.
func (Optional[T]) GetOrZero ¶
func (v Optional[T]) GetOrZero() T
Gets the value or falls back to the zero value.
func (Optional[T]) GetValueRef ¶
func (v Optional[T]) GetValueRef() *T
Returns a pointer to the value field, which may represent an uninitialized object. Never returns nil, not even when present.
func (Optional[T]) GetValueRefOrNil ¶
func (v Optional[T]) GetValueRefOrNil() *T
Returns a pointer to the value field, ONLY IF the value is present.
func (Optional[T]) IsNoneOr ¶
Returns true if the value is absent or if the present value matches the predicate.
func (Optional[T]) MustGet ¶
Asserts that the receiver is `Some`, panics with the given justification otherwise.
The message should be a justification stating why the value must exist, e.g. "1 + 1 should be 2".
func (*Optional[T]) SetOrChoose ¶
Sets the receiver to `Some(value)` if it is None or the new value wins `preferRight`.