1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

fix(util/gconv): incorrect fuzzy converting logic (#3874)

This commit is contained in:
wln32 2024-10-21 09:16:50 +08:00 committed by GitHub
parent c18339b1a9
commit b1d875a31f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 26 deletions

View File

@ -257,7 +257,6 @@ func bindStructWithLoopParamsMap(
var (
fieldName string
cachedFieldInfo *structcache.CachedFieldInfo
fuzzLastKey string
fieldValue reflect.Value
paramKey string
paramValue any
@ -298,37 +297,29 @@ func bindStructWithLoopParamsMap(
if _, ok = usedParamsKeyOrTagNameMap[fieldName]; ok {
continue
}
fuzzLastKey = cachedFieldInfo.LastFuzzyKey.Load().(string)
paramValue, ok = paramsMap[fuzzLastKey]
if !ok {
if strings.EqualFold(
cachedFieldInfo.RemoveSymbolsFieldName, utils.RemoveSymbols(paramKey),
) {
paramValue, ok = paramsMap[paramKey]
// If it is found this time, update it based on what was not found last time.
cachedFieldInfo.LastFuzzyKey.Store(paramKey)
}
if !strings.EqualFold(
cachedFieldInfo.RemoveSymbolsFieldName,
utils.RemoveSymbols(paramKey)) {
continue
}
if ok {
fieldValue = cachedFieldInfo.GetFieldReflectValueFrom(structValue)
if paramValue != nil {
if err = bindVarToStructField(
fieldValue, paramValue, cachedFieldInfo, paramKeyToAttrMap,
fieldValue = cachedFieldInfo.GetFieldReflectValueFrom(structValue)
if paramValue != nil {
if err = bindVarToStructField(
fieldValue, paramValue, cachedFieldInfo, paramKeyToAttrMap,
); err != nil {
return err
}
// handle same field name in nested struct.
if len(cachedFieldInfo.OtherSameNameField) > 0 {
if err = setOtherSameNameField(
cachedFieldInfo, paramValue, structValue, paramKeyToAttrMap,
); err != nil {
return err
}
// handle same field name in nested struct.
if len(cachedFieldInfo.OtherSameNameField) > 0 {
if err = setOtherSameNameField(
cachedFieldInfo, paramValue, structValue, paramKeyToAttrMap,
); err != nil {
return err
}
}
}
usedParamsKeyOrTagNameMap[cachedFieldInfo.FieldName()] = struct{}{}
break
}
usedParamsKeyOrTagNameMap[cachedFieldInfo.FieldName()] = struct{}{}
break
}
}
return nil

View File

@ -757,3 +757,29 @@ func Test_Issue3821(t *testing.T) {
t.AssertEQ(user.DoubleInnerUser.UserId, int64(1))
})
}
// https://github.com/gogf/gf/issues/3868
func Test_Issue3868(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Config struct {
Enable bool
Spec string
PoolSize int
}
data := gjson.New(`[{"enable":false,"spec":"a"},{"enable":true,"poolSize":1}]`)
for i := 0; i < 1000; i++ {
var configs []*Config
err := gconv.Structs(data, &configs)
t.AssertNil(err)
t.Assert(len(configs), 2)
t.Assert(configs[0], &Config{
Enable: false,
Spec: "a",
})
t.Assert(configs[1], &Config{
Enable: true,
PoolSize: 1,
})
}
})
}