diff options
| author | James Elliott <james-d-elliott@users.noreply.github.com> | 2023-11-23 08:20:36 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-23 08:20:36 +1100 |
| commit | c49b973120c7fd755923a2b88afd794c7d320d6e (patch) | |
| tree | 5fcef6de1a85568eee3c67470f73b790b294dda8 /internal/configuration/decode_hooks.go | |
| parent | fa141929a39e546f3f3ca6bcbc7bd72c64e575c8 (diff) | |
fix(configuration): illogical refresh interval default (#6319)
When using the file provider with watch enabled, the refresh interval should just be set to always default as the cost is minimal.
Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
Diffstat (limited to 'internal/configuration/decode_hooks.go')
| -rw-r--r-- | internal/configuration/decode_hooks.go | 153 |
1 files changed, 112 insertions, 41 deletions
diff --git a/internal/configuration/decode_hooks.go b/internal/configuration/decode_hooks.go index 2a6c5ff93..745e27d9d 100644 --- a/internal/configuration/decode_hooks.go +++ b/internal/configuration/decode_hooks.go @@ -113,9 +113,117 @@ func StringToURLHookFunc() mapstructure.DecodeHookFuncType { } } +func DecodeTimeDuration(f, expectedType reflect.Type, prefixType string, data any) (result time.Duration, err error) { + e := reflect.TypeOf(time.Duration(0)) + + switch { + case f.Kind() == reflect.String: + dataStr := data.(string) + + if result, err = utils.ParseDurationString(dataStr); err != nil { + return time.Duration(0), fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err) + } + case f.Kind() == reflect.Int: + seconds := data.(int) + + result = time.Second * time.Duration(seconds) + case f.Kind() == reflect.Int8: + seconds := data.(int8) + + result = time.Second * time.Duration(seconds) + case f.Kind() == reflect.Int16: + seconds := data.(int16) + + result = time.Second * time.Duration(seconds) + case f.Kind() == reflect.Int32: + seconds := data.(int32) + + result = time.Second * time.Duration(seconds) + case f.Kind() == reflect.Float64: + fseconds := data.(float64) + + if fseconds > durationMax.Seconds() { + result = durationMax + } else { + seconds, _ := strconv.Atoi(fmt.Sprintf("%.0f", fseconds)) + + result = time.Second * time.Duration(seconds) + } + case f == e: + result = data.(time.Duration) + case f.Kind() == reflect.Int64: + seconds := data.(int64) + + result = time.Second * time.Duration(seconds) + } + + return result, nil +} + +// ToRefreshIntervalDurationHookFunc converts string and integer types to a schema.RefreshIntervalDuration. +func ToRefreshIntervalDurationHookFunc() mapstructure.DecodeHookFuncType { + return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { + var ptr bool + + switch f.Kind() { + case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float64: + // We only allow string and integer from kinds to match. + break + default: + return data, nil + } + + prefixType := "" + + if t.Kind() == reflect.Ptr { + ptr = true + prefixType = "*" + } + + expectedType := reflect.TypeOf(schema.RefreshIntervalDuration{}) + + if ptr && t.Elem() != expectedType { + return data, nil + } else if !ptr && t != expectedType { + return data, nil + } + + var ( + result schema.RefreshIntervalDuration + decoded bool + ) + + if f.Kind() == reflect.String { + dataStr, ok := data.(string) + if ok { + switch dataStr { + case schema.ProfileRefreshAlways: + result, decoded = schema.NewRefreshIntervalDurationAlways(), true + case schema.ProfileRefreshDisabled: + result, decoded = schema.NewRefreshIntervalDurationNever(), true + } + } + } + + if !decoded { + var resultv time.Duration + + if resultv, err = DecodeTimeDuration(f, expectedType, prefixType, data); err != nil { + return nil, err + } + + result = schema.NewRefreshIntervalDuration(resultv) + } + + if ptr { + return &result, nil + } + + return result, nil + } +} + // ToTimeDurationHookFunc converts string and integer types to a time.Duration. -// -//nolint:gocyclo // Function is necessarily complex though flows well due to switch statement usage. func ToTimeDurationHookFunc() mapstructure.DecodeHookFuncType { return func(f reflect.Type, t reflect.Type, data any) (value any, err error) { var ( @@ -146,45 +254,8 @@ func ToTimeDurationHookFunc() mapstructure.DecodeHookFuncType { var result time.Duration - switch { - case f.Kind() == reflect.String: - dataStr := data.(string) - - if result, err = utils.ParseDurationString(dataStr); err != nil { - return nil, fmt.Errorf(errFmtDecodeHookCouldNotParse, dataStr, prefixType, expectedType, err) - } - case f.Kind() == reflect.Int: - seconds := data.(int) - - result = time.Second * time.Duration(seconds) - case f.Kind() == reflect.Int8: - seconds := data.(int8) - - result = time.Second * time.Duration(seconds) - case f.Kind() == reflect.Int16: - seconds := data.(int16) - - result = time.Second * time.Duration(seconds) - case f.Kind() == reflect.Int32: - seconds := data.(int32) - - result = time.Second * time.Duration(seconds) - case f.Kind() == reflect.Float64: - fseconds := data.(float64) - - if fseconds > durationMax.Seconds() { - result = durationMax - } else { - seconds, _ := strconv.Atoi(fmt.Sprintf("%.0f", fseconds)) - - result = time.Second * time.Duration(seconds) - } - case f == expectedType: - result = data.(time.Duration) - case f.Kind() == reflect.Int64: - seconds := data.(int64) - - result = time.Second * time.Duration(seconds) + if result, err = DecodeTimeDuration(f, expectedType, prefixType, data); err != nil { + return nil, err } if ptr { |
