-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdictionary.go
More file actions
167 lines (131 loc) · 2.96 KB
/
dictionary.go
File metadata and controls
167 lines (131 loc) · 2.96 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package httpsfv
import (
"errors"
"strings"
)
// Dictionary is an ordered map of name-value pairs.
// See https://httpwg.org/specs/rfc9651.html#dictionary
// Values can be:
// * Item (Section 3.3.)
// * Inner List (Section 3.1.1.)
type Dictionary struct {
names []string
values map[string]Member
}
// ErrInvalidDictionaryFormat is returned when a dictionary value is invalid.
var ErrInvalidDictionaryFormat = errors.New("invalid dictionary format")
// NewDictionary creates a new ordered map.
func NewDictionary() *Dictionary {
d := Dictionary{}
d.names = []string{}
d.values = map[string]Member{}
return &d
}
// Get retrieves a member.
func (d *Dictionary) Get(k string) (Member, bool) {
v, ok := d.values[k]
return v, ok
}
// Add appends a new member to the ordered list.
func (d *Dictionary) Add(k string, v Member) {
if _, exists := d.values[k]; !exists {
d.names = append(d.names, k)
}
d.values[k] = v
}
// Del removes a member from the ordered list.
func (d *Dictionary) Del(key string) bool {
if _, ok := d.values[key]; !ok {
return false
}
for i, k := range d.names {
if k == key {
d.names = append(d.names[:i], d.names[i+1:]...)
break
}
}
delete(d.values, key)
return true
}
// Names retrieves the list of member names in the appropriate order.
func (d *Dictionary) Names() []string {
return d.names
}
func (d *Dictionary) marshalSFV(b *strings.Builder) error {
last := len(d.names) - 1
for m, k := range d.names {
if err := marshalKey(b, k); err != nil {
return err
}
v := d.values[k]
if item, ok := v.(Item); ok && item.Value == true {
if err := item.Params.marshalSFV(b); err != nil {
return err
}
} else {
if err := b.WriteByte('='); err != nil {
return err
}
if err := v.marshalSFV(b); err != nil {
return err
}
}
if m != last {
if _, err := b.WriteString(", "); err != nil {
return err
}
}
}
return nil
}
// UnmarshalDictionary parses a dictionary as defined in
// https://httpwg.org/specs/rfc9651.html#parse-dictionary.
func UnmarshalDictionary(v []string) (*Dictionary, error) {
s := &scanner{
data: strings.Join(v, ","),
}
s.scanWhileSp()
sfv, err := parseDictionary(s)
if err != nil {
return sfv, err
}
return sfv, nil
}
func parseDictionary(s *scanner) (*Dictionary, error) {
d := NewDictionary()
for !s.eof() {
k, err := parseKey(s)
if err != nil {
return nil, err
}
var m Member
if !s.eof() && s.data[s.off] == '=' {
s.off++
m, err = parseItemOrInnerList(s)
if err != nil {
return nil, err
}
} else {
p, err := parseParams(s)
if err != nil {
return nil, err
}
m = Item{true, p}
}
d.Add(k, m)
s.scanWhileOWS()
if s.eof() {
return d, nil
}
if s.data[s.off] != ',' {
return nil, &UnmarshalError{s.off, ErrInvalidDictionaryFormat}
}
s.off++
s.scanWhileOWS()
if s.eof() {
// there is a trailing comma
return nil, &UnmarshalError{s.off, ErrInvalidDictionaryFormat}
}
}
return d, nil
}