Skip to content

Commit

Permalink
refactor(generic_filter): struct2MapAll key of map keep type of int f…
Browse files Browse the repository at this point in the history
…loat string
  • Loading branch information
lifangzheng committed Dec 14, 2020
1 parent 1156b92 commit 8e47677
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
19 changes: 16 additions & 3 deletions filter/filter_impl/generic_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,35 @@ func struct2MapAll(obj interface{}) interface{} {
}
return newTemps
} 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()
for iter.Next() {
mapK := iter.Key().String()
if !iter.Value().CanInterface() {
continue
}
key := iter.Key()
mapV := iter.Value().Interface()
newTempMap[mapK] = struct2MapAll(mapV)
newTempMap[convertMapKey(key)] = struct2MapAll(mapV)
}
return newTempMap
} else {
return obj
}
}

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{}) {
result = m
if tagName := structField.Tag.Get("m"); tagName == "" {
Expand Down
29 changes: 18 additions & 11 deletions filter/filter_impl/generic_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,17 @@ func TestStruct2MapAllSlice(t *testing.T) {

func TestStruct2MapAllMap(t *testing.T) {
var testData struct {
AaAa string
Baba map[string]interface{}
CaCa map[string]string
DdDd map[string]interface{}
AaAa string
Baba map[string]interface{}
CaCa map[string]string
DdDd map[string]interface{}
IntMap map[int]interface{}
}
testData.AaAa = "aaaa"
testData.Baba = make(map[string]interface{})
testData.CaCa = make(map[string]string)
testData.DdDd = nil
testData.IntMap = make(map[int]interface{})

testData.Baba["kk"] = 1
var structData struct {
Expand All @@ -117,14 +119,19 @@ func TestStruct2MapAllMap(t *testing.T) {
testData.Baba["nil"] = nil
testData.CaCa["k1"] = "v1"
testData.CaCa["kv2"] = "v2"
testData.IntMap[1] = 1
m := struct2MapAll(testData)

assert.Equal(t, reflect.Map, reflect.TypeOf(m).Kind())
assert.Equal(t, reflect.String, reflect.TypeOf(m.(map[string]interface{})["aaAa"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["baba"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["baba"].(map[string]interface{})["struct"]).Kind())
assert.Equal(t, "str", m.(map[string]interface{})["baba"].(map[string]interface{})["struct"].(map[string]interface{})["str"])
assert.Equal(t, nil, m.(map[string]interface{})["baba"].(map[string]interface{})["nil"])
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["caCa"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(m.(map[string]interface{})["ddDd"]).Kind())
mappedStruct := m.(map[string]interface{})
assert.Equal(t, reflect.String, reflect.TypeOf(mappedStruct["aaAa"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(mappedStruct["baba"]).Kind())
assert.Equal(t, reflect.Map, reflect.TypeOf(mappedStruct["baba"].(map[interface{}]interface{})["struct"]).Kind())
assert.Equal(t, "str", mappedStruct["baba"].(map[interface{}]interface{})["struct"].(map[string]interface{})["str"])
assert.Equal(t, nil, mappedStruct["baba"].(map[interface{}]interface{})["nil"])
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 comments on commit 8e47677

Please sign in to comment.