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

DefaultBinder.Bind() binds path/query params to struct fields without TAG #1729

Closed
aldas opened this issue Dec 20, 2020 · 0 comments
Closed

Comments

@aldas
Copy link
Contributor

aldas commented Dec 20, 2020

This is continuation of #1681 discussions to 'fix' problems with Bind

  1. change struct binding work only when tag exists (do not use field name as backup) + update docs

Originally posted by @aldas in #1681 (comment)

So I started to work with do not bind to field without tag requirement and I'll document some findings here that will be changed with this requirement.

Example with current behaviour (without modifications)

func TestToMultipleFields(t *testing.T) {
	e := New()
	req := httptest.NewRequest(http.MethodGet, "/?id=1&ID=2", nil)
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)

	type Root struct {
		ID int64 `query:"id"`
		Child2 struct{
			ID int64
		}
		Child1 struct{
			ID int64 `query:"id"`
		}
	}

	u := new(Root)
	err := c.Bind(u)
	if assert.NoError(t, err) {
		assert.Equal(t, int64(1), u.ID) // perfectly reasonable
		assert.Equal(t, int64(1), u.Child1.ID) // untagged struct containing tagged field gets filled (by tag)
		assert.Equal(t, int64(2), u.Child2.ID) // untagged struct containing untagged field gets filled (by matching name)
	}
}
  1. One query param can be binded to multiple fields.
    • fine so far - will be possible in future
  2. If bind destination is struct contaning struct fields then matching query params will be bind for each struct in hierarchy. Those inner structs do not need tags atm.
    • after this change u.Child1.ID will be bind but u.Child2.ID will not as later does not have a tag for id
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

1 participant