Have duration.ParseDurationSecond handle json.Number
This commit is contained in:
parent
f2282247ef
commit
eec66eaa3c
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue