1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00
This commit is contained in:
John Guo 2022-02-16 00:47:23 +08:00
parent 5ab959ba58
commit a5cc03ff25
4 changed files with 36 additions and 7 deletions

View File

@ -24,3 +24,14 @@ func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey str
}
return "", nil
}
// MapContainsPossibleKey checks if the given `key` is contained in given map `data`.
// It checks the key ignoring cases and symbols.
//
// Note that this function might be of low performance.
func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
if k, _ := MapPossibleItemByKey(data, key); k != "" {
return true
}
return false
}

View File

@ -241,6 +241,7 @@ 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 ','.
@ -248,11 +249,12 @@ func doStruct(params interface{}, pointer interface{}, mapping map[string]string
// orm:"id, priority"
// orm:"name, with:uid=id"
tagMap[attributeName] = utils.RemoveSymbols(strings.Split(tagName, ",")[0])
// 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 && paramsMap[attributeName] != nil {
paramsMap[attributeName] = paramsMap[tagName]
if paramsMap[tagName] != nil {
if foundKey, _ = utils.MapPossibleItemByKey(paramsMap, attributeName); foundKey != "" {
paramsMap[foundKey] = paramsMap[tagName]
}
}
}

View File

@ -1258,3 +1258,22 @@ func Test_Struct_Empty_MapStringString(t *testing.T) {
t.AssertNil(err)
})
}
// https://github.com/gogf/gf/issues/1563
func Test_Struct_Issue1563(t *testing.T) {
type User struct {
Pass1 string `c:"password1"`
}
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
user := new(User)
params2 := g.Map{
"password1": "111",
"PASS1": "222",
}
if err := gconv.Struct(params2, user); err == nil {
t.Assert(user.Pass1, `111`)
}
}
})
}

View File

@ -76,10 +76,7 @@ func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey str
//
// Note that this function might be of low performance.
func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
if k, _ := MapPossibleItemByKey(data, key); k != "" {
return true
}
return false
return utils.MapContainsPossibleKey(data, key)
}
// MapOmitEmpty deletes all empty values from given map.