Skip to content

Commit 6cde414

Browse files
YangKianunknwon
authored andcommitted
struct: fix incompatibility error with older versions (#219)
* fix incompatibility error with older versions * add a test to make sure fix unspport old version bug. * fix incompatibility error with older versions
1 parent 6982260 commit 6cde414

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

struct.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri
183183
if vt.Name() == "Duration" {
184184
durationVal, err := key.Duration()
185185
if err != nil {
186+
if intVal, err := key.Int64(); err == nil {
187+
field.SetInt(intVal)
188+
return nil
189+
}
186190
return wrapStrictError(err, isStrict)
187191
}
188192
if isPtr {

struct_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type testStruct struct {
4949
Money float64
5050
Born time.Time
5151
Time time.Duration `ini:"Duration"`
52+
OldVersionTime time.Duration
5253
Others testNested
5354
OthersPtr *testNested
5455
NilPtr *testNested
@@ -81,6 +82,7 @@ Male = true
8182
Money = 1.25
8283
Born = 1993-10-07T20:17:05Z
8384
Duration = 2h45m
85+
OldVersionTime = 30
8486
Unsigned = 3
8587
omitthis = true
8688
Shadows = 1, 2
@@ -183,6 +185,8 @@ func Test_MapToStruct(t *testing.T) {
183185
dur, err := time.ParseDuration("2h45m")
184186
So(err, ShouldBeNil)
185187
So(ts.Time.Seconds(), ShouldEqual, dur.Seconds())
188+
189+
So(ts.OldVersionTime * time.Second, ShouldEqual, 30 * time.Second)
186190

187191
So(strings.Join(ts.Others.Cities, ","), ShouldEqual, "HangZhou,Boston")
188192
So(ts.Others.Visits[0].String(), ShouldEqual, t.String())

0 commit comments

Comments
 (0)