|
56 | 56 | confidentialValueDecoder(),
|
57 | 57 | ))
|
58 | 58 |
|
| 59 | + configOptionV2 = viper.DecodeHook(mapstructure.ComposeDecodeHookFunc( |
| 60 | + mapstructure.StringToTimeDurationHookFunc(), |
| 61 | + confidentialValueDecoder(), |
| 62 | + forceSliceReplacementHookFunc(), |
| 63 | + )) |
| 64 | + |
59 | 65 | configOptionHCL = viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
|
60 | 66 | mapstructure.StringToTimeDurationHookFunc(),
|
61 | 67 | confidentialValueDecoder(),
|
@@ -585,10 +591,28 @@ func (c *Config) getSchedule(key string) (Schedule, error) {
|
585 | 591 | func (c *Config) unmarshalKey(key string, rawVal interface{}) error {
|
586 | 592 | if c.format == "hcl" {
|
587 | 593 | return c.viper.UnmarshalKey(key, rawVal, configOptionHCL)
|
| 594 | + } else if c.GetVersion() >= Version02 { |
| 595 | + return c.viper.UnmarshalKey(key, rawVal, configOptionV2) |
588 | 596 | }
|
589 | 597 | return c.viper.UnmarshalKey(key, rawVal, configOption)
|
590 | 598 | }
|
591 | 599 |
|
| 600 | +// forceSliceReplacementHookFunc clears the target slice before assigning a new value |
| 601 | +func forceSliceReplacementHookFunc() mapstructure.DecodeHookFuncValue { |
| 602 | + return func(from reflect.Value, to reflect.Value) (interface{}, error) { |
| 603 | + if kind := from.Type().Kind(); (kind == reflect.Slice || kind == reflect.Array || kind == reflect.String) && from.Len() > 0 { |
| 604 | + if kind = to.Type().Kind(); (kind == reflect.Slice || kind == reflect.Array) && to.Len() > 0 { |
| 605 | + if kind == reflect.Slice { |
| 606 | + to.SetLen(0) |
| 607 | + } else { |
| 608 | + to.Set(to.Slice(0, 0)) |
| 609 | + } |
| 610 | + } |
| 611 | + } |
| 612 | + return from.Interface(), nil |
| 613 | + } |
| 614 | +} |
| 615 | + |
592 | 616 | // sliceOfMapsToMapHookFunc merges a slice of maps to a map
|
593 | 617 | func sliceOfMapsToMapHookFunc() mapstructure.DecodeHookFunc {
|
594 | 618 | return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
|
|
0 commit comments