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
This commit is contained in:
YangKian
2019-12-25 10:24:32 +08:00
committed by Unknwon
parent 6982260a37
commit 6cde41423e
2 changed files with 8 additions and 0 deletions
+4
View File
@@ -183,6 +183,10 @@ func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri
if vt.Name() == "Duration" {
durationVal, err := key.Duration()
if err != nil {
if intVal, err := key.Int64(); err == nil {
field.SetInt(intVal)
return nil
}
return wrapStrictError(err, isStrict)
}
if isPtr {
+4
View File
@@ -49,6 +49,7 @@ type testStruct struct {
Money float64
Born time.Time
Time time.Duration `ini:"Duration"`
OldVersionTime time.Duration
Others testNested
OthersPtr *testNested
NilPtr *testNested
@@ -81,6 +82,7 @@ Male = true
Money = 1.25
Born = 1993-10-07T20:17:05Z
Duration = 2h45m
OldVersionTime = 30
Unsigned = 3
omitthis = true
Shadows = 1, 2
@@ -183,6 +185,8 @@ func Test_MapToStruct(t *testing.T) {
dur, err := time.ParseDuration("2h45m")
So(err, ShouldBeNil)
So(ts.Time.Seconds(), ShouldEqual, dur.Seconds())
So(ts.OldVersionTime * time.Second, ShouldEqual, 30 * time.Second)
So(strings.Join(ts.Others.Cities, ","), ShouldEqual, "HangZhou,Boston")
So(ts.Others.Visits[0].String(), ShouldEqual, t.String())