-
Notifications
You must be signed in to change notification settings - Fork 2
/
component.go
246 lines (215 loc) · 4.91 KB
/
component.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package openapi
import (
"encoding/json"
"fmt"
"github.com/chanced/transcode"
"github.com/chanced/uri"
"gopkg.in/yaml.v3"
)
type Component[T refable] struct {
Location
Reference *Reference[T]
Object T
}
func (c *Component[T]) nodes() []node {
if c == nil {
return nil
}
if c.IsReference() {
return appendEdges(nil, c.Reference)
}
return appendEdges(nil, c.Object)
}
// Edges returns the immediate edges of the Node. This is used to build a
// graph of the OpenAPI document.
//
// IsResolved implements Ref
func (c *Component[T]) IsResolved() bool {
if c == nil || c.Reference == nil {
return true
}
return !c.Object.isNil()
}
// URI implements Ref
func (c *Component[T]) URI() *uri.URI {
if !c.IsReference() {
return nil
}
return c.Reference.Ref
}
// MakeReference converts the Component into a reference, altering the path of
// all nested nodes.
func (c *Component[T]) MakeReference(ref uri.URI) error {
if c.Object.isNil() {
return fmt.Errorf("cannot make reference to nil object")
}
c.Reference.dst = &c.Object
c.Reference.Ref = &ref
loc, err := NewLocation(ref)
if err != nil {
return err
}
return c.Object.setLocation(loc)
}
func (c *Component[T]) Kind() Kind {
switch c.ObjectKind() {
case KindExample:
return KindExampleComponent
case KindHeader:
return KindHeaderComponent
case KindServer:
return KindServerComponent
case KindLink:
return KindLinkComponent
case KindResponse:
return KindResponseComponent
case KindParameter:
return KindParameterComponent
case KindPathItem:
return KindPathItemComponent
case KindRequestBody:
return KindRequestBodyComponent
case KindCallbacks:
return KindCallbacksComponent
case KindSecurityScheme:
return KindSecuritySchemeComponent
default:
return KindUndefined
}
}
func (c *Component[T]) location() Location {
if c.Reference != nil {
return c.Reference.Location
}
return c.Object.location()
}
// IsRef returns false
//
// IsReference returns true if this Component contains a Reference
func (c *Component[T]) IsReference() bool { return !c.Reference.isNil() }
func (c *Component[T]) Refs() []Ref {
if c == nil {
return nil
}
if c.IsReference() {
return []Ref{c.Reference}
}
return c.Object.Refs()
}
func (*Component[T]) mapKind() Kind {
var t T
return t.mapKind()
}
func (*Component[T]) sliceKind() Kind {
var t T
return t.sliceKind()
}
func (c Component[T]) MarshalJSON() ([]byte, error) {
if c.Reference != nil {
return json.Marshal(c.Reference)
}
if any(c.Object) != nil {
return c.Object.MarshalJSON()
}
return nil, nil
}
// ComponentKind returns the Kind of the containing Object, regardless of if it
// is referenced or not.
func (c *Component[T]) ObjectKind() Kind {
return c.Object.Kind()
}
func (c *Component[T]) UnmarshalJSON(data []byte) error {
if isRefJSON(data) {
var ref Reference[T]
if err := json.Unmarshal(data, &ref); err != nil {
return err
}
ref.ReferencedKind = c.ObjectKind()
ref.dst = &c.Object
c.Reference = &ref
*c = Component[T]{
Reference: &ref,
}
return nil
}
var obj T
k := obj.Kind()
_ = k
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
*c = Component[T]{
Object: obj,
}
return nil
}
func (c *Component[T]) 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 *Component[T]) 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 *Component[T]) setLocation(loc Location) error {
if c == nil {
return nil
}
c.Location = loc
if c.Reference != nil {
return c.Reference.setLocation(loc)
} else if !c.Object.isNil() {
return c.Object.setLocation(loc)
}
return nil
}
func (c *Component[T]) Anchors() (*Anchors, error) {
if c == nil {
return nil, nil
}
if c.Reference != nil {
return nil, nil
}
return c.Object.Anchors()
}
func (c *Component[T]) isNil() bool { return c == nil }
var _ node = (*Component[*Response])(nil)
// func (c *Component[T]) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return c.resolveNodeByPointer(ptr)
// }
// func (c *Component[T]) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return c, nil
// }
// nxt, tok, _ := ptr.Next()
// switch tok {
// case "$ref":
// if nxt.IsRoot() {
// return c.Reference, nil
// }
// return nil, newErrNotResolvable(c.Location.AbsoluteLocation(), tok)
// default:
// // TODO: this may need to change. Not sure when I need to perform these
// // resolutions just yet. If before population, Object may be nil at this call.
// return c.Object.resolveNodeByPointer(nxt)
// }
// }