-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodings.go
48 lines (38 loc) · 1.37 KB
/
encodings.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
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"
)
// Encodings is a map between a property name and its encoding information.
type Encodings map[string]*Encoding
func (es Encodings) Validate() error {
for k, e := range es.ByIndex() {
if err := e.Validate(); err != nil {
return &errpath.ErrKey{Key: k, Err: err}
}
}
return nil
}
// ByIndex returns a sequence of key-value pairs ordered by index.
func (es Encodings) ByIndex() iter.Seq2[string, *Encoding] {
return ordmap.ByIndex(es, getIndexEncoding)
}
// Sort sorts the map by key and sets the indices accordingly.
func (es Encodings) Sort() {
ordmap.Sort(es, setIndexEncoding)
}
// Set sets a value in the map, adding it at the end of the order.
func (es *Encodings) Set(key string, e *Encoding) {
ordmap.Set(es, key, e, getIndexEncoding, setIndexEncoding)
}
// MarshalJSONTo marshals the key-value pairs in order.
func (es *Encodings) MarshalJSONTo(enc *jsontext.Encoder, opts json.Options) error {
return ordmap.MarshalJSONTo(es, enc, opts)
}
// UnmarshalJSONFrom unmarshals the key-value pairs in order and sets the indices.
func (es *Encodings) UnmarshalJSONFrom(dec *jsontext.Decoder, opts json.Options) error {
return ordmap.UnmarshalJSONFrom(es, dec, opts, setIndexEncoding)
}