forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_body_types.go
333 lines (307 loc) · 9.03 KB
/
http_body_types.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package expr
import (
"net/http"
"strings"
"unicode"
)
// httpRequestBody returns an attribute describing the HTTP request body of the
// given endpoint. If the DSL defines a body explicitly via the Body function
// then the corresponding attribute is used. Otherwise the attribute is computed
// by removing the attributes of the method payload used to define headers and
// parameters.
func httpRequestBody(a *HTTPEndpointExpr) *AttributeExpr {
const suffix = "RequestBody"
var (
name = concat(a.Name(), "Request", "Body")
)
if a.Body != nil {
a.Body = DupAtt(a.Body)
renameType(a.Body, name, suffix)
return a.Body
}
var (
payload = a.MethodExpr.Payload
headers = a.Headers
params = a.Params
userField string
passField string
)
{
obj := AsObject(payload.Type)
if obj != nil {
for _, at := range *obj {
if _, ok := at.Attribute.Meta["security:username"]; ok {
userField = at.Name
}
if _, ok := at.Attribute.Meta["security:password"]; ok {
passField = at.Name
}
if userField != "" && passField != "" {
break
}
}
}
}
bodyOnly := headers.IsEmpty() && params.IsEmpty() && a.MapQueryParams == nil
// 1. If Payload is not an object then check whether there are params or
// headers defined and if so return empty type (payload encoded in
// request params or headers) otherwise return payload type (payload
// encoded in request body).
if !IsObject(payload.Type) {
if bodyOnly {
payload = DupAtt(payload)
renameType(payload, name, suffix)
return payload
}
return &AttributeExpr{Type: Empty}
}
// 2. Remove header and param attributes
body := NewMappedAttributeExpr(payload)
removeAttributes(body, headers)
removeAttributes(body, params)
if a.MapQueryParams != nil && *a.MapQueryParams != "" {
removeAttribute(body, *a.MapQueryParams)
}
if userField != "" {
removeAttribute(body, userField)
}
if passField != "" {
removeAttribute(body, passField)
}
// 3. Return empty type if no attribute left
if len(*AsObject(body.Type)) == 0 {
return &AttributeExpr{Type: Empty}
}
// 4. Build computed user type
att := body.Attribute()
ut := &UserTypeExpr{
AttributeExpr: att,
TypeName: name,
}
appendSuffix(ut.Attribute().Type, suffix)
return &AttributeExpr{
Type: ut,
Validation: att.Validation,
UserExamples: att.UserExamples,
}
}
// httpStreamingBody returns an attribute representing the structs being
// streamed via websocket.
func httpStreamingBody(e *HTTPEndpointExpr) *AttributeExpr {
if !e.MethodExpr.IsStreaming() || e.MethodExpr.Stream == ServerStreamKind {
return nil
}
att := e.MethodExpr.StreamingPayload
if !IsObject(att.Type) {
return DupAtt(att)
}
const suffix = "StreamingBody"
ut := &UserTypeExpr{
AttributeExpr: DupAtt(att),
TypeName: concat(e.Name(), "Streaming", "Body"),
}
appendSuffix(ut.Attribute().Type, suffix)
return &AttributeExpr{
Type: ut,
Validation: att.Validation,
UserExamples: att.UserExamples,
}
}
// httpResponseBody returns an attribute representing the HTTP response body for
// the given endpoint and response. If the DSL defines a body explicitly via the
// Body function then the corresponding attribute is used. Otherwise the
// attribute is computed by removing the attributes of the method payload used
// to define headers.
func httpResponseBody(a *HTTPEndpointExpr, resp *HTTPResponseExpr) *AttributeExpr {
var name, suffix string
if len(a.Responses) > 1 {
suffix = http.StatusText(resp.StatusCode)
}
name = a.Name() + suffix
return buildHTTPResponseBody(name, a.MethodExpr.Result, resp)
}
// httpErrorResponseBody returns an attribute describing the response body of a
// given error. If the DSL defines a body explicitly via the Body function then
// the corresponding attribute is returned. Otherwise the attribute is computed
// by removing the attributes of the error used to define headers and
// parameters.
func httpErrorResponseBody(a *HTTPEndpointExpr, v *HTTPErrorExpr) *AttributeExpr {
name := a.Name() + "_" + v.ErrorExpr.Name
return buildHTTPResponseBody(name, v.ErrorExpr.AttributeExpr, v.Response)
}
func buildHTTPResponseBody(name string, attr *AttributeExpr, resp *HTTPResponseExpr) *AttributeExpr {
const suffix = "ResponseBody"
name = concat(name, "Response", "Body")
if attr == nil || attr.Type == Empty {
return &AttributeExpr{Type: Empty}
}
if resp.Body != nil {
return resp.Body
}
// 1. If attribute is not an object then check whether there are headers
// defined and if so return empty type (attr encoded in response
// headers) otherwise return renamed attr type (attr encoded in
// response body).
if !IsObject(attr.Type) {
if resp.Headers.IsEmpty() {
attr = DupAtt(attr)
renameType(attr, name, "Response") // Do not use ResponseBody as it could clash with name of element
return attr
}
return &AttributeExpr{Type: Empty}
}
// 2. Remove header attributes
body := NewMappedAttributeExpr(attr)
removeAttributes(body, resp.Headers)
// 3. Return empty type if no attribute left
if len(*AsObject(body.Type)) == 0 {
return &AttributeExpr{Type: Empty}
}
// 4. Build computed user type
userType := &UserTypeExpr{
AttributeExpr: body.Attribute(),
TypeName: name,
}
appendSuffix(userType.Attribute().Type, suffix)
rt, isrt := attr.Type.(*ResultTypeExpr)
if !isrt {
return &AttributeExpr{
Type: userType,
Validation: userType.Validation,
Meta: attr.Meta,
}
}
views := make([]*ViewExpr, len(rt.Views))
for i, v := range rt.Views {
mv := NewMappedAttributeExpr(v.AttributeExpr)
removeAttributes(mv, resp.Headers)
nv := &ViewExpr{
AttributeExpr: mv.Attribute(),
Name: v.Name,
}
views[i] = nv
}
nmt := &ResultTypeExpr{
UserTypeExpr: userType,
Identifier: rt.Identifier,
ContentType: rt.ContentType,
Views: views,
}
for _, v := range views {
v.Parent = nmt
}
return &AttributeExpr{
Type: nmt,
Validation: userType.Validation,
Meta: attr.Meta,
}
}
// buildBodyTypeName concatenates the given strings to generate the
// endpoint's body type name.
//
// The concatenation algorithm is:
// 1) If the first string contains underscores and starts with a lower case,
// the rest of the strings are converted to lower case and concatenated with
// underscores.
// e.g. concat("my_endpoint", "Request", "BODY") => "my_endpoint_request_body"
// 2) If the first string contains underscores and starts with a upper case,
// the rest of the strings are converted to title case and concatenated with
// underscores.
// e.g. concat("My_endpoint", "response", "body") => "My_endpoint_Response_Body"
// 3) If the first string is a single word or camelcased, the rest of the
// strings are concatenated to form a valid upper camelcase.
// e.g. concat("myEndpoint", "streaming", "Body") => "MyEndpointStreamingBody"
//
func concat(strs ...string) string {
if len(strs) == 1 {
return strs[0]
}
// hasUnderscore returns true if the string has at least one underscore.
hasUnderscore := func(str string) bool {
for i := 0; i < len(str); i++ {
if rune(str[i]) == '_' {
return true
}
}
return false
}
// isLower returns true if the first letter in the screen is lower-case.
isLower := func(str string) bool {
return unicode.IsLower(rune(str[0]))
}
name := strs[0]
switch {
case isLower(name) && hasUnderscore(name):
for i := 1; i < len(strs); i++ {
name += "_" + strings.ToLower(strs[i])
}
case !isLower(name) && hasUnderscore(name):
for i := 1; i < len(strs); i++ {
name += "_" + strings.Title(strs[i])
}
default:
name = strings.Title(name)
for i := 1; i < len(strs); i++ {
name += strings.Title(strs[i])
}
}
return name
}
func renameType(att *AttributeExpr, name, suffix string) {
rt := att.Type
switch rtt := rt.(type) {
case UserType:
rtt.Rename(name)
appendSuffix(rtt.Attribute().Type, suffix)
case *Object:
appendSuffix(rt, suffix)
case *Array:
appendSuffix(rt, suffix)
case *Map:
appendSuffix(rt, suffix)
}
}
func appendSuffix(dt DataType, suffix string, seen ...map[string]struct{}) {
var s map[string]struct{}
if len(seen) > 0 {
s = seen[0]
} else {
s = make(map[string]struct{})
seen = append(seen, s)
}
switch actual := dt.(type) {
case UserType:
if _, ok := s[actual.ID()]; ok {
return
}
actual.Rename(actual.Name() + suffix)
s[actual.ID()] = struct{}{}
appendSuffix(actual.Attribute().Type, suffix, seen...)
case *Object:
for _, nat := range *actual {
appendSuffix(nat.Attribute.Type, suffix, seen...)
}
case *Array:
appendSuffix(actual.ElemType.Type, suffix, seen...)
case *Map:
appendSuffix(actual.KeyType.Type, suffix, seen...)
appendSuffix(actual.ElemType.Type, suffix, seen...)
}
}
func removeAttributes(attr, sub *MappedAttributeExpr) {
o := AsObject(sub.Type)
for _, nat := range *o {
removeAttribute(attr, nat.Name)
}
}
func removeAttribute(attr *MappedAttributeExpr, name string) {
attr.Delete(name)
if attr.Validation != nil {
attr.Validation.RemoveRequired(name)
}
for _, ex := range attr.UserExamples {
if m, ok := ex.Value.(map[string]interface{}); ok {
delete(m, name)
}
}
}