-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_items.go
68 lines (54 loc) · 1.85 KB
/
path_items.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
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 PathItems map[string]*PathItemRef
// Validate checks that all keys and values are valid.
func (ps PathItems) Validate() error {
for name, p := range ps.ByIndex() {
if err := validateKey(name); err != nil {
return err
}
if err := p.Validate(); err != nil {
return &errpath.ErrKey{Key: name, Err: err}
}
}
return nil
}
// ByIndex returns a sequence of key-value pairs ordered by index.
func (ps PathItems) ByIndex() iter.Seq2[string, *PathItemRef] {
return ordmap.ByIndex(ps, getIndexRef[PathItem, *PathItem])
}
// Sort sorts the map by key and sets the indices accordingly.
func (ps PathItems) Sort() {
ordmap.Sort(ps, setIndexRef[PathItem, *PathItem])
}
// Set sets a value in the map, adding it at the end of the order.
func (ps *PathItems) Set(key string, v *PathItemRef) {
ordmap.Set(ps, key, v, getIndexRef[PathItem, *PathItem], setIndexRef[PathItem, *PathItem])
}
// MarshalJSONTo marshals the key-value pairs in order.
func (ps *PathItems) MarshalJSONTo(enc *jsontext.Encoder, opts json.Options) error {
return ordmap.MarshalJSONTo(ps, enc, opts)
}
// UnmarshalJSONFrom unmarshals the key-value pairs in order and sets the indices.
func (ps *PathItems) UnmarshalJSONFrom(dec *jsontext.Decoder, opts json.Options) error {
return ordmap.UnmarshalJSONFrom(ps, dec, opts, setIndexRef[PathItem, *PathItem])
}
func (l *loader) collectPathItems(ps PathItems, ref ref) {
for name, p := range ps.ByIndex() {
l.collectPathItemRef(p, append(ref, name))
}
}
func (l *loader) resolvePathItems(ps PathItems) error {
for name, p := range ps.ByIndex() {
if err := l.resolvePathItemRef(p); err != nil {
return &errpath.ErrKey{Key: name, Err: err}
}
}
return nil
}