header.go (869B)
1 // Copyright 2022 Gin Core Team. All rights reserved. 2 // Use of this source code is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package binding 6 7 import ( 8 "net/http" 9 "net/textproto" 10 "reflect" 11 ) 12 13 type headerBinding struct{} 14 15 func (headerBinding) Name() string { 16 return "header" 17 } 18 19 func (headerBinding) Bind(req *http.Request, obj any) error { 20 21 if err := mapHeader(obj, req.Header); err != nil { 22 return err 23 } 24 25 return validate(obj) 26 } 27 28 func mapHeader(ptr any, h map[string][]string) error { 29 return mappingByPtr(ptr, headerSource(h), "header") 30 } 31 32 type headerSource map[string][]string 33 34 var _ setter = headerSource(nil) 35 36 func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (bool, error) { 37 return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt) 38 }