Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(generic_filter): struct2MapAll key of map keep type of int f… #928

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
ljun20160606 marked this conversation as resolved.
Show resolved Hide resolved

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])
}