-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
item.go
73 lines (56 loc) · 1.21 KB
/
item.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
package httpsfv
import (
"strings"
)
// Item is a bare value and associated parameters.
// See https://httpwg.org/specs/rfc8941.html#item.
type Item struct {
Value interface{}
Params *Params
}
// NewItem returns a new Item.
func NewItem(v interface{}) Item {
assertBareItem(v)
return Item{v, NewParams()}
}
func (i Item) member() {
}
// marshalSFV serializes as defined in
// https://httpwg.org/specs/rfc8941.html#ser-item.
func (i Item) marshalSFV(b *strings.Builder) error {
if i.Value == nil {
return ErrInvalidBareItem
}
if err := marshalBareItem(b, i.Value); err != nil {
return err
}
return i.Params.marshalSFV(b)
}
// UnmarshalItem parses an item as defined in
// https://httpwg.org/specs/rfc8941.html#parse-item.
func UnmarshalItem(v []string) (Item, error) {
s := &scanner{
data: strings.Join(v, ","),
}
s.scanWhileSp()
sfv, err := parseItem(s)
if err != nil {
return Item{}, err
}
s.scanWhileSp()
if !s.eof() {
return Item{}, &UnmarshalError{off: s.off}
}
return sfv, nil
}
func parseItem(s *scanner) (Item, error) {
bi, err := parseBareItem(s)
if err != nil {
return Item{}, err
}
p, err := parseParams(s)
if err != nil {
return Item{}, err
}
return Item{bi, p}, nil
}