diff --git a/utils/reflect.go b/utils/reflect.go index 39e8c4aec..0ae691815 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -50,6 +50,9 @@ func CastFieldIfToString(fld interface{}) (string, bool) { if byteVal, converted = fld.([]byte); converted { strVal = string(byteVal) } + case time.Duration: + strVal = fld.(time.Duration).String() + converted = true default: // Maybe we are lucky and the value converts to string strVal, converted = fld.(string) } diff --git a/utils/reflect_test.go b/utils/reflect_test.go index bd6878d66..3e9c990b1 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -216,3 +216,11 @@ func TestStringToInterface(t *testing.T) { t.Error("not parsing time.Duration") } } + +func TestCastFieldIfToString(t *testing.T) { + if strVal, cast := CastFieldIfToString(time.Duration(1 * time.Second)); !cast { + t.Error("cannot cast time.Duration") + } else if strVal != "1s" { + t.Errorf("received: %s", strVal) + } +}