forked from go-openapi/spec3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaths.go
84 lines (70 loc) · 1.84 KB
/
paths.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package spec3
import (
"fmt"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// Paths holds the relative paths to the individual endpoints and their operations.
// The path is appended to the URL from the Server Object in order to construct the full URL.
// The Paths MAY be empty, due to ACL constraints.
type Paths struct {
data OrderedMap
}
func (p *Paths) ForEach(fn func(string, []string) error) error {
for _, k := range p.data.Keys() {
scopes, ok := p.data.Get(k).([]string)
if !ok {
return fmt.Errorf("security requirement scopes not a []string but %T", p.data.Get(k))
}
if err := fn(k, scopes); err != nil {
return err
}
}
return nil
}
// Keys gets list of all the keys
func (p *Paths) Keys() []string {
return p.data.Keys()
}
// MarshalJSON supports json.Marshaler interface
func (p Paths) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
encodeSortedMap(&w, p.data)
return w.Buffer.BuildBytes(), w.Error
}
// MarshalEasyJSON supports easyjson.Marshaler interface
func (p Paths) MarshalEasyJSON(w *jwriter.Writer) {
encodeSortedMap(w, p.data)
}
// UnmarshalJSON supports json.Unmarshaler interface
func (p *Paths) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
decodeSortedMap(&r, &p.data)
return r.Error()
}
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (p *Paths) UnmarshalEasyJSON(l *jlexer.Lexer) {
decodeSortedMap(l, &p.data)
}
func (p *Paths) Get(path string) *PathItem {
v, ok := p.data.GetOK(path)
if !ok {
return nil
}
pi, ok := v.(*PathItem)
if !ok {
return nil
}
return pi
}
func (p *Paths) GetOK(path string) (*PathItem, bool) {
v, ok := p.data.GetOK(path)
if !ok {
return nil, false
}
pi, ok := v.(*PathItem)
return pi, ok
}
func (p *Paths) Set(path string, item *PathItem) bool {
return p.data.Set(path, item)
}