bugfix:fix gconv map deep option not effective (#3287)

This commit is contained in:
conwaypan 2024-01-29 19:52:04 +08:00 committed by GitHub
parent 4ddc9c3909
commit c4a51f4c2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 0 deletions

View File

@ -74,6 +74,9 @@ func doMapConvert(value interface{}, recursive recursiveType, mustMapReturn bool
usedOption = getUsedMapOption(option...)
newTags = StructTagPriority
)
if usedOption.Deep {
recursive = recursiveTypeTrue
}
switch len(usedOption.Tags) {
case 0:
// No need handling.

View File

@ -538,6 +538,60 @@ field3:
})
}
func Test_MapWithDeepOption(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.Map(user)
t.Assert(m, g.Map{
"base": user.UserBase,
"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, gconv.MapOption{Deep: true})
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,
})
})
}
func TestMapStrStr(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gconv.MapStrStr(map[string]string{"k": "v"}), map[string]string{"k": "v"})