Have duration.ParseDurationSecond handle json.Number

This commit is contained in:
Jeff Mitchell 2017-03-01 08:32:05 -05:00
parent f2282247ef
commit eec66eaa3c
2 changed files with 13 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package duration
import (
"encoding/json"
"errors"
"strconv"
"strings"
@ -9,6 +10,10 @@ import (
func ParseDurationSecond(in interface{}) (time.Duration, error) {
var dur time.Duration
jsonIn, ok := in.(json.Number)
if ok {
in = jsonIn.String()
}
switch in.(type) {
case string:
inp := in.(string)

View file

@ -1,6 +1,7 @@
package duration
import (
"encoding/json"
"testing"
"time"
)
@ -20,4 +21,11 @@ func Test_ParseDurationSecond(t *testing.T) {
if outp != time.Duration(9876)*time.Second {
t.Fatal("not equivalent")
}
outp, err = ParseDurationSecond(json.Number("4352"))
if err != nil {
t.Fatal(err)
}
if outp != time.Duration(4352)*time.Second {
t.Fatal("not equivalent")
}
}