-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathresponse.go
268 lines (262 loc) Β· 7.25 KB
/
response.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package dsl
import (
"goa.design/goa/eval"
"goa.design/goa/expr"
)
// Response describes a HTTP or a gRPC response. Response describes both success
// and error responses. When describing an error response the first argument is
// the name of the error.
//
// While a service method may only define a single result type, Response may
// appear multiple times to define multiple success HTTP responses. In this case
// the Tag expression makes it possible to identify a result type attribute and
// a corresponding string value used to select the proper success response (each
// success response is associated with a different tag value). gRPC responses
// may only define one success response.
//
// Response may appear in an API or service expression to define error responses
// common to all the API or service methods. Response may also appear in a
// method expression to define both success and error responses specific to the
// method.
//
// Response accepts one to three arguments. Success response accepts a status
// code as first argument. If the first argument is a status code then a
// function may be given as the second argument. This function may provide a
// description and describes how to map the result type attributes to transport
// specific constructs (e.g. HTTP headers and body, gRPC metadata and message).
//
// The valid invocations for successful response are thus:
//
// * Response(status)
//
// * Response(func)
//
// * Response(status, func)
//
// Error responses additionally accept the name of the error as first argument.
//
// * Response(error_name, status)
//
// * Response(error_name, func)
//
// * Response(error_name, status, func)
//
// By default (i.e. if Response only defines a status code) then:
//
// - success HTTP responses use code 200 (OK) and error HTTP responses use code 400 (BadRequest)
// - success gRPC responses use code 0 (OK) and error gRPC response use code 2 (Unknown)
// - The result type attributes are all mapped to the HTTP response body or gRPC response message.
//
// Example:
//
// Method("create", func() {
// Payload(CreatePayload)
// Result(CreateResult)
// Error("an_error")
//
// HTTP(func() {
// Response(StatusAccepted, func() { // HTTP status code set using argument
// Description("Response used for async creations")
// Tag("outcome", "accepted") // Tag identifies a result type attribute and corresponding
// // value for this response to be selected.
// Header("taskHref") // map "taskHref" attribute to header, all others to body
// })
//
// Response(StatusCreated, func () {
// Tag("outcome", "created") // CreateResult type to describe body
// })
//
// Response(func() {
// Description("Response used when item already exists")
// Code(StatusNoContent) // HTTP status code set using Code
// Body(Empty) // Override method result type
// })
//
// Response("an_error", StatusConflict) // Override default of 400
// })
//
// GRPC(func() {
// Response(CodeOK, func() {
// Metadata("taskHref") // map "taskHref" attribute to metadata, all others to message
// })
//
// Response("an_error", CodeInternal, func() {
// Description("Error returned for internal errors")
// })
// })
// })
//
func Response(val interface{}, args ...interface{}) {
name, ok := val.(string)
switch t := eval.Current().(type) {
case *expr.HTTPExpr:
if !ok {
eval.InvalidArgError("name of error", val)
return
}
if e := httpError(name, t, args...); e != nil {
t.Errors = append(t.Errors, e)
}
case *expr.GRPCExpr:
if !ok {
eval.InvalidArgError("name of error", val)
return
}
if e := grpcError(name, t, args...); e != nil {
t.Errors = append(t.Errors, e)
}
case *expr.HTTPServiceExpr:
if !ok {
eval.InvalidArgError("name of error", val)
return
}
if e := httpError(name, t, args...); e != nil {
t.HTTPErrors = append(t.HTTPErrors, e)
}
case *expr.HTTPEndpointExpr:
if ok {
if e := httpError(name, t, args...); e != nil {
t.HTTPErrors = append(t.HTTPErrors, e)
}
return
}
code, fn := parseResponseArgs(val, args...)
if code == 0 {
code = expr.StatusOK
}
resp := &expr.HTTPResponseExpr{
StatusCode: code,
Parent: t,
}
if fn != nil {
eval.Execute(fn, resp)
}
t.Responses = append(t.Responses, resp)
case *expr.GRPCServiceExpr:
if !ok {
eval.InvalidArgError("name of error", val)
return
}
if e := grpcError(name, t, args...); e != nil {
t.GRPCErrors = append(t.GRPCErrors, e)
}
case *expr.GRPCEndpointExpr:
if ok {
// error response
if e := grpcError(name, t, args...); e != nil {
t.GRPCErrors = append(t.GRPCErrors, e)
}
return
}
code, fn := parseResponseArgs(val, args...)
resp := &expr.GRPCResponseExpr{
StatusCode: code,
Parent: t,
}
if fn != nil {
eval.Execute(fn, resp)
}
t.Response = resp
default:
eval.IncompatibleDSL()
}
}
// Code sets the Response status code.
//
// Code must appear in a Response expression.
//
// Code accepts one argument: the HTTP or gRPC status code.
func Code(code int) {
switch t := eval.Current().(type) {
case *expr.HTTPResponseExpr:
t.StatusCode = code
case *expr.GRPCResponseExpr:
t.StatusCode = code
default:
eval.IncompatibleDSL()
}
}
func grpcError(n string, p eval.Expression, args ...interface{}) *expr.GRPCErrorExpr {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
return nil
}
var (
code int
fn func()
val interface{}
)
val = args[0]
args = args[1:]
code, fn = parseResponseArgs(val, args...)
if code == 0 {
code = CodeUnknown
}
resp := &expr.GRPCResponseExpr{
StatusCode: code,
Parent: p,
}
if fn != nil {
eval.Execute(fn, resp)
}
return &expr.GRPCErrorExpr{
Name: n,
Response: resp,
}
}
func parseResponseArgs(val interface{}, args ...interface{}) (code int, fn func()) {
switch t := val.(type) {
case int:
code = t
if len(args) > 1 {
eval.ReportError("too many arguments given to Response (%d)", len(args)+1)
return
}
if len(args) == 1 {
if d, ok := args[0].(func()); ok {
fn = d
} else {
eval.InvalidArgError("function", args[0])
return
}
}
case func():
if len(args) > 0 {
eval.InvalidArgError("int (HTTP status code)", val)
return
}
fn = t
default:
eval.InvalidArgError("int (HTTP status code) or function", val)
return
}
return
}
func httpError(n string, p eval.Expression, args ...interface{}) *expr.HTTPErrorExpr {
if len(args) == 0 {
eval.ReportError("not enough arguments, use Response(name, status), Response(name, status, func()) or Response(name, func())")
return nil
}
var (
code int
fn func()
val interface{}
)
val = args[0]
args = args[1:]
code, fn = parseResponseArgs(val, args...)
if code == 0 {
code = expr.StatusBadRequest
}
resp := &expr.HTTPResponseExpr{
StatusCode: code,
Parent: p,
}
if fn != nil {
eval.Execute(fn, resp)
}
return &expr.HTTPErrorExpr{
Name: n,
Response: resp,
}
}