@@ -44,10 +44,10 @@ const (
4444
4545type Request interface {
4646 ContentType () string
47- Header (key string ) string
48- Cookie (name string ) string
49- PathParam (name string ) string
50- QueryParam (name string ) string
47+ Header (key string ) ( string , bool )
48+ Cookie (name string ) ( string , bool )
49+ PathParam (name string ) ( string , bool )
50+ QueryParam (name string ) ( string , bool )
5151 FormParams () (url.Values , error )
5252 MultipartParams (maxMemory int64 ) (* multipart.Form , error )
5353 RequestBody () io.Reader
@@ -70,7 +70,7 @@ var scopeTags = map[BindScope]string{
7070 BindScopeCookie : "cookie" ,
7171}
7272
73- var scopeGetters = map [BindScope ]func (r Request , name string ) string {
73+ var scopeGetters = map [BindScope ]func (r Request , name string ) ( string , bool ) {
7474 BindScopeURI : Request .PathParam ,
7575 BindScopeQuery : Request .QueryParam ,
7676 BindScopeHeader : Request .Header ,
@@ -95,6 +95,11 @@ func RegisterBodyBinder(mime string, binder BodyBinder) {
9595 bodyBinders [mime ] = binder
9696}
9797
98+ // Bind checks the Method and Content-Type to select a binding engine automatically,
99+ // Depending on the "Content-Type" header different bindings are used, for example:
100+ //
101+ // "application/json" --> JSON binding
102+ // "application/xml" --> XML binding
98103func Bind (i interface {}, r Request ) error {
99104 if err := bindScope (i , r ); err != nil {
100105 return fmt .Errorf ("%w: %v" , ErrBinding , err )
@@ -138,8 +143,7 @@ func bindScope(i interface{}, r Request) error {
138143 fv := ev .Field (j )
139144 ft := et .Field (j )
140145 for scope := BindScopeURI ; scope < BindScopeBody ; scope ++ {
141- err := bindScopeField (scope , fv , ft , r )
142- if err != nil {
146+ if err := bindScopeField (scope , fv , ft , r ); err != nil {
143147 return err
144148 }
145149 }
@@ -149,14 +153,11 @@ func bindScope(i interface{}, r Request) error {
149153
150154func bindScopeField (scope BindScope , v reflect.Value , field reflect.StructField , r Request ) error {
151155 if tag , loaded := scopeTags [scope ]; loaded {
152- if name , ok := field .Tag .Lookup (tag ); ok {
153- if name == "-" {
154- return nil // ignore bind
155- }
156- val := scopeGetters [scope ](r , name )
157- err := bindData (v , val )
158- if err != nil {
159- return err
156+ if name , ok := field .Tag .Lookup (tag ); ok && name != "-" {
157+ if val , exists := scopeGetters [scope ](r , name ); exists {
158+ if err := bindData (v , val ); err != nil {
159+ return err
160+ }
160161 }
161162 }
162163 }
0 commit comments