-
Notifications
You must be signed in to change notification settings - Fork 2
/
callbacks.go
173 lines (150 loc) · 3.85 KB
/
callbacks.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package openapi
import (
"encoding/json"
"strings"
"github.com/chanced/transcode"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
)
// CallbacksMap is a map of reusable Callback Objects.
type CallbacksMap = ComponentMap[*Callbacks]
// Callbacks is map of possible out-of band callbacks related to the parent
// operation. Each value in the map is a Path Item Object that describes a set
// of requests that may be initiated by the API provider and the expected
// responses. The key value used to identify the path item object is an
// expression, evaluated at runtime, that identifies a URL to use for the
// callback operation.
//
// To describe incoming requests from the API provider independent from another
// API call, use the webhooks field.
type Callbacks struct {
Extensions `json:"-"`
PathItems `json:"-"`
}
func (c *Callbacks) Nodes() []Node {
if c == nil {
return nil
}
return downcastNodes(c.nodes())
}
func (c *Callbacks) nodes() []node {
if c == nil {
return nil
}
edges := make([]node, 0, 1)
edges = appendEdges(edges, c.PathItems.nodes()...)
return edges
}
func (c *Callbacks) ref() Ref { return nil }
// Edges returns the immediate edges of the Node. This is used to build a
// graph of the OpenAPI document.
//
// kind returns KindCallback
func (*Callbacks) Kind() Kind { return KindCallbacks }
func (*Callbacks) mapKind() Kind { return KindCallbacksMap }
func (Callbacks) sliceKind() Kind { return KindUndefined }
func (c *Callbacks) isNil() bool {
return c == nil
}
func (c *Callbacks) Anchors() (*Anchors, error) {
if c == nil {
return nil, nil
}
return c.PathItems.Anchors()
}
func (c *Callbacks) Refs() []Ref {
if c == nil {
return nil
}
return c.PathItems.Refs()
}
// // ResolveNodeByPointer performs a l
// func (c *Callbacks) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return c.resolveNodeByPointer(ptr)
// }
// func (c *Callbacks) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return c, nil
// }
// nxt, tok, _ := ptr.Next()
// item := c.Items.Get(Text(tok))
// if item == nil {
// return nil, newErrNotFound(c.Location.AbsoluteLocation(), tok)
// }
// return item.resolveNodeByPointer(nxt)
// }
func (c *Callbacks) location() Location {
return c.Location
}
// MarshalJSON marshals JSON
func (c Callbacks) MarshalJSON() ([]byte, error) {
type callback Callbacks
return marshalExtendedJSON(callback(c))
}
// UnmarshalJSON unmarshals JSON
func (c *Callbacks) UnmarshalJSON(data []byte) error {
*c = Callbacks{
Extensions: Extensions{},
}
var err error
gjson.ParseBytes(data).ForEach(func(key, value gjson.Result) bool {
if strings.HasPrefix(key.String(), "x-") {
c.SetRawExtension(Text(key.String()), []byte(value.Raw))
} else {
var v PathItem
err = json.Unmarshal([]byte(value.Raw), &v)
c.Set(Text(key.String()), &v)
}
return err == nil
})
return err
}
func (c *Callbacks) setLocation(loc Location) error {
if c == nil {
return nil
}
c.Location = loc
return c.PathItems.setLocation(loc)
}
func (c Callbacks) MarshalYAML() (interface{}, error) {
j, err := c.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 implements yaml.Unmarshaler
func (c *Callbacks) 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, c)
}
// func (c *Callbacks) Walk(v Visitor) error {
// if v == nil {
// return nil
// }
// v, err := v.Visit(c)
// if err != nil {
// return err
// }
// if v == nil {
// return nil
// }
// return c.Items.Walk(v)
// }
func (c *Callbacks) refable() {}
var _ node = (*Callbacks)(nil)