From ee614f9c6b1f71e537df4a00984637c38c1f3b39 Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 23 Feb 2022 10:27:31 +0800 Subject: [PATCH] fix issue #1563 --- util/gconv/gconv_struct.go | 14 +++++++++----- util/gconv/gconv_z_unit_struct_test.go | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index 628baf3c5..b24d9d711 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -196,7 +196,7 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string elemFieldType reflect.StructField elemFieldValue reflect.Value elemType = pointerElemReflectValue.Type() - attrMap = make(map[string]string) + attrMap = make(map[string]string) // Attribute name to its check name which has no symbols. ) for i := 0; i < pointerElemReflectValue.NumField(); i++ { elemFieldType = elemType.Field(i) @@ -229,7 +229,7 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string // The key of the tagMap is the attribute name of the struct, // and the value is its replaced tag name for later comparison to improve performance. var ( - tagMap = make(map[string]string) + tagMap = make(map[string]string) // Tag name to its check name which has no symbols. priorityTagArray []string ) if priorityTag != "" { @@ -241,7 +241,6 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string if err != nil { return err } - var foundKey string for tagName, attributeName := range tagToNameMap { // If there's something else in the tag string, // it uses the first part which is split using char ','. @@ -252,8 +251,13 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string // If tag and attribute values both exist in `paramsMap`, // it then uses the tag value overwriting the attribute value in `paramsMap`. if paramsMap[tagName] != nil { - if foundKey, _ = utils.MapPossibleItemByKey(paramsMap, attributeName); foundKey != "" { - paramsMap[foundKey] = paramsMap[tagName] + for paramsKey, _ := range paramsMap { + if paramsKey == tagName { + continue + } + if utils.EqualFoldWithoutChars(paramsKey, attributeName) { + paramsMap[paramsKey] = paramsMap[tagName] + } } } } diff --git a/util/gconv/gconv_z_unit_struct_test.go b/util/gconv/gconv_z_unit_struct_test.go index 50b4a17f2..a224b746a 100644 --- a/util/gconv/gconv_z_unit_struct_test.go +++ b/util/gconv/gconv_z_unit_struct_test.go @@ -1271,6 +1271,7 @@ func Test_Struct_Issue1563(t *testing.T) { params2 := g.Map{ "password1": "111", "PASS1": "222", + "Pass1": "333", } if err := gconv.Struct(params2, user); err == nil { t.Assert(user.Pass1, `111`)