diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index 14da6eeabe1395c01db4d4e9ddfcc18a44495681..b9ed9b62e9298d3ff8a58188b3412448539e61bc 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package impl import ( @@ -26,13 +43,12 @@ func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.In if invocation.MethodName() == constant.GENERIC { var newArguments = invocation.Arguments() for i := range newArguments { - newArguments[i] = Struct2MapAll(newArguments[i]) + newArguments[i] = struct2MapAll(newArguments[i]) } newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) return invoker.Invoke(newInvocation) } return invoker.Invoke(invocation) - } func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { @@ -42,7 +58,7 @@ func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Inv func GetGenericFilter() filter.Filter { return &GenericFilter{} } -func Struct2MapAll(obj interface{}) map[string]interface{} { +func struct2MapAll(obj interface{}) map[string]interface{} { t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) result := make(map[string]interface{}) @@ -50,9 +66,7 @@ func Struct2MapAll(obj interface{}) map[string]interface{} { for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { if v.Field(i).CanInterface() { - result[headerAtoa(t.Field(i).Name)] = Struct2MapAll(v.Field(i).Interface()) - } else { - println("not in to map,field:" + t.Field(i).Name) + result[headerAtoa(t.Field(i).Name)] = struct2MapAll(v.Field(i).Interface()) } } else { if v.Field(i).CanInterface() { @@ -61,8 +75,6 @@ func Struct2MapAll(obj interface{}) map[string]interface{} { } else { result[tagName] = v.Field(i).Interface() } - } else { - println("not in to map,field:" + t.Field(i).Name) } } } diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3e236a1c08204a1ba0320c691dec22dfe8cd2417 --- /dev/null +++ b/filter/impl/generic_filter_test.go @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package impl + +import ( + "github.com/stretchr/testify/assert" +) +import ( + "fmt" + "testing" +) + +func Test_struct2MapAll(t *testing.T) { + var testData struct { + AaAa string `m:"aaAa"` + BaBa string `m:"baBa"` + CaCa struct { + AaAa string + BaBa string `m:"baBa"` + XxYy struct { + xxXx string `m:"xxXx"` + Xx string `m:"xx"` + } `m:"xxYy"` + } `m:"caCa"` + } + testData.AaAa = "1" + testData.BaBa = "1" + testData.CaCa.BaBa = "2" + testData.CaCa.AaAa = "2" + testData.CaCa.XxYy.xxXx = "3" + testData.CaCa.XxYy.Xx = "3" + m := struct2MapAll(testData) + fmt.Printf("%v", m) + expect := `map[aaAa:1 baBa:1 caCa:map[aaAa:2 baBa:2 xxYy:map[xx:3]]]` + get := fmt.Sprintf("%v", m) + assert.Equal(t, expect, get) +}