diff --git a/internal/configuration/decode_hooks.go b/internal/configuration/decode_hooks.go index f8ce99f6a..939347639 100644 --- a/internal/configuration/decode_hooks.go +++ b/internal/configuration/decode_hooks.go @@ -67,7 +67,11 @@ func ToTimeDurationFunc() mapstructure.DecodeHookFuncType { switch { case f.Kind() == reflect.String: - break + dataStr := data.(string) + + if duration, err = utils.ParseDurationString(dataStr); err != nil { + return nil, err + } case f.Kind() == reflect.Int: seconds := data.(int) @@ -84,14 +88,6 @@ func ToTimeDurationFunc() mapstructure.DecodeHookFuncType { duration = time.Second * time.Duration(seconds) } - if duration == 0 { - dataStr := data.(string) - - if duration, err = utils.ParseDurationString(dataStr); err != nil { - return nil, err - } - } - if ptr { return &duration, nil } diff --git a/internal/configuration/decode_hooks_test.go b/internal/configuration/decode_hooks_test.go index 2b038cdb3..41b665e0c 100644 --- a/internal/configuration/decode_hooks_test.go +++ b/internal/configuration/decode_hooks_test.go @@ -268,3 +268,25 @@ func TestToTimeDurationFunc_ShouldNotParse_FromBool(t *testing.T) { assert.NoError(t, err) assert.Equal(t, from, result) } + +func TestToTimeDurationFunc_ShouldParse_FromZero(t *testing.T) { + hook := ToTimeDurationFunc() + + var ( + from = 0 + expected = time.Duration(0) + + to time.Duration + ptrTo *time.Duration + result interface{} + err error + ) + + result, err = hook(reflect.TypeOf(from), reflect.TypeOf(to), from) + assert.NoError(t, err) + assert.Equal(t, expected, result) + + result, err = hook(reflect.TypeOf(from), reflect.TypeOf(ptrTo), from) + assert.NoError(t, err) + assert.Equal(t, &expected, result) +}