-
Notifications
You must be signed in to change notification settings - Fork 2
/
paths.go
148 lines (128 loc) · 3.31 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package openapi
import (
"bytes"
"encoding/json"
"strings"
"github.com/chanced/caps/text"
"github.com/chanced/transcode"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
)
type PathItemEntry struct {
Key text.Text
PathItem *PathItem
}
type PathItems = ObjMap[*PathItem]
// PathItemMap is a map of Paths that can either be a Path or a Reference
type PathItemMap = ComponentMap[*PathItem]
// 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 Access Control List
// (ACL) constraints.
type Paths struct {
Extensions `json:"-"`
// Items are the Path
PathItems `json:"-"`
}
func (p *Paths) Nodes() []Node {
if p == nil {
return nil
}
return downcastNodes(p.nodes())
}
func (p *Paths) Anchors() (*Anchors, error) {
if p == nil {
return nil, nil
}
return p.PathItems.Anchors()
}
func (p *Paths) nodes() []node {
if p == nil {
return nil
}
return appendEdges(nil, p.PathItems.nodes()...)
}
func (*Paths) ref() Ref { return nil }
func (p *Paths) setLocation(loc Location) error {
if p == nil {
return nil
}
p.PathItems.setLocation(loc)
return nil
}
// func (p *Paths) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return p.resolveNodeByPointer(ptr)
// }
// func (p *Paths) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return p, nil
// }
// nxt, tok, _ := ptr.Next()
// v := p.Items.Get(Text(tok))
// if v == nil {
// return nil, newErrNotFound(p.Location.AbsoluteLocation(), tok)
// }
// return v.resolveNodeByPointer(nxt)
// }
func (p *Paths) isNil() bool { return p == nil }
func (*Paths) Kind() Kind { return KindPaths }
func (*Paths) mapKind() Kind { return KindUndefined }
func (*Paths) sliceKind() Kind { return KindUndefined }
// MarshalJSON marshals JSON
func (p Paths) MarshalJSON() ([]byte, error) {
j, err := p.PathItems.MarshalJSON()
if err != nil {
return nil, err
}
b := bytes.Buffer{}
// removing the last } as marshalExtensionsInto execpts a buffer without it
b.Write(j[:len(j)-1])
return marshalExtensionsInto(&b, p.Extensions)
}
// UnmarshalJSON unmarshals JSON data into p
func (p *Paths) UnmarshalJSON(data []byte) error {
*p = Paths{
Extensions: Extensions{},
}
var err error
gjson.ParseBytes(data).ForEach(func(key, value gjson.Result) bool {
if strings.HasPrefix(key.String(), "x-") {
p.SetRawExtension(Text(key.String()), []byte(value.Raw))
} else {
var v PathItem
err = json.Unmarshal([]byte(value.Raw), &v)
p.Set(Text(key.String()), &v)
}
return err == nil
})
return err
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (p Paths) MarshalYAML() (interface{}, error) {
j, err := p.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (p *Paths) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, p)
}
var _ node = (*Paths)(nil)