fix issue in gconv.Struct* functions panic when converting attribute value is nil

This commit is contained in:
John
2020-02-05 22:02:49 +08:00
parent 4d2b244319
commit b15075fdfe
7 changed files with 136 additions and 12 deletions

View File

@ -462,3 +462,39 @@ func Test_IsNil(t *testing.T) {
gtest.Assert(j.IsNil(), true)
})
}
func Test_ToStructDeep(t *testing.T) {
gtest.Case(t, func() {
type Item struct {
Title string `json:"title"`
Key string `json:"key"`
}
type M struct {
Id string `json:"id"`
Me map[string]interface{} `json:"me"`
Txt string `json:"txt"`
Items []*Item `json:"items"`
}
txt := `{
"id":"88888",
"me":{"name":"mikey","day":"20009"},
"txt":"hello",
"items":null
}`
j, err := gjson.LoadContent(txt)
gtest.Assert(err, nil)
gtest.Assert(j.GetString("me.name"), "mikey")
gtest.Assert(j.GetString("items"), "")
gtest.Assert(j.GetBool("items"), false)
gtest.Assert(j.GetArray("items"), nil)
m := new(M)
err = j.ToStructDeep(m)
gtest.Assert(err, nil)
gtest.AssertNE(m.Me, nil)
gtest.Assert(m.Me["day"], "20009")
gtest.Assert(m.Items, nil)
})
}