From cc6c57b3b971c3af2be8db2992c88616a4682545 Mon Sep 17 00:00:00 2001 From: UNILORN Date: Thu, 28 Apr 2022 17:25:58 +0900 Subject: [PATCH] Fixed: time.Duration raw strings value --- defaults.go | 6 +++++- defaults_test.go | 16 +++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/defaults.go b/defaults.go index 5513b98..e131f53 100644 --- a/defaults.go +++ b/defaults.go @@ -89,7 +89,11 @@ func newDefaultFiller() *Filler { types := make(map[TypeHash]FillerFunc, 1) types["time.Duration"] = func(field *FieldData) { - d, _ := time.ParseDuration(field.TagValue) + d, err := time.ParseDuration(field.TagValue) + if err != nil { + v, _ := strconv.ParseInt(field.TagValue, 10, 64) + d = time.Duration(v) + } field.Value.Set(reflect.ValueOf(d)) } diff --git a/defaults_test.go b/defaults_test.go index c86a960..a3bda99 100644 --- a/defaults_test.go +++ b/defaults_test.go @@ -52,13 +52,14 @@ type ExampleBasic struct { Bool bool `default:"true"` Integer int `default:"33"` } - Duration time.Duration `default:"1s"` - Children []Child - Second time.Duration `default:"1s"` - StringSlice []string `default:"[1,2,3,4]"` - IntSlice []int `default:"[1,2,3,4]"` - IntSliceSlice [][]int `default:"[[1],[2],[3],[4]]"` - StringSliceSlice [][]string `default:"[[1],[]]"` + Duration time.Duration `default:"1s"` + Children []Child + Second time.Duration `default:"1s"` + Integer64Duration time.Duration `default:"1000000000"` + StringSlice []string `default:"[1,2,3,4]"` + IntSlice []int `default:"[1,2,3,4]"` + IntSliceSlice [][]int `default:"[[1],[2],[3],[4]]"` + StringSliceSlice [][]string `default:"[[1],[]]"` DateTime string `default:"{{date:1,-10,0}} {{time:1,-5,10}}"` } @@ -101,6 +102,7 @@ func (s *DefaultsSuite) assertTypes(c *C, foo *ExampleBasic) { c.Assert(foo.Duration, Equals, time.Second) c.Assert(foo.Children, IsNil) c.Assert(foo.Second, Equals, time.Second) + c.Assert(foo.Integer64Duration, Equals, time.Second) c.Assert(foo.StringSlice, DeepEquals, []string{"1", "2", "3", "4"}) c.Assert(foo.IntSlice, DeepEquals, []int{1, 2, 3, 4}) c.Assert(foo.IntSliceSlice, DeepEquals, [][]int{[]int{1}, []int{2}, []int{3}, []int{4}})