mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
exposed default binder, tag for binding query params
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
20954afd66
commit
0e7a9c1d49
18
binder.go
18
binder.go
@ -17,13 +17,15 @@ type (
|
||||
Bind(i interface{}, c Context) error
|
||||
}
|
||||
|
||||
binder struct{}
|
||||
// DefaultBinder is the default implementation of the Binder interface.
|
||||
DefaultBinder struct{}
|
||||
)
|
||||
|
||||
func (b *binder) Bind(i interface{}, c Context) (err error) {
|
||||
// Bind implements the `Binder#Bind` function.
|
||||
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
|
||||
req := c.Request()
|
||||
if req.Method == GET {
|
||||
if err = b.bindData(i, c.QueryParams()); err != nil {
|
||||
if err = b.bindData(i, c.QueryParams(), "query"); err != nil {
|
||||
return NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return
|
||||
@ -58,7 +60,7 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
|
||||
if err != nil {
|
||||
return NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
if err = b.bindData(i, params); err != nil {
|
||||
if err = b.bindData(i, params, "form"); err != nil {
|
||||
return NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
default:
|
||||
@ -67,7 +69,7 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (b *binder) bindData(ptr interface{}, data map[string][]string) error {
|
||||
func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error {
|
||||
typ := reflect.TypeOf(ptr).Elem()
|
||||
val := reflect.ValueOf(ptr).Elem()
|
||||
|
||||
@ -82,13 +84,13 @@ func (b *binder) bindData(ptr interface{}, data map[string][]string) error {
|
||||
continue
|
||||
}
|
||||
structFieldKind := structField.Kind()
|
||||
inputFieldName := typeField.Tag.Get("form")
|
||||
inputFieldName := typeField.Tag.Get(tag)
|
||||
|
||||
if inputFieldName == "" {
|
||||
inputFieldName = typeField.Name
|
||||
// If "form" tag is nil, we inspect if the field is a struct.
|
||||
// If tag is nil, we inspect if the field is a struct.
|
||||
if structFieldKind == reflect.Struct {
|
||||
err := b.bindData(structField.Addr().Interface(), data)
|
||||
err := b.bindData(structField.Addr().Interface(), data, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -105,10 +105,10 @@ func TestBinderUnsupportedMediaType(t *testing.T) {
|
||||
testBinderError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
|
||||
}
|
||||
|
||||
func TestBinderbindForm(t *testing.T) {
|
||||
func TestBinderbindData(t *testing.T) {
|
||||
ts := new(binderTestStruct)
|
||||
b := new(binder)
|
||||
b.bindData(ts, values)
|
||||
b := new(DefaultBinder)
|
||||
b.bindData(ts, values, "form")
|
||||
assertBinderTestStruct(t, ts)
|
||||
}
|
||||
|
||||
|
2
echo.go
2
echo.go
@ -245,7 +245,7 @@ func New() (e *Echo) {
|
||||
Color: color.New(),
|
||||
}
|
||||
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
|
||||
e.Binder = &binder{}
|
||||
e.Binder = &DefaultBinder{}
|
||||
e.Logger.SetLevel(glog.OFF)
|
||||
e.pool.New = func() interface{} {
|
||||
return e.NewContext(nil, nil)
|
||||
|
@ -17,8 +17,8 @@ import (
|
||||
|
||||
type (
|
||||
user struct {
|
||||
ID int `json:"id" xml:"id" form:"id"`
|
||||
Name string `json:"name" xml:"name" form:"name"`
|
||||
ID int `json:"id" xml:"id" form:"id" query:"id"`
|
||||
Name string `json:"name" xml:"name" form:"name" query:"name"`
|
||||
}
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user