forked from goadesign/examples
/
types.go
439 lines (405 loc) · 15.3 KB
/
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Code generated by goa v3.13.1, DO NOT EDIT.
//
// storage HTTP server types
//
// Command:
// $ goa gen github.com/ikawaha/examples/cellar/design
package server
import (
"unicode/utf8"
storage "github.com/ikawaha/examples/cellar/gen/storage"
storageviews "github.com/ikawaha/examples/cellar/gen/storage/views"
goa "goa.design/goa/v3/pkg"
)
// AddRequestBody is the type of the "storage" service "add" endpoint HTTP
// request body.
type AddRequestBody struct {
// Name of bottle
Name *string `form:"name,omitempty" json:"name,omitempty" xml:"name,omitempty"`
// Winery that produces wine
Winery *WineryRequestBody `form:"winery,omitempty" json:"winery,omitempty" xml:"winery,omitempty"`
// Vintage of bottle
Vintage *uint32 `form:"vintage,omitempty" json:"vintage,omitempty" xml:"vintage,omitempty"`
// Composition is the list of grape varietals and associated percentage.
Composition []*ComponentRequestBody `form:"composition,omitempty" json:"composition,omitempty" xml:"composition,omitempty"`
// Description of bottle
Description *string `form:"description,omitempty" json:"description,omitempty" xml:"description,omitempty"`
// Rating of bottle from 1 (worst) to 5 (best)
Rating *uint32 `form:"rating,omitempty" json:"rating,omitempty" xml:"rating,omitempty"`
}
// MultiUpdateRequestBody is the type of the "storage" service "multi_update"
// endpoint HTTP request body.
type MultiUpdateRequestBody struct {
// Array of bottle info that matches the ids attribute
Bottles []*BottleRequestBody `form:"bottles,omitempty" json:"bottles,omitempty" xml:"bottles,omitempty"`
}
// StoredBottleResponseTinyCollection is the type of the "storage" service
// "list" endpoint HTTP response body.
type StoredBottleResponseTinyCollection []*StoredBottleResponseTiny
// ShowResponseBody is the type of the "storage" service "show" endpoint HTTP
// response body.
type ShowResponseBody struct {
// ID is the unique id of the bottle.
ID string `form:"id" json:"id" xml:"id"`
// Name of bottle
Name string `form:"name" json:"name" xml:"name"`
// Winery that produces wine
Winery *WineryResponseBodyTiny `form:"winery" json:"winery" xml:"winery"`
// Vintage of bottle
Vintage uint32 `form:"vintage" json:"vintage" xml:"vintage"`
// Composition is the list of grape varietals and associated percentage.
Composition []*ComponentResponseBody `form:"composition,omitempty" json:"composition,omitempty" xml:"composition,omitempty"`
// Description of bottle
Description *string `form:"description,omitempty" json:"description,omitempty" xml:"description,omitempty"`
// Rating of bottle from 1 (worst) to 5 (best)
Rating *uint32 `form:"rating,omitempty" json:"rating,omitempty" xml:"rating,omitempty"`
}
// ShowResponseBodyTiny is the type of the "storage" service "show" endpoint
// HTTP response body.
type ShowResponseBodyTiny struct {
// ID is the unique id of the bottle.
ID string `form:"id" json:"id" xml:"id"`
// Name of bottle
Name string `form:"name" json:"name" xml:"name"`
// Winery that produces wine
Winery *WineryResponseBodyTiny `form:"winery" json:"winery" xml:"winery"`
}
// ShowNotFoundResponseBody is the type of the "storage" service "show"
// endpoint HTTP response body for the "not_found" error.
type ShowNotFoundResponseBody struct {
// Message of error
Message string `form:"message" json:"message" xml:"message"`
// ID of missing bottle
ID string `form:"id" json:"id" xml:"id"`
}
// StoredBottleResponseTiny is used to define fields on response body types.
type StoredBottleResponseTiny struct {
// ID is the unique id of the bottle.
ID string `form:"id" json:"id" xml:"id"`
// Name of bottle
Name string `form:"name" json:"name" xml:"name"`
// Winery that produces wine
Winery *WineryResponseTiny `form:"winery" json:"winery" xml:"winery"`
}
// WineryResponseTiny is used to define fields on response body types.
type WineryResponseTiny struct {
// Name of winery
Name string `form:"name" json:"name" xml:"name"`
}
// WineryResponseBodyTiny is used to define fields on response body types.
type WineryResponseBodyTiny struct {
// Name of winery
Name string `form:"name" json:"name" xml:"name"`
}
// ComponentResponseBody is used to define fields on response body types.
type ComponentResponseBody struct {
// Grape varietal
Varietal string `form:"varietal" json:"varietal" xml:"varietal"`
// Percentage of varietal in wine
Percentage *uint32 `form:"percentage,omitempty" json:"percentage,omitempty" xml:"percentage,omitempty"`
}
// WineryRequestBody is used to define fields on request body types.
type WineryRequestBody struct {
// Name of winery
Name *string `form:"name,omitempty" json:"name,omitempty" xml:"name,omitempty"`
// Region of winery
Region *string `form:"region,omitempty" json:"region,omitempty" xml:"region,omitempty"`
// Country of winery
Country *string `form:"country,omitempty" json:"country,omitempty" xml:"country,omitempty"`
// Winery website URL
URL *string `form:"url,omitempty" json:"url,omitempty" xml:"url,omitempty"`
}
// ComponentRequestBody is used to define fields on request body types.
type ComponentRequestBody struct {
// Grape varietal
Varietal *string `form:"varietal,omitempty" json:"varietal,omitempty" xml:"varietal,omitempty"`
// Percentage of varietal in wine
Percentage *uint32 `form:"percentage,omitempty" json:"percentage,omitempty" xml:"percentage,omitempty"`
}
// BottleRequestBody is used to define fields on request body types.
type BottleRequestBody struct {
// Name of bottle
Name *string `form:"name,omitempty" json:"name,omitempty" xml:"name,omitempty"`
// Winery that produces wine
Winery *WineryRequestBody `form:"winery,omitempty" json:"winery,omitempty" xml:"winery,omitempty"`
// Vintage of bottle
Vintage *uint32 `form:"vintage,omitempty" json:"vintage,omitempty" xml:"vintage,omitempty"`
// Composition is the list of grape varietals and associated percentage.
Composition []*ComponentRequestBody `form:"composition,omitempty" json:"composition,omitempty" xml:"composition,omitempty"`
// Description of bottle
Description *string `form:"description,omitempty" json:"description,omitempty" xml:"description,omitempty"`
// Rating of bottle from 1 (worst) to 5 (best)
Rating *uint32 `form:"rating,omitempty" json:"rating,omitempty" xml:"rating,omitempty"`
}
// NewStoredBottleResponseTinyCollection builds the HTTP response body from the
// result of the "list" endpoint of the "storage" service.
func NewStoredBottleResponseTinyCollection(res storageviews.StoredBottleCollectionView) StoredBottleResponseTinyCollection {
body := make([]*StoredBottleResponseTiny, len(res))
for i, val := range res {
body[i] = marshalStorageviewsStoredBottleViewToStoredBottleResponseTiny(val)
}
return body
}
// NewShowResponseBody builds the HTTP response body from the result of the
// "show" endpoint of the "storage" service.
func NewShowResponseBody(res *storageviews.StoredBottleView) *ShowResponseBody {
body := &ShowResponseBody{
ID: *res.ID,
Name: *res.Name,
Vintage: *res.Vintage,
Description: res.Description,
Rating: res.Rating,
}
if res.Winery != nil {
body.Winery = marshalStorageviewsWineryViewToWineryResponseBodyTiny(res.Winery)
}
if res.Composition != nil {
body.Composition = make([]*ComponentResponseBody, len(res.Composition))
for i, val := range res.Composition {
body.Composition[i] = marshalStorageviewsComponentViewToComponentResponseBody(val)
}
}
return body
}
// NewShowResponseBodyTiny builds the HTTP response body from the result of the
// "show" endpoint of the "storage" service.
func NewShowResponseBodyTiny(res *storageviews.StoredBottleView) *ShowResponseBodyTiny {
body := &ShowResponseBodyTiny{
ID: *res.ID,
Name: *res.Name,
}
if res.Winery != nil {
body.Winery = marshalStorageviewsWineryViewToWineryResponseBodyTiny(res.Winery)
}
return body
}
// NewShowNotFoundResponseBody builds the HTTP response body from the result of
// the "show" endpoint of the "storage" service.
func NewShowNotFoundResponseBody(res *storage.NotFound) *ShowNotFoundResponseBody {
body := &ShowNotFoundResponseBody{
Message: res.Message,
ID: res.ID,
}
return body
}
// NewShowPayload builds a storage service show endpoint payload.
func NewShowPayload(id string, view *string) *storage.ShowPayload {
v := &storage.ShowPayload{}
v.ID = id
v.View = view
return v
}
// NewAddBottle builds a storage service add endpoint payload.
func NewAddBottle(body *AddRequestBody) *storage.Bottle {
v := &storage.Bottle{
Name: *body.Name,
Vintage: *body.Vintage,
Description: body.Description,
Rating: body.Rating,
}
v.Winery = unmarshalWineryRequestBodyToStorageWinery(body.Winery)
if body.Composition != nil {
v.Composition = make([]*storage.Component, len(body.Composition))
for i, val := range body.Composition {
v.Composition[i] = unmarshalComponentRequestBodyToStorageComponent(val)
}
}
return v
}
// NewRemovePayload builds a storage service remove endpoint payload.
func NewRemovePayload(id string) *storage.RemovePayload {
v := &storage.RemovePayload{}
v.ID = id
return v
}
// NewMultiAddBottle builds a storage service multi_add endpoint payload.
func NewMultiAddBottle(body []*BottleRequestBody) []*storage.Bottle {
v := make([]*storage.Bottle, len(body))
for i, val := range body {
v[i] = unmarshalBottleRequestBodyToStorageBottle(val)
}
return v
}
// NewMultiUpdatePayload builds a storage service multi_update endpoint payload.
func NewMultiUpdatePayload(body *MultiUpdateRequestBody, ids []string) *storage.MultiUpdatePayload {
v := &storage.MultiUpdatePayload{}
v.Bottles = make([]*storage.Bottle, len(body.Bottles))
for i, val := range body.Bottles {
v.Bottles[i] = unmarshalBottleRequestBodyToStorageBottle(val)
}
v.Ids = ids
return v
}
// ValidateAddRequestBody runs the validations defined on AddRequestBody
func ValidateAddRequestBody(body *AddRequestBody) (err error) {
if body.Name == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("name", "body"))
}
if body.Winery == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("winery", "body"))
}
if body.Vintage == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("vintage", "body"))
}
if body.Name != nil {
if utf8.RuneCountInString(*body.Name) > 100 {
err = goa.MergeErrors(err, goa.InvalidLengthError("body.name", *body.Name, utf8.RuneCountInString(*body.Name), 100, false))
}
}
if body.Winery != nil {
if err2 := ValidateWineryRequestBody(body.Winery); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
if body.Vintage != nil {
if *body.Vintage < 1900 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.vintage", *body.Vintage, 1900, true))
}
}
if body.Vintage != nil {
if *body.Vintage > 2020 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.vintage", *body.Vintage, 2020, false))
}
}
for _, e := range body.Composition {
if e != nil {
if err2 := ValidateComponentRequestBody(e); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
if body.Description != nil {
if utf8.RuneCountInString(*body.Description) > 2000 {
err = goa.MergeErrors(err, goa.InvalidLengthError("body.description", *body.Description, utf8.RuneCountInString(*body.Description), 2000, false))
}
}
if body.Rating != nil {
if *body.Rating < 1 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.rating", *body.Rating, 1, true))
}
}
if body.Rating != nil {
if *body.Rating > 5 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.rating", *body.Rating, 5, false))
}
}
return
}
// ValidateMultiUpdateRequestBody runs the validations defined on
// multi_update_request_body
func ValidateMultiUpdateRequestBody(body *MultiUpdateRequestBody) (err error) {
if body.Bottles == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("bottles", "body"))
}
for _, e := range body.Bottles {
if e != nil {
if err2 := ValidateBottleRequestBody(e); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
return
}
// ValidateWineryRequestBody runs the validations defined on WineryRequestBody
func ValidateWineryRequestBody(body *WineryRequestBody) (err error) {
if body.Name == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("name", "body"))
}
if body.Region == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("region", "body"))
}
if body.Country == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("country", "body"))
}
if body.Region != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("body.region", *body.Region, "[a-zA-Z '\\.]+"))
}
if body.Country != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("body.country", *body.Country, "[a-zA-Z '\\.]+"))
}
if body.URL != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("body.url", *body.URL, "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$"))
}
return
}
// ValidateComponentRequestBody runs the validations defined on
// ComponentRequestBody
func ValidateComponentRequestBody(body *ComponentRequestBody) (err error) {
if body.Varietal == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("varietal", "body"))
}
if body.Varietal != nil {
err = goa.MergeErrors(err, goa.ValidatePattern("body.varietal", *body.Varietal, "[A-Za-z' ]+"))
}
if body.Varietal != nil {
if utf8.RuneCountInString(*body.Varietal) > 100 {
err = goa.MergeErrors(err, goa.InvalidLengthError("body.varietal", *body.Varietal, utf8.RuneCountInString(*body.Varietal), 100, false))
}
}
if body.Percentage != nil {
if *body.Percentage < 1 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.percentage", *body.Percentage, 1, true))
}
}
if body.Percentage != nil {
if *body.Percentage > 100 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.percentage", *body.Percentage, 100, false))
}
}
return
}
// ValidateBottleRequestBody runs the validations defined on BottleRequestBody
func ValidateBottleRequestBody(body *BottleRequestBody) (err error) {
if body.Name == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("name", "body"))
}
if body.Winery == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("winery", "body"))
}
if body.Vintage == nil {
err = goa.MergeErrors(err, goa.MissingFieldError("vintage", "body"))
}
if body.Name != nil {
if utf8.RuneCountInString(*body.Name) > 100 {
err = goa.MergeErrors(err, goa.InvalidLengthError("body.name", *body.Name, utf8.RuneCountInString(*body.Name), 100, false))
}
}
if body.Winery != nil {
if err2 := ValidateWineryRequestBody(body.Winery); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
if body.Vintage != nil {
if *body.Vintage < 1900 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.vintage", *body.Vintage, 1900, true))
}
}
if body.Vintage != nil {
if *body.Vintage > 2020 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.vintage", *body.Vintage, 2020, false))
}
}
for _, e := range body.Composition {
if e != nil {
if err2 := ValidateComponentRequestBody(e); err2 != nil {
err = goa.MergeErrors(err, err2)
}
}
}
if body.Description != nil {
if utf8.RuneCountInString(*body.Description) > 2000 {
err = goa.MergeErrors(err, goa.InvalidLengthError("body.description", *body.Description, utf8.RuneCountInString(*body.Description), 2000, false))
}
}
if body.Rating != nil {
if *body.Rating < 1 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.rating", *body.Rating, 1, true))
}
}
if body.Rating != nil {
if *body.Rating > 5 {
err = goa.MergeErrors(err, goa.InvalidRangeError("body.rating", *body.Rating, 5, false))
}
}
return
}