-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.go
More file actions
72 lines (57 loc) · 2.08 KB
/
content.go
File metadata and controls
72 lines (57 loc) · 2.08 KB
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
package openapi
import (
"encoding/json/jsontext"
"encoding/json/v2"
"iter"
"github.com/MarkRosemaker/errpath"
"github.com/MarkRosemaker/ordmap"
)
// The content of a request body. The key is a media type or media type range, see [RFC7231 Appendix D], and the value describes it. For requests that match multiple keys, only the most specific key is applicable. e.g. text/plain overrides text/*
// [Specification]
// ([Specification])
//
// [Specification]: https://spec.openapis.org/oas/v3.1.0#fixed-fields-10
// [RFC7231 Appendix D]: https://datatracker.ietf.org/doc/html/rfc7231#appendix-D
type Content map[MediaRange]*MediaType
// Validate validates the request body content.
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)
}
var _ json.MarshalerTo = (*Content)(nil)
// MarshalJSONTo marshals the key-value pairs in order.
func (c *Content) MarshalJSONTo(enc *jsontext.Encoder) error {
return ordmap.MarshalJSONTo(c, enc)
}
var _ json.UnmarshalerFrom = (*Content)(nil)
// UnmarshalJSONFrom unmarshals the key-value pairs in order and sets the indices.
func (c *Content) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
return ordmap.UnmarshalJSONFrom(c, dec, 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
}