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

Added feature to map url params to a struct with the default binder #1165

Merged
merged 8 commits into from
Jun 21, 2019

Conversation

kolaente
Copy link
Contributor

Resolves #1162.

@codecov
Copy link

codecov bot commented Jul 24, 2018

Codecov Report

Merging #1165 into master will decrease coverage by 2.62%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1165      +/-   ##
==========================================
- Coverage   84.28%   81.66%   -2.63%     
==========================================
  Files          27       26       -1     
  Lines        2049     1936     -113     
==========================================
- Hits         1727     1581     -146     
- Misses        209      247      +38     
+ Partials      113      108       -5
Impacted Files Coverage Δ
bind.go 85.87% <100%> (-4.99%) ⬇️
context.go 75.22% <0%> (-15.49%) ⬇️
middleware/compress.go 68.18% <0%> (-9.1%) ⬇️
group.go 93.75% <0%> (-6.25%) ⬇️
response.go 65.78% <0%> (-5.27%) ⬇️
middleware/body_dump.go 82.85% <0%> (-5.03%) ⬇️
router.go 93.3% <0%> (-2.03%) ⬇️
middleware/cors.go 75% <0%> (-1.37%) ⬇️
middleware/secure.go 92% <0%> (-1.34%) ⬇️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3136157...3b0700f. Read the comment docs.

bind.go Outdated
paramValues := c.ParamValues()
paramVars := make(map[string][]string)
for in, name := range paramNames {
// Fix for an echo bug where a param name would show up which dont exist if its name contains "id" e.g. "userid"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kolaente What is it about?

Copy link
Contributor Author

@kolaente kolaente Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the param name is defined as somethingid, echo sometimes puts this as id,somethingid (one string with a comma) into c.ParamNames(). I've tried to track this down and fix it, but I wasn't successful which is why I put that workaround here. The bug seems to occur pretty randomly. Or is that a feature?

I know it's probably not the best place to do this here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide steps to reproduces?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've registered a route to /thing/:somethingid and printed out ParamNames in the route handler after calling the default binder (before my modifications).

I wasn't able to reproduce this, with some tests right now, if you were able to, this should probably be fixed in a seperate pr before this one. If not, I'd better remove my workaround as in that case it probably was an issue with my setup.

bind.go Outdated Show resolved Hide resolved
bind.go Outdated Show resolved Hide resolved
bind.go Outdated
params := make(map[string][]string)
for i, name := range paramNames {
// Fix for an echo bug where a param name would show up which dont exist if its name contains "id" e.g. "userid"
names := strings.Split(name, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	// BindUnmarshaler is the interface used to wrap the UnmarshalParam method.
	BindUnmarshaler interface {
		// UnmarshalParam decodes and assigns a value from an form or query param.
		UnmarshalParam(param string) error
	}

and example:

type StringArray []string

func (a *StringArray) UnmarshalParam(src string) error {
	*a = StringArray(strings.Split(src, ","))
	return nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean I should implement the bind param method via UnmarshalParam?
Or do you mean I should run the map of mapped params through UnmarshalParam to fix that bug I described?

bind.go Outdated
paramValues := c.ParamValues()
params := make(map[string][]string)
for i, name := range paramNames {
// Fix for an echo bug where a param name would show up which dont exist if its name contains "id" e.g. "userid"
Copy link
Contributor

@im-kulikov im-kulikov Aug 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  // id-named fields
  type testUser struct {
		ID      int `param:"userid"`
		NameID  int `param:"nameid"`
		GroupID int `param:"groupid"`
		SameID  int `param:"sameid"`
	}

...

	t.Run("should be ok for id-named fields", func(t *testing.T) {
		req := httptest.NewRequest(echo.GET, "/1/2/3/4", nil)
		testHandler := func(ctx echo.Context) error {
			u := new(testUser)
			err := ctx.Bind(u)
			if assert.NoError(t, err) {
				assert.Equal(t, 1, u.ID)
				assert.Equal(t, 2, u.NameID)
				assert.Equal(t, 3, u.GroupID)
				assert.Equal(t, 4, u.SameID)
			}

			return nil
		}
		e.GET("/:userid/:nameid/:groupid/:sameid", testHandler)
		e.ServeHTTP(rec, req)
	})

can't reproduce this bug

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test with the pr in place? Or without the dirty hack I put in to prevent it...

@stale
Copy link

stale bot commented Nov 30, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 30, 2018
@stale stale bot removed the wontfix label Nov 30, 2018
@stale
Copy link

stale bot commented Mar 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 28, 2019
@kolaente
Copy link
Contributor Author

kolaente commented Apr 1, 2019

@vishr @im-kulikov So, should I remove my workaround? Since its not reproducable, I could just remove it and look a bit more in my application where the bug comes from.

@stale stale bot removed the wontfix label Apr 1, 2019
@im-kulikov
Copy link
Contributor

@vishr any thoughts?

@kolaente
Copy link
Contributor Author

So, what else needs to be done to get this merged? What can I do about this? @vishr

@vishr vishr added the v5 label Apr 19, 2019
@stale
Copy link

stale bot commented Jun 18, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 18, 2019
@kolaente
Copy link
Contributor Author

kolaente commented Jun 18, 2019

@vishr anything else that needs to be done here to get this merged?

@stale stale bot removed the wontfix label Jun 18, 2019
@vishr
Copy link
Member

vishr commented Jun 18, 2019

@kolaente I will look into this today. We want this feature and I marked it for v5 but could be sooner.

@vishr
Copy link
Member

vishr commented Jun 18, 2019

If the bug isn't reproducible, remove the fix.

@vishr vishr merged commit 858270f into labstack:master Jun 21, 2019
@kolaente
Copy link
Contributor Author

Oh, thanks! I just wanted to fix that failing test first...

@vishr
Copy link
Member

vishr commented Jun 21, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[proposal] Map URL params to struct
3 participants