Documentation
¶
Overview ¶
Package hooks provides mapstructure decode hooks for use with DcValue[T] fields. When decoding YAML, env, or map[string]any data into structs containing DcValue[T] fields, use MapstructureHookFunc() so that mapstructure automatically constructs properly-typed DcValue instances.
This package targets github.com/mitchellh/mapstructure (v1), which is what viper < 1.18 and many older tools use. For koanf and viper >= 1.18, which use github.com/go-viper/mapstructure/v2, use the sibling package github.com/PapaDanielVi/poya/hooks/mapstructurev2 instead.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONStringHookFunc ¶ added in v0.1.14
func JSONStringHookFunc() mapstructure.DecodeHookFunc
JSONStringHookFunc returns a hook that decodes a JSON string source (for example a struct or array stored as a single config value) into a struct or slice DcValue[T]. Non-matching values pass through unchanged.
func MapstructureHookFunc ¶
func MapstructureHookFunc() mapstructure.DecodeHookFunc
MapstructureHookFunc returns a mapstructure.DecodeHookFunc that detects target fields of type *poya.DcValue[T] and initializes them from the decoded source data.
It handles every DcValue kind:
- scalar T: the source value is converted, or parsed when it arrives as a string (common with env vars and flat config), into T.
- time.Duration T: string sources like "30s" are parsed with time.ParseDuration.
- time.Time T: string sources in RFC3339 are parsed with time.Parse.
- any T whose pointer implements encoding.TextUnmarshaler: parses itself.
- struct T: a map source (typically from YAML) is JSON-encoded and decoded into T.
- slice T: a slice source is JSON-encoded and decoded into the slice.
Usage:
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: hooks.MapstructureHookFunc(),
Result: &cfg,
})
if err != nil { ... }
err = decoder.Decode(viper.AllSettings())
func MapstructureHookFuncValue ¶
func MapstructureHookFuncValue() mapstructure.DecodeHookFuncValue
MapstructureHookFuncValue returns a mapstructure.DecodeHookFuncValue for use with ComposeDecodeHookFunc or when the value hook type is needed.
func StringToDcValueHookFunc ¶ added in v0.1.14
func StringToDcValueHookFunc() mapstructure.DecodeHookFunc
StringToDcValueHookFunc returns a hook that only handles string sources targeting a DcValue[T] with a non-string scalar T (int, uint, float, bool, complex, time.Duration, time.Time). It is the composable piece used when configuration arrives entirely as strings, such as from environment variables. Non-matching values pass through unchanged so it can be combined with other hooks via mapstructure.ComposeDecodeHookFunc.
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
dchook
Package dchook holds the reflection logic shared by poya's mapstructure decode hooks.
|
Package dchook holds the reflection logic shared by poya's mapstructure decode hooks. |
|
Package mapstructurev2 provides decode hooks for DcValue[T] fields that target github.com/go-viper/mapstructure/v2, the actively maintained mapstructure fork.
|
Package mapstructurev2 provides decode hooks for DcValue[T] fields that target github.com/go-viper/mapstructure/v2, the actively maintained mapstructure fork. |