reflect.IfaceToDuration to consider float64 as nanoseconds since also ints are converted to float

This commit is contained in:
DanB
2018-08-20 13:21:06 +02:00
parent 9f0dd9842c
commit 88a31dd533
2 changed files with 3 additions and 3 deletions

View File

@@ -169,8 +169,8 @@ func IfaceAsDuration(itm interface{}) (d time.Duration, err error) {
switch itm.(type) {
case time.Duration:
return itm.(time.Duration), nil
case float64:
return time.Duration(int64(itm.(float64) * float64(time.Second))), nil
case float64: // automatically hitting here also ints
return time.Duration(int64(itm.(float64))), nil
case int64:
return time.Duration(itm.(int64)), nil
case string:

View File

@@ -277,7 +277,7 @@ func TestIfaceAsDuration(t *testing.T) {
} else if eItm != itmConvert {
t.Errorf("received: %+v", itmConvert)
}
if itmConvert, err := IfaceAsDuration(interface{}(float64(1.0))); err != nil {
if itmConvert, err := IfaceAsDuration(interface{}(float64(1000000000.0))); err != nil {
t.Error(err)
} else if eItm != itmConvert {
t.Errorf("received: %+v", itmConvert)