You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2020/12/27 02:31:49 UTC
[dubbo-go] branch develop updated: refactor(generic_filter):
struct2MapAll key of map keep type of int float string
This is an automated email from the ASF dual-hosted git repository.
alexstocks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new e3c5cf3 refactor(generic_filter): struct2MapAll key of map keep type of int float string
new 9449801 Merge pull request #928 from ljun20160606/master
e3c5cf3 is described below
commit e3c5cf3e7fac2e7dddbfe5a22bf153d3fd79b7de
Author: ljun20160606 <lj...@gmail.com>
AuthorDate: Fri Dec 11 21:47:37 2020 +0800
refactor(generic_filter): struct2MapAll key of map keep type of int float string
---
filter/filter_impl/generic_filter.go | 19 ++++++++++++++++---
filter/filter_impl/generic_filter_test.go | 29 ++++++++++++++++++-----------
2 files changed, 34 insertions(+), 14 deletions(-)
diff --git a/filter/filter_impl/generic_filter.go b/filter/filter_impl/generic_filter.go
index 36b4b13..cf307d0 100644
--- a/filter/filter_impl/generic_filter.go
+++ b/filter/filter_impl/generic_filter.go
@@ -122,15 +122,15 @@ 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 {
@@ -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{}) {
result = m
if tagName := structField.Tag.Get("m"); tagName == "" {
diff --git a/filter/filter_impl/generic_filter_test.go b/filter/filter_impl/generic_filter_test.go
index 40cf743..4203dd6 100644
--- a/filter/filter_impl/generic_filter_test.go
+++ b/filter/filter_impl/generic_filter_test.go
@@ -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 {
@@ -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])
}