2021-08-12 20:03:24 +01:00
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/textproto"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type headerBinding struct{}
|
|
|
|
|
|
|
|
func (headerBinding) Name() string {
|
|
|
|
return "header"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (headerBinding) Bind(req *http.Request, obj interface{}) error {
|
|
|
|
|
|
|
|
if err := mapHeader(obj, req.Header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return validate(obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapHeader(ptr interface{}, h map[string][]string) error {
|
|
|
|
return mappingByPtr(ptr, headerSource(h), "header")
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerSource map[string][]string
|
|
|
|
|
|
|
|
var _ setter = headerSource(nil)
|
|
|
|
|
2021-11-27 14:26:58 +00:00
|
|
|
func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) {
|
2021-08-12 20:03:24 +01:00
|
|
|
return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt)
|
|
|
|
}
|