Skip to content

Commit

Permalink
fix(util/gconv): incorrect fuzzy converting logic (#3874)
Browse files Browse the repository at this point in the history
  • Loading branch information
wln32 authored Oct 21, 2024
1 parent c18339b commit b1d875a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
43 changes: 17 additions & 26 deletions util/gconv/gconv_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ func bindStructWithLoopParamsMap(
var (
fieldName string
cachedFieldInfo *structcache.CachedFieldInfo
fuzzLastKey string
fieldValue reflect.Value
paramKey string
paramValue any
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions util/gconv/gconv_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
})
}

0 comments on commit b1d875a

Please sign in to comment.