diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 7534f9175..89bf8fc0f 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -332,6 +332,9 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in mapKey = strings.TrimSpace(array[0]) } } + if mapKey == "" { + mapKey = fieldName + } } if in.RecursiveOption || rtField.Anonymous { // Do map converting recursively. diff --git a/util/gconv/gconv_z_unit_map_test.go b/util/gconv/gconv_z_unit_map_test.go index 3ac544d8f..efa6e0fd4 100644 --- a/util/gconv/gconv_z_unit_map_test.go +++ b/util/gconv/gconv_z_unit_map_test.go @@ -606,3 +606,17 @@ func TestMapsDeep(t *testing.T) { 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}) + }) +}