1
0
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:
Vishal Rana 2016-12-10 08:38:27 -08:00
parent 20954afd66
commit 0e7a9c1d49
4 changed files with 16 additions and 14 deletions

View File

@ -17,13 +17,15 @@ type (
Bind(i interface{}, c Context) error 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() req := c.Request()
if req.Method == GET { 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 NewHTTPError(http.StatusBadRequest, err.Error())
} }
return return
@ -58,7 +60,7 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
if err != nil { if err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error()) 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()) return NewHTTPError(http.StatusBadRequest, err.Error())
} }
default: default:
@ -67,7 +69,7 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
return 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() typ := reflect.TypeOf(ptr).Elem()
val := reflect.ValueOf(ptr).Elem() val := reflect.ValueOf(ptr).Elem()
@ -82,13 +84,13 @@ func (b *binder) bindData(ptr interface{}, data map[string][]string) error {
continue continue
} }
structFieldKind := structField.Kind() structFieldKind := structField.Kind()
inputFieldName := typeField.Tag.Get("form") inputFieldName := typeField.Tag.Get(tag)
if inputFieldName == "" { if inputFieldName == "" {
inputFieldName = typeField.Name 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 { if structFieldKind == reflect.Struct {
err := b.bindData(structField.Addr().Interface(), data) err := b.bindData(structField.Addr().Interface(), data, tag)
if err != nil { if err != nil {
return err return err
} }

View File

@ -105,10 +105,10 @@ func TestBinderUnsupportedMediaType(t *testing.T) {
testBinderError(t, strings.NewReader(invalidContent), MIMEApplicationJSON) testBinderError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
} }
func TestBinderbindForm(t *testing.T) { func TestBinderbindData(t *testing.T) {
ts := new(binderTestStruct) ts := new(binderTestStruct)
b := new(binder) b := new(DefaultBinder)
b.bindData(ts, values) b.bindData(ts, values, "form")
assertBinderTestStruct(t, ts) assertBinderTestStruct(t, ts)
} }

View File

@ -245,7 +245,7 @@ func New() (e *Echo) {
Color: color.New(), Color: color.New(),
} }
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
e.Binder = &binder{} e.Binder = &DefaultBinder{}
e.Logger.SetLevel(glog.OFF) e.Logger.SetLevel(glog.OFF)
e.pool.New = func() interface{} { e.pool.New = func() interface{} {
return e.NewContext(nil, nil) return e.NewContext(nil, nil)

View File

@ -17,8 +17,8 @@ import (
type ( type (
user struct { user struct {
ID int `json:"id" xml:"id" form:"id"` ID int `json:"id" xml:"id" form:"id" query:"id"`
Name string `json:"name" xml:"name" form:"name"` Name string `json:"name" xml:"name" form:"name" query:"name"`
} }
) )