-
Notifications
You must be signed in to change notification settings - Fork 2
/
operation_ref.go
168 lines (143 loc) · 3.45 KB
/
operation_ref.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
package openapi
import (
"encoding/json"
"fmt"
"github.com/chanced/transcode"
"github.com/chanced/uri"
"gopkg.in/yaml.v3"
)
type OperationRef struct {
Location
Ref *uri.URI
Resolved *Operation
}
func (*OperationRef) RefType() RefType { return RefTypeOperationRef }
func (*OperationRef) RefKind() Kind { return KindOperation }
func (or *OperationRef) Nodes() []Node {
if or == nil {
return nil
}
return downcastNodes(or.nodes())
}
func (or *OperationRef) ResolvedNode() Node {
return or.Resolved
}
func (or *OperationRef) nodes() []node {
if or == nil {
return nil
}
var edges []node
return appendEdges(edges, or.Resolved)
}
func (or *OperationRef) refs() []node {
return []node{or.Resolved}
}
func (or *OperationRef) Refs() []Ref {
return nil
}
func (or *OperationRef) IsResolved() bool {
return or.Resolved != nil
}
// URI returns the reference URI
func (or *OperationRef) URI() *uri.URI {
return or.Ref
}
func (*OperationRef) Anchors() (*Anchors, error) { return nil, nil }
func (*OperationRef) Kind() Kind { return KindOperationRef }
func (*OperationRef) mapKind() Kind { return KindUndefined }
func (*OperationRef) sliceKind() Kind { return KindUndefined }
// func (or *OperationRef) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return or.resolveNodeByPointer(ptr)
// }
// func (or *OperationRef) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return or, nil
// }
// tok, _ := ptr.NextToken()
// return nil, newErrNotResolvable(or.AbsoluteLocation(), tok)
// }
func (or OperationRef) MarshalJSON() ([]byte, error) {
return json.Marshal(or.Ref)
}
func (or *OperationRef) UnmarshalJSON(data []byte) error {
var uri uri.URI
if err := json.Unmarshal(data, &uri); err != nil {
return err
}
or.Ref = &uri
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (or OperationRef) MarshalYAML() (interface{}, error) {
j, err := or.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 (or *OperationRef) 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, or)
}
func (o *OperationRef) isNil() bool { return o == nil }
func (op *OperationRef) setLocation(loc Location) error {
if op == nil {
return nil
}
op.Location = loc
return nil
}
func (o *OperationRef) resolve(n Node) error {
if o == nil {
return fmt.Errorf("openapi: OperationRef is nil")
}
if n == nil {
return fmt.Errorf("openapi: node is nil")
}
switch n.Kind() {
case KindOperation:
o.Resolved = n.(*Operation)
default:
return fmt.Errorf("openapi: cannot resolve %s to %s", n.Kind(), o.Kind())
}
if op, ok := n.(*Operation); ok {
o.Resolved = op
return nil
}
return fmt.Errorf("openapi: failed convert %s to %s", n.Kind(), o.Kind())
}
var (
_ node = (*OperationRef)(nil)
_ Ref = (*OperationRef)(nil)
_ ref = (*OperationRef)(nil)
)
// func (or *OperationRef) Walk(v Visitor) error {
// if v == nil {
// return nil
// }
// v, err := v.Visit(or)
// if err != nil {
// return err
// }
// if v == nil {
// return nil
// }
// _, err = v.VisitOperationRef(or)
// return err
// }