mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Request: New bind property for route parameters (#973)
* bind route params * rename to bindPathData
This commit is contained in:
parent
0769b34b52
commit
f8c3008787
16
bind.go
16
bind.go
@ -39,6 +39,9 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
|||||||
}
|
}
|
||||||
return NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
|
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)
|
ctype := req.Header.Get(HeaderContentType)
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
case strings.HasPrefix(ctype, MIMEApplicationJSON):
|
||||||
@ -75,6 +78,19 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
|||||||
return
|
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 {
|
func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error {
|
||||||
typ := reflect.TypeOf(ptr).Elem()
|
typ := reflect.TypeOf(ptr).Elem()
|
||||||
val := reflect.ValueOf(ptr).Elem()
|
val := reflect.ValueOf(ptr).Elem()
|
||||||
|
17
bind_test.go
17
bind_test.go
@ -140,6 +140,23 @@ func TestBindForm(t *testing.T) {
|
|||||||
assert.Error(t, err)
|
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) {
|
func TestBindQueryParams(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil)
|
req := httptest.NewRequest(GET, "/?id=1&name=Jon+Snow", nil)
|
||||||
|
@ -18,13 +18,14 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
user struct {
|
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"`
|
Name string `json:"name" xml:"name" form:"name" query:"name"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
userJSON = `{"id":1,"name":"Jon Snow"}`
|
userJSON = `{"id":1,"name":"Jon Snow"}`
|
||||||
|
userJSONOnlyName = `{"name":"Jon Snow"}`
|
||||||
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
|
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
|
||||||
userForm = `id=1&name=Jon Snow`
|
userForm = `id=1&name=Jon Snow`
|
||||||
invalidContent = "invalid content"
|
invalidContent = "invalid content"
|
||||||
|
Loading…
Reference in New Issue
Block a user