gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.79k stars 1.61k forks source link

util/gconv: Fields with the same name in nested structures overwrite values #3797

Closed wln32 closed 2 months ago

wln32 commented 2 months ago

Go version

1.22

GoFrame version

2.7.3

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

type Rule struct {
    ID int64 `json:"id"`
    Rule []*option.Rule `json:"rule"`
}

type Res1 struct {
    g.Meta
    Rule
}

var err error
var r = &Rule{
    ID: 100,
}
var res = &Res1{}
if err = gconv.Scan(r, res); err != nil {
    fmt.Println("err=", err)
    return
}
for {
    if res.ID == 0 {
        fmt.Println("res.id")
        g.Dump(res)
        fmt.Printf("%+v\n", res)
        time.Sleep(time.Second)
    }
}

What did you see happen?

res.id
{
    ID:   0,
    Rule: {
        ID:   0,
        Rule: [],
    },
}
&{Meta:{} Rule:{ID:0 Rule:[]}}

What did you expect to see?

什么都不输出,赋值正确即可

wln32 commented 2 months ago

gconv先把struct转成map image

在循环赋值时,如果先拿到的key是id的话,正常赋值成功没有什么问题 image

第二次是rule时 image

会走empty.IsNil分支,从而把原值100覆盖掉了 image

因为Rule结构体和里面的Rule字段重名导致被覆盖

Issues-translate-bot commented 2 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


gconv first converts struct into map image

During the loop assignment, if the key obtained first is the id, there will be no problem with the normal assignment succeeding. image

The second time is when rule image

Will take the empty.IsNil branch, thereby overwriting the original value of 100 image