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

Cause bug when have struct method #30

Closed
j796160836 opened this issue Jun 14, 2021 · 1 comment
Closed

Cause bug when have struct method #30

j796160836 opened this issue Jun 14, 2021 · 1 comment

Comments

@j796160836
Copy link

Hi, I found a bug when a structure having method.
For the example,

Find the User struct in sample code

type User struct {
	Username string   `json:"username" groups:"api"`
	Email    string   `json:"email"`
	Name     string   `json:"name" groups:"api"`
	Roles    []string `json:"roles" groups:"api"`
}

Add the following code below,

func (u User) String() string {
	return fmt.Sprintf("User<%s %s>", u.Username, u.Name)
}

It will brick the scoping, marshal all the stuff into json.

@mweibel
Copy link
Collaborator

mweibel commented Mar 6, 2024

type User struct {
	Username string   `json:"username" groups:"api"`
	Email    string   `json:"email"`
	Name     string   `json:"name" groups:"api"`
	Roles    []string `json:"roles" groups:"api"`
}

func (u User) String() string {
	return fmt.Sprintf("User<%s %s>", u.Username, u.Name)
}

func TestMarshal_User(t *testing.T) {
	u := &User{
		Username: "test",
		Email:    "[email protected]",
		Name:     "tester",
		Roles:    []string{"admin"},
	}
	v, err := Marshal(&Options{Groups: []string{"api"}}, u)
	assert.NoError(t, err)
	assert.Equal(t, map[string]interface{}{"name":"tester", "roles":[]interface {}{"admin"}, "username":"test"}, v)
}

This looks correct to me. Please clarify what the issue is or provide a PR. Until then, I close the issue.

@mweibel mweibel closed this as completed Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants