-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathheader.go
58 lines (49 loc) · 1.45 KB
/
header.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
package postman
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
// Header represents an HTTP Header.
type Header struct {
Key string `json:"key"`
Value string `json:"value"`
Disabled bool `json:"disabled,omitempty"`
Description string `json:"description,omitempty"`
}
// HeaderList contains a list of headers.
type HeaderList struct {
Headers []*Header
}
// MarshalJSON returns the JSON encoding of a HeaderList.
func (hl HeaderList) MarshalJSON() ([]byte, error) {
return json.Marshal(hl.Headers)
}
// UnmarshalJSON parses the JSON-encoded data and create a HeaderList from it.
// A HeaderList can be created from an array or a string.
func (hl *HeaderList) UnmarshalJSON(b []byte) (err error) {
if len(b) == 0 {
return nil
} else if len(b) >= 2 && b[0] == '"' && b[len(b)-1] == '"' {
headersString := string(b[1 : len(b)-1])
for _, header := range strings.Split(headersString, "\n") {
if strings.TrimSpace(header) == "" {
continue
}
headerParts := strings.Split(header, ":")
if len(headerParts) != 2 {
return fmt.Errorf("invalid header, missing key or value: %s", header)
}
hl.Headers = append(hl.Headers, &Header{
Key: strings.TrimSpace(headerParts[0]),
Value: strings.TrimSpace(string(headerParts[1])),
})
}
} else if len(b) >= 2 && b[0] == '[' && b[len(b)-1] == ']' {
err = json.Unmarshal(b, &hl.Headers)
} else {
err = errors.New("unsupported type for header list")
}
return
}