optional

package module
v0.2.30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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

View Source
const ErrDecode errs.Const = "optional: decode configuration"

ErrDecode reports that a Configuration could not be decoded into the target struct; it wraps the underlying mapstructure failure. It is a const of errs.Const so callers can match it with errors.Is.

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 New

func New[T any](opts ...Option[T]) T

New returns a T that's been initialized with all the options you handed it.

func NewWithDefault

func NewWithDefault[T any](def T, opts ...Option[T]) T

NewWithDefault returns a T that starts from a default T and then has all the options folded on top, skipping any nil options.

Types

type Configuration

type Configuration Map[string, any]

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

type ConfigurationKey[T any] string

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 Map

type Map[K comparable, V any] map[K]V

Map is an even more general configurable map than a Configuration is.

type Opt

type Opt[T any] Pair[string, T]

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 KV

func KV[T any](k ConfigurationKey[T], v T) Opt[T]

KV is a quick little helper for spinning up an Opt option.

func (Opt[T]) Apply

func (c Opt[T]) Apply(t Configuration) Configuration

Apply is how Opt satisfies the Option interface: it returns the Configuration with the option set, allocating the map first when it's nil.

type Option

type Option[T any] interface {
	Apply(T) T
}

Option is the interface for any type that knows how to apply its field values onto a T: Apply receives the value being built and returns the updated value. Options are value transformers — a T is built by folding options over a starting value, never through a pointer.

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Pair is a single Map option, ready to apply.

func (Pair[K, V]) Apply

func (c Pair[K, V]) Apply(t Map[K, V]) Map[K, V]

Apply is how Pair satisfies the Option interface: it returns the map with the pair set, allocating the map first when it's nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址