Skip to content

Request: New bind property for route parameters #973

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

Merged
merged 2 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
}
return NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
}
if err = b.bindPathData(i, c); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error())
}
ctype := req.Header.Get(HeaderContentType)
switch {
case strings.HasPrefix(ctype, MIMEApplicationJSON):
Expand Down Expand Up @@ -75,6 +78,19 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
return
}

func (b *DefaultBinder) bindPathData(ptr interface{}, c Context) error {
m := make(map[string][]string)
for _, key := range c.ParamNames() {
m[key] = []string{c.Param(key)}
}
if len(m) >= 0 {
if err := b.bindData(ptr, m, "param"); err != nil {
return err
}
}
return nil
}

func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error {
typ := reflect.TypeOf(ptr).Elem()
val := reflect.ValueOf(ptr).Elem()
Expand Down
17 changes: 17 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ func TestBindForm(t *testing.T) {
assert.Error(t, err)
}

func TestBindRouteParam(t *testing.T) {
e := New()
r := strings.NewReader(userJSONOnlyName)
req := httptest.NewRequest(POST, "/", r)
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues("5")
u := new(user)
err := c.Bind(u)
if assert.NoError(t, err) {
assert.Equal(t, 5, u.ID)
assert.Equal(t, "Jon Snow", u.Name)
}
}

func TestBindQueryParams(t *testing.T) {
e := New()
req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil)
Expand Down
11 changes: 6 additions & 5 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (

type (
user struct {
ID int `json:"id" xml:"id" form:"id" query:"id"`
ID int `json:"id" xml:"id" form:"id" query:"id" param:"id"`
Name string `json:"name" xml:"name" form:"name" query:"name"`
}
)

const (
userJSON = `{"id":1,"name":"Jon Snow"}`
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
userForm = `id=1&name=Jon Snow`
invalidContent = "invalid content"
userJSON = `{"id":1,"name":"Jon Snow"}`
userJSONOnlyName = `{"name":"Jon Snow"}`
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
userForm = `id=1&name=Jon Snow`
invalidContent = "invalid content"
)

const userJSONPretty = `{
Expand Down