fix issue struct attribute converting when has no json name but with omitempty tag (#2486)

This commit is contained in:
John Guo 2023-03-07 21:26:32 +08:00 committed by GitHub
parent e8088a6563
commit 13f6fb1929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -332,6 +332,9 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in
mapKey = strings.TrimSpace(array[0]) mapKey = strings.TrimSpace(array[0])
} }
} }
if mapKey == "" {
mapKey = fieldName
}
} }
if in.RecursiveOption || rtField.Anonymous { if in.RecursiveOption || rtField.Anonymous {
// Do map converting recursively. // Do map converting recursively.

View File

@ -606,3 +606,17 @@ func TestMapsDeep(t *testing.T) {
t.Assert(list[1]["id"], 200) t.Assert(list[1]["id"], 200)
}) })
} }
func TestMapWithJsonOmitEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type S struct {
Key string `json:",omitempty"`
Value interface{} `json:",omitempty"`
}
s := S{
Key: "",
Value: 1,
}
t.Assert(gconv.Map(s), g.Map{"Value": 1})
})
}