-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.go
61 lines (48 loc) · 1.6 KB
/
content.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
package openapi
import (
"iter"
"github.com/MarkRosemaker/errpath"
"github.com/MarkRosemaker/ordmap"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
type Content map[MediaRange]*MediaType
func (c Content) Validate() error {
for mr, mt := range c.ByIndex() {
if err := mr.Validate(); err != nil {
return &errpath.ErrKey{Key: string(mr), Err: err}
}
if err := mt.Validate(); err != nil {
return &errpath.ErrKey{Key: string(mr), Err: err}
}
}
return nil
}
// ByIndex returns a sequence of key-value pairs ordered by index.
func (c Content) ByIndex() iter.Seq2[MediaRange, *MediaType] {
return ordmap.ByIndex(c, getIndexMediaType)
}
// Sort sorts the map by key and sets the indices accordingly.
func (c Content) Sort() {
ordmap.Sort(c, setIndexMediaType)
}
// Set sets a value in the map, adding it at the end of the order.
func (c *Content) Set(mr MediaRange, mt *MediaType) {
ordmap.Set(c, mr, mt, getIndexMediaType, setIndexMediaType)
}
// MarshalJSONTo marshals the key-value pairs in order.
func (c *Content) MarshalJSONTo(enc *jsontext.Encoder, opts json.Options) error {
return ordmap.MarshalJSONTo(c, enc, opts)
}
// UnmarshalJSONFrom unmarshals the key-value pairs in order and sets the indices.
func (c *Content) UnmarshalJSONFrom(dec *jsontext.Decoder, opts json.Options) error {
return ordmap.UnmarshalJSONFrom(c, dec, opts, setIndexMediaType)
}
func (l *loader) resolveContent(c Content) error {
for mr, mt := range c.ByIndex() {
if err := l.resolveMediaType(mt); err != nil {
return &errpath.ErrKey{Key: string(mr), Err: err}
}
}
return nil
}