-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
81 lines (70 loc) · 1.55 KB
/
headers.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
package urit
import (
"errors"
)
type HeadersOption interface {
GetHeaders() (map[string]string, error)
}
type Headers interface {
HeadersOption
Set(key string, value interface{}) Headers
Get(key string) (interface{}, bool)
Has(key string) bool
Del(key string) Headers
Clone() Headers
}
func NewHeaders(namesAndValues ...interface{}) (Headers, error) {
if len(namesAndValues)%2 != 0 {
return nil, errors.New("must be a value for each name")
}
result := &headers{
entries: map[string]interface{}{},
}
for i := 0; i < len(namesAndValues)-1; i += 2 {
if k, ok := namesAndValues[i].(string); ok {
result.entries[k] = namesAndValues[i+1]
} else {
return nil, errors.New("name must be a string")
}
}
return result, nil
}
type headers struct {
entries map[string]interface{}
}
func (h *headers) GetHeaders() (map[string]string, error) {
result := map[string]string{}
for k, v := range h.entries {
if str, err := getValue(v); err == nil {
result[k] = str
} else {
return result, err
}
}
return result, nil
}
func (h *headers) Set(key string, value interface{}) Headers {
h.entries[key] = value
return h
}
func (h *headers) Get(key string) (interface{}, bool) {
v, ok := h.entries[key]
return v, ok
}
func (h *headers) Has(key string) bool {
_, ok := h.entries[key]
return ok
}
func (h *headers) Del(key string) Headers {
delete(h.entries, key)
return h
}
func (h *headers) Clone() Headers {
result := &headers{
entries: map[string]interface{}{},
}
for k, v := range h.entries {
result.entries[k] = v
}
return result
}