fix issue in gconv.MapDeep

This commit is contained in:
John Guo
2021-06-05 08:58:54 +08:00
parent 8aa7f08350
commit eb723e47c2
2 changed files with 55 additions and 1 deletions

View File

@ -301,7 +301,7 @@ func doMapConvertForMapOrStructValue(isRoot bool, value interface{}, recursive b
// It means this attribute field has desired tag.
dataMap[mapKey] = doMapConvertForMapOrStructValue(false, rvAttrInterface, true, tags...)
} else {
dataMap[mapKey] = doMapConvertForMapOrStructValue(false, rvAttrInterface, false, tags...)
dataMap[mapKey] = doMapConvertForMapOrStructValue(false, rvAttrInterface, recursive, tags...)
}
// The struct attribute is type of slice.

View File

@ -317,6 +317,60 @@ func Test_MapDeep2(t *testing.T) {
})
}
func Test_MapDeep3(t *testing.T) {
type Base struct {
Id int `c:"id"`
Date string `c:"date"`
}
type User struct {
UserBase Base `c:"base"`
Passport string `c:"passport"`
Password string `c:"password"`
Nickname string `c:"nickname"`
}
gtest.C(t, func(t *gtest.T) {
user := &User{
UserBase: Base{
Id: 1,
Date: "2019-10-01",
},
Passport: "john",
Password: "123456",
Nickname: "JohnGuo",
}
m := gconv.MapDeep(user)
t.Assert(m, g.Map{
"base": g.Map{
"id": user.UserBase.Id,
"date": user.UserBase.Date,
},
"passport": user.Passport,
"password": user.Password,
"nickname": user.Nickname,
})
})
gtest.C(t, func(t *gtest.T) {
user := &User{
UserBase: Base{
Id: 1,
Date: "2019-10-01",
},
Passport: "john",
Password: "123456",
Nickname: "JohnGuo",
}
m := gconv.Map(user)
t.Assert(m, g.Map{
"base": user.UserBase,
"passport": user.Passport,
"password": user.Password,
"nickname": user.Nickname,
})
})
}
func Test_MapDeepWithAttributeTag(t *testing.T) {
type Ids struct {
Id int `c:"id"`