-
Notifications
You must be signed in to change notification settings - Fork 2
/
link.go
176 lines (150 loc) · 4.56 KB
/
link.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
package openapi
import (
"encoding/json"
"github.com/chanced/jsonx"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
// LinkMap is a Map of either LinkMap or References to LinkMap
type (
LinkMap = ComponentMap[*Link]
)
// Link represents a possible design-time link for a response. The presence of a
// link does not guarantee the caller's ability to successfully invoke it,
// rather it provides a known relationship and traversal mechanism between
// responses and other operations.
//
// Unlike dynamic links (i.e. links provided in the response payload), the OAS
// linking mechanism does not require link information in the runtime response.
//
// For computing links, and providing instructions to execute them, a runtime
// expression is used for accessing values in an operation and using them as
// parameters while invoking the linked operation.
type Link struct {
Extensions `json:"-"`
Location `json:"-"`
// The name of an existing, resolvable OAS operation, as defined with a
// unique operationId. This field is mutually exclusive of the operationRef
// field.
OperationID Text `json:"operationId,omitempty"`
// A relative or absolute URI reference to an OAS operation.
//
// This field is mutually exclusive of the operationId field, and MUST point
// to an Operation Object.
OperationRef *OperationRef `json:"operationRef,omitempty"`
// A description of the link. CommonMark syntax MAY be used for rich text
// representation.
Description Text `json:"description,omitempty"`
// A map representing parameters to pass to an operation as specified with
// operationID or identified via operationRef.
//
// The key is the parameter name
// to be used, whereas the value can be a constant or an expression to be
// evaluated and passed to the linked operation.
//
// The parameter name can be
// qualified using the parameter location [{in}.]{name} for operations that
// use the same parameter name in different locations (e.g. path.id).
Parameters OrderedJSONObj `json:"parameters,omitempty"`
// A literal value or {expression} to use as a request body when calling the
// target operation.
RequestBody jsonx.RawMessage `json:"requestBody,omitempty"`
}
func (l *Link) Nodes() []Node {
if l == nil {
return nil
}
return downcastNodes(l.nodes())
}
func (l *Link) nodes() []node {
if l == nil {
return nil
}
return appendEdges(nil, l.OperationRef)
}
func (l *Link) Refs() []Ref {
if l == nil {
return nil
}
var refs []Ref
if l.OperationRef != nil {
refs = append(refs, l.OperationRef)
}
return refs
}
func (l *Link) Anchors() (*Anchors, error) { return nil, nil }
// func (l *Link) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return l.resolveNodeByPointer(ptr)
// }
// func (l *Link) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return l, nil
// }
// tok, _ := ptr.NextToken()
// return nil, newErrNotResolvable(l.Location.AbsoluteLocation(), tok)
// }
func (*Link) mapKind() Kind { return KindLinkMap }
func (*Link) sliceKind() Kind { return KindUndefined }
// MarshalJSON marshals JSON
func (l Link) MarshalJSON() ([]byte, error) {
type link Link
return marshalExtendedJSON(link(l))
}
// UnmarshalJSON unmarshals JSON
func (l *Link) UnmarshalJSON(data []byte) error {
type link Link
var lv link
if err := unmarshalExtendedJSON(data, &lv); err != nil {
return err
}
*l = Link(lv)
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (l Link) MarshalYAML() (interface{}, error) {
j, err := l.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 (l *Link) 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, l)
}
// DecodeRequestBody decodes l.RequestBody into dst
//
// dst should be a pointer to a concrete type
func (l *Link) DecodeRequestBody(dst interface{}) error {
return json.Unmarshal(l.RequestBody, dst)
}
func (*Link) Kind() Kind { return KindLink }
func (l *Link) setLocation(loc Location) error {
if l == nil {
return nil
}
l.Location = loc
if l.OperationRef != nil {
l.OperationRef.Location = loc.AppendLocation("operationRef")
}
return nil
}
func (l *Link) isNil() bool { return l == nil }
func (l *Link) refable() {}
var _ node = (*Link)(nil)