-
Notifications
You must be signed in to change notification settings - Fork 636
/
header_encoder.go
120 lines (95 loc) · 2.69 KB
/
header_encoder.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package protocol
import (
"fmt"
"net/http"
"strings"
)
// HeaderMapEncoder builds a map valu
type HeaderMapEncoder struct {
Prefix string
Header http.Header
Err error
}
// MapSetValue adds a single value to the header.
func (e *HeaderMapEncoder) MapSetValue(k string, v ValueMarshaler) {
if e.Err != nil {
return
}
str, err := v.MarshalValue()
if err != nil {
e.Err = err
return
}
k = strings.TrimSpace(k)
str = strings.TrimSpace(str)
if len(e.Prefix) > 0 {
k = e.Prefix + k
}
e.Header.Set(k, str)
}
// List executes the passed in callback with a list encoder based on
// the context of this HeaderMapEncoder.
func (e *HeaderMapEncoder) List(k string) ListEncoder {
if e.Err != nil {
return nil
}
if len(e.Prefix) > 0 {
k = e.Prefix + k
}
return &HeaderListEncoder{Key: k, Header: e.Header}
}
// Map sets the header element with nested maps appending the
// passed in k to the prefix if one was set.
func (e *HeaderMapEncoder) Map(k string) MapEncoder {
if e.Err != nil {
return nil
}
if len(e.Prefix) > 0 {
k = e.Prefix + k
}
return &HeaderMapEncoder{Prefix: k, Header: e.Header}
}
// Start does nothing for header encodings.
func (e *HeaderMapEncoder) Start() {}
// End does nothing for header encodings.
func (e *HeaderMapEncoder) End() {}
// MapSetFields Is not implemented, query map of FieldMarshaler is undefined.
func (e *HeaderMapEncoder) MapSetFields(k string, m FieldMarshaler) {
e.Err = fmt.Errorf("header map encoder MapSetFields not supported, %s", k)
}
// HeaderListEncoder will encode list values nested into a header key.
type HeaderListEncoder struct {
Key string
Header http.Header
Err error
}
// ListAddValue encodes an individual list value into the header.
func (e *HeaderListEncoder) ListAddValue(v ValueMarshaler) {
if e.Err != nil {
return
}
str, err := v.MarshalValue()
if err != nil {
e.Err = err
return
}
e.Header.Add(e.Key, str)
}
// List Is not implemented, header list of list is undefined.
func (e *HeaderListEncoder) List() ListEncoder {
e.Err = fmt.Errorf("header list encoder ListAddList not supported, %s", e.Key)
return nil
}
// Map Is not implemented, header list of map is undefined.
func (e *HeaderListEncoder) Map() MapEncoder {
e.Err = fmt.Errorf("header list encoder ListAddMap not supported, %s", e.Key)
return nil
}
// Start does nothing for header list encodings.
func (e *HeaderListEncoder) Start() {}
// End does nothing for header list encodings.
func (e *HeaderListEncoder) End() {}
// ListAddFields Is not implemented, query list of FieldMarshaler is undefined.
func (e *HeaderListEncoder) ListAddFields(m FieldMarshaler) {
e.Err = fmt.Errorf("header list encoder ListAddFields not supported, %s", e.Key)
}