CastFieldIfToString with time.Duration support for acd/tcd

This commit is contained in:
DanB
2017-10-22 17:21:50 +02:00
parent f50f833c1c
commit c135290fc3
2 changed files with 11 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -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)
}
}