-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.go
67 lines (53 loc) · 1.71 KB
/
links.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
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 Links map[string]*LinkRef
func (ls Links) Validate() error {
for expr, l := range ls.ByIndex() {
if err := validateKey(expr); err != nil {
return err
}
if err := l.Validate(); err != nil {
return &errpath.ErrField{Field: expr, Err: err}
}
}
return nil
}
// ByIndex returns a sequence of key-value pairs ordered by index.
func (ls Links) ByIndex() iter.Seq2[string, *LinkRef] {
return ordmap.ByIndex(ls, getIndexRef[Link, *Link])
}
// Sort sorts the map by key and sets the indices accordingly.
func (ls Links) Sort() {
ordmap.Sort(ls, setIndexRef[Link, *Link])
}
// Set sets a value in the map, adding it at the end of the order.
func (ls *Links) Set(key string, l *LinkRef) {
ordmap.Set(ls, key, l, getIndexRef[Link, *Link], setIndexRef[Link, *Link])
}
// MarshalJSONTo marshals the key-value pairs in order.
func (ls *Links) MarshalJSONTo(enc *jsontext.Encoder, opts json.Options) error {
return ordmap.MarshalJSONTo(ls, enc, opts)
}
// UnmarshalJSONFrom unmarshals the key-value pairs in order and sets the indices.
func (ls *Links) UnmarshalJSONFrom(dec *jsontext.Decoder, opts json.Options) error {
return ordmap.UnmarshalJSONFrom(ls, dec, opts, setIndexRef[Link, *Link])
}
func (l *loader) collectLinks(ls Links, ref ref) {
for expr, lr := range ls.ByIndex() {
l.collectLinkRef(lr, append(ref, expr))
}
}
func (l *loader) resolveLinks(ls Links) error {
for expr, lr := range ls.ByIndex() {
if err := l.resolveLinkRef(lr); err != nil {
return &errpath.ErrField{Field: expr, Err: err}
}
}
return nil
}