Skip to content
Snippets Groups Projects
Commit f8f19d1e authored by Xin.Zh's avatar Xin.Zh Committed by GitHub
Browse files

Merge pull request #1041 from jack15083/fix_generic_map_issue

Fix generic map convert issue
parents 4fb9e7cd 38ca643a
No related branches found
No related tags found
No related merge requests found
...@@ -122,15 +122,15 @@ func struct2MapAll(obj interface{}) interface{} { ...@@ -122,15 +122,15 @@ func struct2MapAll(obj interface{}) interface{} {
} }
return newTemps return newTemps
} else if t.Kind() == reflect.Map { } else if t.Kind() == reflect.Map {
var newTempMap = make(map[string]interface{}, v.Len()) var newTempMap = make(map[interface{}]interface{}, v.Len())
iter := v.MapRange() iter := v.MapRange()
for iter.Next() { for iter.Next() {
mapK := iter.Key().String()
if !iter.Value().CanInterface() { if !iter.Value().CanInterface() {
continue continue
} }
key := iter.Key()
mapV := iter.Value().Interface() mapV := iter.Value().Interface()
newTempMap[mapK] = struct2MapAll(mapV) newTempMap[convertMapKey(key)] = struct2MapAll(mapV)
} }
return newTempMap return newTempMap
} else { } else {
...@@ -138,6 +138,19 @@ func struct2MapAll(obj interface{}) interface{} { ...@@ -138,6 +138,19 @@ func struct2MapAll(obj interface{}) interface{} {
} }
} }
func convertMapKey(key reflect.Value) interface{} {
switch key.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8,
reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Float32,
reflect.Float64, reflect.String:
return key.Interface()
default:
return key.String()
}
}
func setInMap(m map[string]interface{}, structField reflect.StructField, value interface{}) (result map[string]interface{}) { func setInMap(m map[string]interface{}, structField reflect.StructField, value interface{}) (result map[string]interface{}) {
result = m result = m
if tagName := structField.Tag.Get("m"); tagName == "" { if tagName := structField.Tag.Get("m"); tagName == "" {
......
...@@ -98,15 +98,17 @@ func TestStruct2MapAllSlice(t *testing.T) { ...@@ -98,15 +98,17 @@ func TestStruct2MapAllSlice(t *testing.T) {
func TestStruct2MapAllMap(t *testing.T) { func TestStruct2MapAllMap(t *testing.T) {
var testData struct { var testData struct {
AaAa string AaAa string
Baba map[string]interface{} Baba map[string]interface{}
CaCa map[string]string CaCa map[string]string
DdDd map[string]interface{} DdDd map[string]interface{}
IntMap map[int]interface{}
} }
testData.AaAa = "aaaa" testData.AaAa = "aaaa"
testData.Baba = make(map[string]interface{}) testData.Baba = make(map[string]interface{})
testData.CaCa = make(map[string]string) testData.CaCa = make(map[string]string)
testData.DdDd = nil testData.DdDd = nil
testData.IntMap = make(map[int]interface{})
testData.Baba["kk"] = 1 testData.Baba["kk"] = 1
var structData struct { var structData struct {
...@@ -117,14 +119,19 @@ func TestStruct2MapAllMap(t *testing.T) { ...@@ -117,14 +119,19 @@ func TestStruct2MapAllMap(t *testing.T) {
testData.Baba["nil"] = nil testData.Baba["nil"] = nil
testData.CaCa["k1"] = "v1" testData.CaCa["k1"] = "v1"
testData.CaCa["kv2"] = "v2" testData.CaCa["kv2"] = "v2"
testData.IntMap[1] = 1
m := struct2MapAll(testData) m := struct2MapAll(testData)
assert.Equal(t, reflect.Map, reflect.TypeOf(m).Kind()) assert.Equal(t, reflect.Map, reflect.TypeOf(m).Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(m.(map[string]interface{})["aaAa"]).Kind()) mappedStruct := m.(map[string]interface{})
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["baba"]).Kind()) assert.Equal(t, reflect.String, reflect.TypeOf(mappedStruct["aaAa"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["baba"].(map[string]interface{})["struct"]).Kind()) assert.Equal(t, reflect.Map, reflect.TypeOf(mappedStruct["baba"]).Kind())
assert.Equal(t, "str", m.(map[string]interface{})["baba"].(map[string]interface{})["struct"].(map[string]interface{})["str"]) assert.Equal(t, reflect.Map, reflect.TypeOf(mappedStruct["baba"].(map[interface{}]interface{})["struct"]).Kind())
assert.Equal(t, nil, m.(map[string]interface{})["baba"].(map[string]interface{})["nil"]) assert.Equal(t, "str", mappedStruct["baba"].(map[interface{}]interface{})["struct"].(map[string]interface{})["str"])
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["caCa"]).Kind()) assert.Equal(t, nil, mappedStruct["baba"].(map[interface{}]interface{})["nil"])
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["ddDd"]).Kind()) assert.Equal(t, reflect.Map, reflect.TypeOf(mappedStruct["caCa"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(mappedStruct["ddDd"]).Kind())
intMap := mappedStruct["intMap"]
assert.Equal(t, reflect.Map, reflect.TypeOf(intMap).Kind())
assert.Equal(t, 1, intMap.(map[interface{}]interface{})[1])
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment