Documentation
¶
Overview ¶
Package optional is a generic take on [functional options](https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
func Decode[T any](in Configuration) (*T, error)
Decode turns the Configuration map into a struct for you.
func Must ¶
func Must[T any](t *T) *T
Must keeps you safe from a nil panic: hand it a nil t and you get a new *T back (so using it inside an Apply just does nothing), otherwise you get t untouched. If you'd actually rather have the panic, skip Must and let the usual nil panic happen.
func NewWithDefault ¶
NewWithDefault returns a T that starts from a default T and then has all the options applied on top.
Types ¶
type Configuration ¶
Configuration is a nice general-purpose configurable map.
func NewConfiguration ¶
func NewConfiguration(opts ...Option[Configuration]) Configuration
NewConfiguration whips up a Configuration from whatever options you pass in.
type ConfigurationKey ¶
ConfigurationKey is a typed key for a value living in a Configuration.
func (ConfigurationKey[T]) Opt ¶
func (o ConfigurationKey[T]) Opt(v T) Opt[T]
Opt builds an Opt for this key, wrapping up the value you give it.
type Error ¶
type Error string
Error is the constant error type for the optional package; every error the package emits is a const of this type so callers can match it with errors.Is.
const ErrDecode Error = "optional: decode configuration"
ErrDecode reports that a Configuration could not be decoded into the target struct; it wraps the underlying mapstructure failure.
type Map ¶
type Map[K comparable, V any] map[K]V
Map is an even more general configurable map than a Configuration is.
type Opt ¶
Opt is a single Configuration option, ready to apply.
func Enabled ¶
func Enabled(o ConfigurationKey[bool]) Opt[bool]
Enabled builds a bool Opt for this key and flips it on (true).
func (Opt[T]) Apply ¶
func (c Opt[T]) Apply(t *Configuration)
Apply is how Opt satisfies the Option interface.