feat: allow setting a default decode hook

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-24 18:20:56 +02:00
parent 9a962379e6
commit ceb3d00ef2
No known key found for this signature in database
GPG key ID: 31AB0439F4C5C90E
2 changed files with 63 additions and 10 deletions

View file

@ -183,6 +183,8 @@ type Viper struct {
encoderRegistry EncoderRegistry
decoderRegistry DecoderRegistry
decodeHook mapstructure.DecodeHookFunc
experimentalFinder bool
experimentalBindStruct bool
}
@ -251,6 +253,17 @@ func EnvKeyReplacer(r StringReplacer) Option {
})
}
// WithDecodeHook sets a default decode hook for mapstructure.
func WithDecodeHook(h mapstructure.DecodeHookFunc) Option {
return optionFunc(func(v *Viper) {
if h == nil {
return
}
v.decodeHook = h
})
}
// NewWithOptions creates a new Viper instance.
func NewWithOptions(opts ...Option) *Viper {
v := New()
@ -896,7 +909,7 @@ func UnmarshalKey(key string, rawVal any, opts ...DecoderConfigOption) error {
}
func (v *Viper) UnmarshalKey(key string, rawVal any, opts ...DecoderConfigOption) error {
return decode(v.Get(key), defaultDecoderConfig(rawVal, opts...))
return decode(v.Get(key), v.defaultDecoderConfig(rawVal, opts...))
}
// Unmarshal unmarshals the config into a Struct. Make sure that the tags
@ -919,13 +932,13 @@ func (v *Viper) Unmarshal(rawVal any, opts ...DecoderConfigOption) error {
}
// TODO: struct keys should be enough?
return decode(v.getSettings(keys), defaultDecoderConfig(rawVal, opts...))
return decode(v.getSettings(keys), v.defaultDecoderConfig(rawVal, opts...))
}
func (v *Viper) decodeStructKeys(input any, opts ...DecoderConfigOption) ([]string, error) {
var structKeyMap map[string]any
err := decode(input, defaultDecoderConfig(&structKeyMap, opts...))
err := decode(input, v.defaultDecoderConfig(&structKeyMap, opts...))
if err != nil {
return nil, err
}
@ -942,15 +955,20 @@ func (v *Viper) decodeStructKeys(input any, opts ...DecoderConfigOption) ([]stri
// defaultDecoderConfig returns default mapstructure.DecoderConfig with support
// of time.Duration values & string slices.
func defaultDecoderConfig(output any, opts ...DecoderConfigOption) *mapstructure.DecoderConfig {
c := &mapstructure.DecoderConfig{
Metadata: nil,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
func (v *Viper) defaultDecoderConfig(output any, opts ...DecoderConfigOption) *mapstructure.DecoderConfig {
decodeHook := v.decodeHook
if decodeHook == nil {
decodeHook = mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
// mapstructure.StringToSliceHookFunc(","),
stringToWeakSliceHookFunc(","),
),
)
}
c := &mapstructure.DecoderConfig{
Metadata: nil,
WeaklyTypedInput: true,
DecodeHook: decodeHook,
}
for _, opt := range opts {
@ -1001,7 +1019,7 @@ func UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error {
}
func (v *Viper) UnmarshalExact(rawVal any, opts ...DecoderConfigOption) error {
config := defaultDecoderConfig(rawVal, opts...)
config := v.defaultDecoderConfig(rawVal, opts...)
config.ErrorUnused = true
keys := v.AllKeys()

View file

@ -894,6 +894,41 @@ func TestUnmarshal(t *testing.T) {
)
}
func TestUnmarshalWithDefaultDecodeHook(t *testing.T) {
opt := mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
// Custom Decode Hook Function
func(rf reflect.Kind, rt reflect.Kind, data any) (any, error) {
if rf != reflect.String || rt != reflect.Map {
return data, nil
}
m := map[string]string{}
raw := data.(string)
if raw == "" {
return m, nil
}
err := json.Unmarshal([]byte(raw), &m)
return m, err
},
)
v := NewWithOptions(WithDecodeHook(opt))
v.Set("credentials", "{\"foo\":\"bar\"}")
type config struct {
Credentials map[string]string
}
var C config
require.NoError(t, v.Unmarshal(&C), "unable to decode into struct")
assert.Equal(t, &config{
Credentials: map[string]string{"foo": "bar"},
}, &C)
}
func TestUnmarshalWithDecoderOptions(t *testing.T) {
v := New()
v.Set("credentials", "{\"foo\":\"bar\"}")