Skip to content

Commit

Permalink
fix: config parsing backward compatibility for time.Duration (#67)
Browse files Browse the repository at this point in the history
fix: config parsing decoder hook backward compatibility

In last release, mapstructure decode overrides were introduced but by default
they were overriding the viper(library we use for config parsing) options of
mapstructure decoding. Adding those options back would make this change backward
compatible.

For example, in shield this is causing failure in parsing "10m" as time.Duration type

Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
  • Loading branch information
kushsharma authored Jun 27, 2023
1 parent 2768269 commit 5201185
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ func NewLoader(options ...LoaderOption) *Loader {
v: getViperWithDefaults(),
opts: []viper.DecoderConfigOption{
viper.DecodeHook(
StringToJsonFunc(),
mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","), // default delimiter
StringToJsonFunc(),
),
),
},
}
Expand Down Expand Up @@ -234,8 +238,8 @@ func getFlattenedStructKeys(config interface{}) ([]string, error) {
return keys, nil
}

// StringToJsonFunc is a mapstructure.DecodeHookFunc that converts a string to a interface{}
// if the string is valid json. This is useful for unmarshaling json strings into a map.
// StringToJsonFunc is a mapstructure.DecodeHookFunc that converts a string to an interface{}
// if the string is valid json. This is useful for unmarshalling json strings into a map.
// For example, if you have a struct with a field of type map[string]string like labels or annotations,
func StringToJsonFunc() mapstructure.DecodeHookFunc {
return func(f, t reflect.Type, data interface{}) (interface{}, error) {
Expand Down

0 comments on commit 5201185

Please sign in to comment.