mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-22 02:43:36 +08:00
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
// Copyright 2022 Gin Core Team. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package binding
|
|
|
|
import (
|
|
"net/http"
|
|
"net/textproto"
|
|
"reflect"
|
|
)
|
|
|
|
type headerBinding struct{}
|
|
|
|
func (headerBinding) Name() string {
|
|
return "header"
|
|
}
|
|
|
|
func (headerBinding) Bind(req *http.Request, obj any) error {
|
|
if err := (headerBinding{}).BindNoValidate(req, obj); err != nil {
|
|
return err
|
|
}
|
|
return validate(obj)
|
|
}
|
|
|
|
func (headerBinding) BindNoValidate(req *http.Request, obj any) error {
|
|
if err := mapHeader(obj, req.Header); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func mapHeader(ptr any, h map[string][]string) error {
|
|
return mappingByPtr(ptr, headerSource(h), "header")
|
|
}
|
|
|
|
type headerSource map[string][]string
|
|
|
|
var _ setter = headerSource(nil)
|
|
|
|
func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (bool, error) {
|
|
return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt)
|
|
}
|