github.com/lingyao2333/mo-zero@v1.4.1/rest/internal/encoding/parser.go (about)

     1  package encoding
     2  
     3  import (
     4  	"net/http"
     5  	"net/textproto"
     6  
     7  	"github.com/lingyao2333/mo-zero/core/mapping"
     8  )
     9  
    10  const headerKey = "header"
    11  
    12  var headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues(),
    13  	mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
    14  
    15  // ParseHeaders parses the headers request.
    16  func ParseHeaders(header http.Header, v interface{}) error {
    17  	m := map[string]interface{}{}
    18  	for k, v := range header {
    19  		if len(v) == 1 {
    20  			m[k] = v[0]
    21  		} else {
    22  			m[k] = v
    23  		}
    24  	}
    25  
    26  	return headerUnmarshaler.Unmarshal(m, v)
    27  }