@@ -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 }
0 commit comments