Skip to content

Commit

Permalink
exposed default binder, tag for binding query params
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Dec 10, 2016
1 parent 20954af commit 0e7a9c1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
18 changes: 10 additions & 8 deletions binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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()

Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
)

Expand Down

0 comments on commit 0e7a9c1

Please sign in to comment.