forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
45 lines (40 loc) · 1.03 KB
/
converter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package hit
import (
"net/http"
"reflect"
"strings"
"github.com/Eun/go-convert"
"golang.org/x/xerrors"
)
//nolint:gochecknoglobals
var converter convert.Converter
//nolint:gochecknoinits
// create a new converter with custom converts
func init() {
converter = convert.New(convert.Options{
Recipes: []convert.Recipe{
// convert http.Header to different map types (map[string]string, map[string]interface{}, ...)
{
From: reflect.TypeOf(http.Header{}),
To: convert.MapType,
Func: func(c convert.Converter, in reflect.Value, out reflect.Value) error {
if !in.CanInterface() {
return xerrors.New("CanInterface() == false")
}
hdr, ok := in.Interface().(http.Header)
if !ok {
return xerrors.New("source is not a http.Header")
}
if hdr == nil {
return xerrors.New("source is nil")
}
m := make(map[string]string)
for key, value := range hdr {
m[key] = strings.Join(value, "")
}
return c.ConvertReflectValue(reflect.ValueOf(m), out)
},
},
},
})
}