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/gutil: gconv struct to map bug, omitempty takes no effect #3721

Closed
nogolang opened this issue Aug 8, 2024 · 8 comments
Closed

util/gutil: gconv struct to map bug, omitempty takes no effect #3721

nogolang opened this issue Aug 8, 2024 · 8 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@nogolang
Copy link

nogolang commented Aug 8, 2024

这是官方的案例
https://goframe.org/pages/viewpage.action?pageId=1114356

下面案例中,理论上nickname不应该出现在输出结果中
同样的案例,使用mapstructure库测试是没有问题的

package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	type User struct {
		Uid      int
		Name     string `c:"-"`
		NickName string `c:"nickname, omitempty"`
		Pass1    string `c:"password1"`
		Pass2    string `c:"password2"`
	}
	user := User{
		Uid:   100,
		Name:  "john",
		Pass1: "123",
		Pass2: "456",
	}
	g.Dump(gconv.Map(user))
}

输出

{
    "Uid": 100,
    "password1": "123",
    "password2": "456",
    "nickname": "",
}
@Issues-translate-bot Issues-translate-bot changed the title struct to map中使用了omitempty,当值为0值得时候,为什么输出结果依旧存在字段? struct to map. When the value is 0, why does the output result still have fields? Aug 8, 2024
@nogolang nogolang changed the title struct to map. When the value is 0, why does the output result still have fields? gconv struct to map bug,omitempty标签无效 Aug 8, 2024
@wsy9981999
Copy link

这个时候还是用json标签把

@Issues-translate-bot
Copy link

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


At this time it is better to use json tags

@nogolang
Copy link
Author

nogolang commented Aug 9, 2024

这个时候还是用json标签把

这个和json没有关系,之所以结果是json是因为使用了g.Dump把map转换为了json。
问题的本质是map本身不应该含有nickname字段,在mapstructure库测试是没有问题的

@Issues-translate-bot
Copy link

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


At this time, it is better to use json tags

This has nothing to do with json. The reason why the result is json is because g.Dump is used to convert map to json.
The essence of the problem is that the map itself should not contain the nickname field. There is no problem when testing in the mapstructure library.

@gqcn gqcn changed the title gconv struct to map bug,omitempty标签无效 util/gutil: gconv struct to map bug, omitempty take no effect Sep 12, 2024
@gqcn gqcn changed the title util/gutil: gconv struct to map bug, omitempty take no effect util/gutil: gconv struct to map bug, omitempty takes no effect Sep 12, 2024
@github-actions github-actions bot removed the inactive label Sep 12, 2024
@gqcn gqcn added bug It is confirmed a bug, but don't worry, we'll handle it. and removed question labels Sep 12, 2024
@wwwfeng
Copy link
Contributor

wwwfeng commented Sep 14, 2024

我看了一下这个gconv.Map 是有option参数的,比如:

			g.Dump(gconv.Map(user, gconv.MapOption{
						OmitEmpty: true,
					}))

设置参数以后输出的结果就是下面的样子,我不知道这是不是属于特性

{
    "password2": "456",
    "Uid":       100,
    "password1": "123",
}

@Issues-translate-bot
Copy link

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


I took a look and this gconv.Map has option parameters, such as:

g.Dump(gconv.Map(user, gconv.MapOption{
OmitEmpty: true,
}))

After setting the parameters, the output result is as follows. I don’t know if this is a feature.

{
    "password2": "456",
    "Uid": 100,
    "password1": "123",
}

@nogolang
Copy link
Author

看样子必须使用option选项才行

我看了一下这个gconv.Map 是有option参数的,比如:

			g.Dump(gconv.Map(user, gconv.MapOption{
						OmitEmpty: true,
					}))

设置参数以后输出的结果就是下面的样子,我不知道这是不是属于特性

{
    "password2": "456",
    "Uid":       100,
    "password1": "123",
}

@Issues-translate-bot
Copy link

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


It seems that you must use the option option.

I took a look and this gconv.Map has option parameters, such as:

g.Dump(gconv.Map(user, gconv.MapOption{
OmitEmpty: true,
}))

After setting the parameters, the output result is as follows. I don’t know if this is a feature.

{
"password2": "456",
"Uid": 100,
"password1": "123",
}

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
Development

No branches or pull requests

5 participants