From 0e7a9c1d497b84d79fca0d201638f393942e44a4 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sat, 10 Dec 2016 08:38:27 -0800 Subject: [PATCH] exposed default binder, tag for binding query params Signed-off-by: Vishal Rana --- binder.go | 18 ++++++++++-------- binder_test.go | 6 +++--- echo.go | 2 +- echo_test.go | 4 ++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/binder.go b/binder.go index 7b213b9f9..056aacb5f 100644 --- a/binder.go +++ b/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 } diff --git a/binder_test.go b/binder_test.go index 662782be0..e1bffbbe7 100644 --- a/binder_test.go +++ b/binder_test.go @@ -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) } diff --git a/echo.go b/echo.go index 1d7dbc966..adf431750 100644 --- a/echo.go +++ b/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) diff --git a/echo_test.go b/echo_test.go index 88a1deb63..775d5e92a 100644 --- a/echo_test.go +++ b/echo_test.go @@ -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"` } )