Skip to content

Commit 0e7a9c1

Browse files
committed
exposed default binder, tag for binding query params
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 20954af commit 0e7a9c1

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

binder.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ type (
1717
Bind(i interface{}, c Context) error
1818
}
1919

20-
binder struct{}
20+
// DefaultBinder is the default implementation of the Binder interface.
21+
DefaultBinder struct{}
2122
)
2223

23-
func (b *binder) Bind(i interface{}, c Context) (err error) {
24+
// Bind implements the `Binder#Bind` function.
25+
func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
2426
req := c.Request()
2527
if req.Method == GET {
26-
if err = b.bindData(i, c.QueryParams()); err != nil {
28+
if err = b.bindData(i, c.QueryParams(), "query"); err != nil {
2729
return NewHTTPError(http.StatusBadRequest, err.Error())
2830
}
2931
return
@@ -58,7 +60,7 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
5860
if err != nil {
5961
return NewHTTPError(http.StatusBadRequest, err.Error())
6062
}
61-
if err = b.bindData(i, params); err != nil {
63+
if err = b.bindData(i, params, "form"); err != nil {
6264
return NewHTTPError(http.StatusBadRequest, err.Error())
6365
}
6466
default:
@@ -67,7 +69,7 @@ func (b *binder) Bind(i interface{}, c Context) (err error) {
6769
return
6870
}
6971

70-
func (b *binder) bindData(ptr interface{}, data map[string][]string) error {
72+
func (b *DefaultBinder) bindData(ptr interface{}, data map[string][]string, tag string) error {
7173
typ := reflect.TypeOf(ptr).Elem()
7274
val := reflect.ValueOf(ptr).Elem()
7375

@@ -82,13 +84,13 @@ func (b *binder) bindData(ptr interface{}, data map[string][]string) error {
8284
continue
8385
}
8486
structFieldKind := structField.Kind()
85-
inputFieldName := typeField.Tag.Get("form")
87+
inputFieldName := typeField.Tag.Get(tag)
8688

8789
if inputFieldName == "" {
8890
inputFieldName = typeField.Name
89-
// If "form" tag is nil, we inspect if the field is a struct.
91+
// If tag is nil, we inspect if the field is a struct.
9092
if structFieldKind == reflect.Struct {
91-
err := b.bindData(structField.Addr().Interface(), data)
93+
err := b.bindData(structField.Addr().Interface(), data, tag)
9294
if err != nil {
9395
return err
9496
}

binder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func TestBinderUnsupportedMediaType(t *testing.T) {
105105
testBinderError(t, strings.NewReader(invalidContent), MIMEApplicationJSON)
106106
}
107107

108-
func TestBinderbindForm(t *testing.T) {
108+
func TestBinderbindData(t *testing.T) {
109109
ts := new(binderTestStruct)
110-
b := new(binder)
111-
b.bindData(ts, values)
110+
b := new(DefaultBinder)
111+
b.bindData(ts, values, "form")
112112
assertBinderTestStruct(t, ts)
113113
}
114114

echo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func New() (e *Echo) {
245245
Color: color.New(),
246246
}
247247
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
248-
e.Binder = &binder{}
248+
e.Binder = &DefaultBinder{}
249249
e.Logger.SetLevel(glog.OFF)
250250
e.pool.New = func() interface{} {
251251
return e.NewContext(nil, nil)

echo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717

1818
type (
1919
user struct {
20-
ID int `json:"id" xml:"id" form:"id"`
21-
Name string `json:"name" xml:"name" form:"name"`
20+
ID int `json:"id" xml:"id" form:"id" query:"id"`
21+
Name string `json:"name" xml:"name" form:"name" query:"name"`
2222
}
2323
)
2424

0 commit comments

Comments
 (0)