Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wln32 opened this issue Sep 23, 2024 · 2 comments · Fixed by #3799
Closed

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

wln32 opened this issue Sep 23, 2024 · 2 comments · Fixed by #3799
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@wln32
Copy link
Member

wln32 commented Sep 23, 2024

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 wln32 added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 23, 2024
@Issues-translate-bot Issues-translate-bot changed the title util/gconv: 嵌套结构体同名字段覆盖值 util/gconv: Fields with the same name in nested structures overwrite values Sep 23, 2024
@wln32
Copy link
Member Author

wln32 commented Sep 23, 2024

gconv先把struct转成map
image

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

第二次是rule时
image

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

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

@Issues-translate-bot
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
2 participants