mirror of
https://github.com/labstack/echo.git
synced 2025-11-25 22:32:23 +02:00
8
bind.go
8
bind.go
@@ -42,6 +42,9 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
|||||||
if err = b.bindPathData(i, c); err != nil {
|
if err = b.bindPathData(i, c); err != nil {
|
||||||
return NewHTTPError(http.StatusBadRequest, err.Error())
|
return NewHTTPError(http.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
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):
|
||||||
@@ -79,7 +82,10 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *DefaultBinder) bindPathData(ptr interface{}, c Context) error {
|
func (b *DefaultBinder) bindPathData(ptr interface{}, c Context) error {
|
||||||
m := make(map[string][]string)
|
if reflect.TypeOf(ptr).Elem().Kind() == reflect.Slice {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m := make(map[string][]string, len(c.ParamNames()))
|
||||||
for _, key := range c.ParamNames() {
|
for _, key := range c.ParamNames() {
|
||||||
m[key] = []string{c.Param(key)}
|
m[key] = []string{c.Param(key)}
|
||||||
}
|
}
|
||||||
|
|||||||
17
bind_test.go
17
bind_test.go
@@ -118,6 +118,7 @@ var values = map[string][]string{
|
|||||||
func TestBindJSON(t *testing.T) {
|
func TestBindJSON(t *testing.T) {
|
||||||
testBindOkay(t, strings.NewReader(userJSON), MIMEApplicationJSON)
|
testBindOkay(t, strings.NewReader(userJSON), MIMEApplicationJSON)
|
||||||
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
|
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
|
||||||
|
testBindSlice(t, strings.NewReader(userJSONArray), MIMEApplicationJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindXML(t *testing.T) {
|
func TestBindXML(t *testing.T) {
|
||||||
@@ -349,3 +350,19 @@ func testBindError(t *testing.T, r io.Reader, ctype string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBindSlice(t *testing.T, r io.Reader, ctype string) {
|
||||||
|
e := New()
|
||||||
|
req := httptest.NewRequest(POST, "/", r)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
req.Header.Set(HeaderContentType, ctype)
|
||||||
|
us := []user{}
|
||||||
|
err := c.Bind(&us)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, 1, us[0].ID)
|
||||||
|
assert.Equal(t, "Jon Snow", us[0].Name)
|
||||||
|
assert.Equal(t, 2, us[1].ID)
|
||||||
|
assert.Equal(t, "Arya Stark", us[1].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ type (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
userJSON = `{"id":1,"name":"Jon Snow"}`
|
userJSON = `{"id":1,"name":"Jon Snow"}`
|
||||||
|
userJSONArray = `[{"id":1,"name":"Jon Snow"},{"id":2,"name":"Arya Stark"}]`
|
||||||
userJSONOnlyName = `{"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`
|
||||||
|
|||||||
Reference in New Issue
Block a user