From c135290fc39d5f9c6ac5dcaa51480f45ae3fa5f7 Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 22 Oct 2017 17:21:50 +0200 Subject: [PATCH] CastFieldIfToString with time.Duration support for acd/tcd --- utils/reflect.go | 3 +++ utils/reflect_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) 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) + } +}