-
Notifications
You must be signed in to change notification settings - Fork 3
/
responses.go
157 lines (133 loc) · 3.47 KB
/
responses.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
package oas
import (
"encoding/json"
"fmt"
"strconv"
)
type Responses struct {
ResponsesObject
SpecExtensions
}
func (i Responses) MarshalJSON() ([]byte, error) {
return flattenMarshalJSON(i.ResponsesObject, i.SpecExtensions)
}
func (i *Responses) UnmarshalJSON(data []byte) error {
return flattenUnmarshalJSON(data, &i.ResponsesObject, &i.SpecExtensions)
}
type ResponsesObject struct {
Default *Response
Responses map[int]*Response
}
func (o *ResponsesObject) SetDefaultResponse(r *Response) {
if r == nil {
return
}
o.Default = r
}
func (o *ResponsesObject) AddResponse(statusCode int, r *Response) {
if r == nil {
return
}
if o.Responses == nil {
o.Responses = make(map[int]*Response)
}
o.Responses[statusCode] = r
}
func (o ResponsesObject) MarshalJSON() ([]byte, error) {
responses := make(map[string]*Response)
if o.Default != nil {
responses["default"] = o.Default
}
for status := range o.Responses {
responses[fmt.Sprintf("%d", status)] = o.Responses[status]
}
return json.Marshal(responses)
}
func (o *ResponsesObject) UnmarshalJSON(data []byte) error {
responses := make(map[string]*Response)
err := json.Unmarshal(data, &responses)
if err != nil {
return err
}
for statusOrDefault := range responses {
if statusOrDefault == "default" {
o.Default = responses[statusOrDefault]
} else if status, err := strconv.ParseInt(statusOrDefault, 10, 10); err == nil {
if o.Responses == nil {
o.Responses = make(map[int]*Response)
}
o.Responses[int(status)] = responses[statusOrDefault]
}
}
return nil
}
func NewResponse(desc string) *Response {
resp := &Response{}
resp.Description = desc
return resp
}
type Response struct {
Reference
ResponseObject
SpecExtensions
}
func (r Response) MarshalJSON() ([]byte, error) {
return r.MarshalJSONRefFirst(r.ResponseObject, r.SpecExtensions)
}
func (r *Response) UnmarshalJSON(data []byte) error {
return r.UnmarshalJSONRefFirst(data, &r.ResponseObject, &r.SpecExtensions)
}
type ResponseObject struct {
Description string `json:"description"`
WithHeaders
WithContent
WithLinks
}
type WithLinks struct {
Links map[string]*Link `json:"links,omitempty"`
}
func (object *WithLinks) AddLink(name string, l *Link) {
if l == nil {
return
}
if object.Links == nil {
object.Links = make(map[string]*Link)
}
object.Links[name] = l
}
func NewLink(operationId string) *Link {
return &Link{
LinkObject: LinkObject{
OperationId: operationId,
},
}
}
type Link struct {
Reference
LinkObject
SpecExtensions
}
func (l Link) MarshalJSON() ([]byte, error) {
return l.MarshalJSONRefFirst(l.LinkObject, l.SpecExtensions)
}
func (l *Link) UnmarshalJSON(data []byte) error {
return l.UnmarshalJSONRefFirst(data, &l.LinkObject, &l.SpecExtensions)
}
type LinkObject struct {
OperationRef string `json:"operationRef,omitempty"`
OperationId string `json:"operationId,omitempty"`
Parameters map[string]RuntimeExpression `json:"parameters,omitempty"`
RequestBody RuntimeExpression `json:"requestBody,omitempty"`
Description string `json:"description,omitempty"`
Server *Server `json:"server,omitempty"`
}
func (o *LinkObject) AddParameter(name string, expr RuntimeExpression) {
if o.Parameters == nil {
o.Parameters = make(map[string]RuntimeExpression)
}
o.Parameters[name] = expr
}
func (o *LinkObject) SetRequestBody(expr RuntimeExpression) {
o.RequestBody = expr
}
type RuntimeExpression string