Documentation
¶
Overview ¶
Package structdefaults provides a koanf provider that reads koanf-default struct tags and emits a nested map[string]any of default values whose tree shape mirrors the koanf path layout. Load it as the first (lowest-priority) layer so that file, env, and flag providers override it naturally.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCyclicType = errors.New("structdefaults: cyclic struct type")
ErrCyclicType is returned when the walker encounters a struct type that recursively references itself (directly or transitively). Loading defaults for such a type would cause unbounded recursion at startup.
var ErrInvalidConfig = errors.New("structdefaults: invalid Options")
ErrInvalidConfig is returned by New when the Options struct is invalid — e.g. Delim is empty. This indicates a programmer error in the call site.
var ErrInvalidInput = errors.New("structdefaults: input must be a non-nil pointer to a struct")
ErrInvalidInput is returned when the target passed to New is nil, a non-struct, or a nil pointer-to-struct.
var ErrInvalidValue = errors.New("structdefaults: invalid default value")
ErrInvalidValue is returned when a field's type IS supported but the tag value (or its post-substitution form) cannot be parsed — e.g. a malformed integer, a bad duration string, or a TextUnmarshaler that rejected the input. This is typically an operator/config error: fix the tag value or the env var feeding it. Use errors.As to recover the underlying parse error (*strconv.NumError, etc.).
var ErrUnsetEnv = errors.New("structdefaults: env var unset with no fallback")
ErrUnsetEnv is returned when a default value contains ${VAR} and the referenced environment variable is unset with no fallback provided. Use ${VAR:-} to opt into an empty-string fallback.
var ErrUnsupported = errors.New("structdefaults: ReadBytes is not supported")
ErrUnsupported is returned by ReadBytes because the structdefaults provider operates on in-memory structs, not byte streams.
var ErrUnsupportedType = errors.New("structdefaults: unsupported field type")
ErrUnsupportedType is returned when a field's Go type cannot carry a koanf-default value at all — e.g. slice, map, channel, or function fields with a default tag. This is a programmer error: fix the struct definition.
Functions ¶
This section is empty.
Types ¶
type EnvLookup ¶
EnvLookup resolves an environment variable name to its value. Implementations must return (value, true) when the variable is set (even to an empty string) and ("", false) when it is unset. The default lookup is os.LookupEnv.
Implementations must be safe for concurrent use; the provider holds a reference and may call it from any goroutine that triggers a Read.
type Options ¶
type Options struct {
// Delim is the path separator used both to interpret the path tag values
// and to nest entries in the output map. Required; empty Delim returns
// ErrInvalidConfig from New.
Delim string
// PathTag is the struct tag whose value names the config path segment for
// each field. Defaults to "koanf".
PathTag string
// DefaultTag is the struct tag whose value declares the field's default.
// Defaults to "koanf-default".
DefaultTag string
// Lookup resolves ${VAR} references found in DefaultTag values. Defaults
// to os.LookupEnv. Pass a custom function for hermetic tests, secret
// stores (Vault, AWS Secrets Manager), or precedence layering.
Lookup EnvLookup
// Strict, when true, eagerly walks the entire struct at construction time
// and surfaces any error (cyclic types, parse failures, unset env vars
// without fallback) from New rather than waiting for the first Read call.
Strict bool
}
Options configures a StructDefaults provider. All fields are optional except Delim. Zero values trigger sensible defaults documented per field.
type StructDefaults ¶
type StructDefaults struct {
// contains filtered or unexported fields
}
StructDefaults walks struct tags to produce a nested map of defaults suitable for koanf.Load. It is immutable after construction; safe for concurrent Read calls.
func New ¶
func New(target any, opts Options) (*StructDefaults, error)
New constructs a StructDefaults provider. It validates Options and the target struct shape, applying defaults for any zero-valued option fields. When Options.Strict is true, it additionally performs a full walk of the target struct so that any default-parsing or env-substitution errors surface immediately rather than at the first Read call.
Returns ErrInvalidConfig if Options.Delim is empty, ErrInvalidInput if target is not a non-nil pointer to a struct, or any error produced by an eager Strict-mode walk (ErrCyclicType, ErrInvalidValue, ErrUnsetEnv, ErrUnsupportedType).
func (*StructDefaults) Read ¶
func (p *StructDefaults) Read() (map[string]any, error)
Read walks the struct tags and returns a nested map[string]any whose tree shape mirrors the koanf path layout (split on Options.Delim). Only fields with an explicit DefaultTag contribute entries (sparse output).
func (*StructDefaults) ReadBytes ¶
func (p *StructDefaults) ReadBytes() ([]byte, error)
ReadBytes is not supported. Returns ErrUnsupported.