mirror of
https://github.com/labstack/echo.git
synced 2024-11-30 08:46:41 +02:00
parent
858270f6f5
commit
8fb7b5be27
13
bind.go
13
bind.go
@ -34,11 +34,11 @@ type (
|
|||||||
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
|
|
||||||
paramNames := c.ParamNames()
|
names := c.ParamNames()
|
||||||
paramValues := c.ParamValues()
|
values := c.ParamValues()
|
||||||
params := make(map[string][]string)
|
params := map[string][]string{}
|
||||||
for i, name := range paramNames {
|
for i, name := range names {
|
||||||
params[name] = []string{paramValues[i]}
|
params[name] = []string{values[i]}
|
||||||
}
|
}
|
||||||
if err := b.bindData(i, params, "param"); err != nil {
|
if err := b.bindData(i, params, "param"); err != nil {
|
||||||
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
|
||||||
@ -88,6 +88,9 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
typ := reflect.TypeOf(ptr).Elem()
|
typ := reflect.TypeOf(ptr).Elem()
|
||||||
val := reflect.ValueOf(ptr).Elem()
|
val := reflect.ValueOf(ptr).Elem()
|
||||||
|
|
||||||
|
@ -788,15 +788,10 @@ func TestContext_Bind(t *testing.T) {
|
|||||||
e := New()
|
e := New()
|
||||||
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON))
|
||||||
c := e.NewContext(req, nil)
|
c := e.NewContext(req, nil)
|
||||||
|
u := new(user)
|
||||||
var u *user
|
|
||||||
|
|
||||||
err := c.Bind(u)
|
|
||||||
testify.Error(t, err)
|
|
||||||
testify.Nil(t, u)
|
|
||||||
|
|
||||||
req.Header.Add(HeaderContentType, MIMEApplicationJSON)
|
req.Header.Add(HeaderContentType, MIMEApplicationJSON)
|
||||||
err = c.Bind(&u)
|
err := c.Bind(u)
|
||||||
testify.NoError(t, err)
|
testify.NoError(t, err)
|
||||||
testify.Equal(t, &user{1, "Jon Snow"}, u)
|
testify.Equal(t, &user{1, "Jon Snow"}, u)
|
||||||
}
|
}
|
||||||
|
1
go.sum
1
go.sum
@ -1,5 +1,6 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||||
|
Loading…
Reference in New Issue
Block a user