-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathuser_type.go
279 lines (268 loc) Β· 7.93 KB
/
user_type.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
package dsl
import (
"goa.design/goa/eval"
"goa.design/goa/expr"
)
var (
// ErrorResultIdentifier is the result type identifier used for error
// responses.
ErrorResultIdentifier = expr.ErrorResultIdentifier
// ErrorResult is the built-in result type for error responses.
ErrorResult = expr.ErrorResult
)
// Type defines a user type. A user type has a unique name and may be an alias
// to an existing type or may describe a completely new type using a list of
// attributes (object fields). Attribute types may themselves be user type.
// When a user type is defined as an alias to another type it may define
// additional validations - for example it a user type which is an alias of
// String may define a validation pattern that all instances of the type
// must match.
//
// Type is a top level definition.
//
// Type takes two or three arguments: the first argument is the name of the type.
// The name must be unique. The second argument is either another type or a
// function. If the second argument is a type then there may be a function passed
// as third argument.
//
// Example:
//
// // simple alias
// var MyString = Type("MyString", String)
//
// // alias with description and additional validation
// var Hostname = Type("Hostname", String, func() {
// Description("A host name")
// Format(FormatHostname)
// })
//
// // new type
// var SumPayload = Type("SumPayload", func() {
// Description("Type sent to add method")
//
// Attribute("a", String) // string attribute "a"
// Attribute("b", Int32, "operand") // attribute with description
// Attribute("operands", ArrayOf(Int32)) // array attribute
// Attribute("ops", MapOf(String, Int32)) // map attribute
// Attribute("c", SumMod) // attribute using user type
// Attribute("len", Int64, func() { // attribute with validation
// Minimum(1)
// })
//
// Required("a") // Required attributes
// Required("b", "c")
// })
//
func Type(name string, args ...interface{}) expr.UserType {
if len(args) > 2 {
eval.ReportError("too many arguments")
return nil
}
if t := expr.Root.UserType(name); t != nil {
eval.ReportError("type %#v defined twice", name)
return nil
}
if _, ok := eval.Current().(eval.TopExpr); !ok {
eval.IncompatibleDSL()
return nil
}
var (
base expr.DataType
fn func()
)
if len(args) == 0 {
// Make Type behave like Attribute
args = []interface{}{expr.String}
}
switch a := args[0].(type) {
case expr.DataType:
base = a
if len(args) == 2 {
d, ok := args[1].(func())
if !ok {
eval.ReportError("third argument must be a function")
return nil
}
fn = d
}
case func():
base = &expr.Object{}
fn = a
if len(args) == 2 {
eval.ReportError("only one argument allowed when it is a function")
return nil
}
default:
eval.InvalidArgError("type or function", args[0])
return nil
}
t := &expr.UserTypeExpr{
TypeName: name,
AttributeExpr: &expr.AttributeExpr{Type: base, DSLFunc: fn},
}
expr.Root.Types = append(expr.Root.Types, t)
return t
}
// ArrayOf creates an array type from its element type.
//
// ArrayOf may be used wherever types can.
// The first argument of ArrayOf is the type of the array elements specified by
// name or by reference.
// The second argument of ArrayOf is an optional function that defines
// validations for the array elements.
//
// Examples:
//
// var Names = ArrayOf(String, func() {
// Pattern("[a-zA-Z]+") // Validates elements of the array
// })
//
// var Account = Type("Account", func() {
// Attribute("bottles", ArrayOf(Bottle), "Account bottles", func() {
// MinLength(1) // Validates array as a whole
// })
// })
//
// Note: CollectionOf and ArrayOf both return array types. CollectionOf returns
// a result type where ArrayOf returns a user type. In general you want to use
// CollectionOf if the argument is a result type and ArrayOf if it is a user
// type.
func ArrayOf(v interface{}, fn ...func()) *expr.Array {
var t expr.DataType
var ok bool
t, ok = v.(expr.DataType)
if !ok {
if name, ok := v.(string); ok {
t = expr.Root.UserType(name)
}
}
// never return nil to avoid panics, errors are reported after DSL execution
if t == nil {
eval.ReportError("invalid ArrayOf argument: not a type and not a known user type name")
return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}}
}
if len(fn) > 1 {
eval.ReportError("ArrayOf: too many arguments")
return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}}
}
at := expr.AttributeExpr{Type: t}
if len(fn) == 1 {
eval.Execute(fn[0], &at)
}
return &expr.Array{ElemType: &at}
}
// MapOf creates a map from its key and element types.
//
// MapOf may be used wherever types can.
// MapOf takes two arguments: the key and value types either by name of by reference.
//
// Example:
//
// var ReviewByID = MapOf(Int64, String, func() {
// Key(func() {
// Minimum(1) // Validates keys of the map
// })
// Elem(func() {
// Pattern("[a-zA-Z]+") // Validates values of the map
// })
// })
//
// var Review = Type("Review", func() {
// Attribute("ratings", MapOf(Bottle, Int32), "Bottle ratings")
// })
//
func MapOf(k, v interface{}, fn ...func()) *expr.Map {
var tk, tv expr.DataType
var ok bool
tk, ok = k.(expr.DataType)
if !ok {
if name, ok := k.(string); ok {
tk = expr.Root.UserType(name)
}
}
tv, ok = v.(expr.DataType)
if !ok {
if name, ok := v.(string); ok {
tv = expr.Root.UserType(name)
}
}
// never return nil to avoid panics, errors are reported after DSL execution
if tk == nil {
eval.ReportError("invalid MapOf key argument: not a type and not a known user type name")
return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}}
}
if expr.IsMap(tk) {
eval.ReportError("invalid MapOf key type: key type must be a primitive, array, or user type")
return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}}
}
if tv == nil {
eval.ReportError("invalid MapOf value argument: not a type and not a known user type name")
return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}}
}
if len(fn) > 1 {
eval.ReportError("MapOf: too many arguments")
return &expr.Map{KeyType: &expr.AttributeExpr{Type: expr.String}, ElemType: &expr.AttributeExpr{Type: expr.String}}
}
kat := expr.AttributeExpr{Type: tk}
vat := expr.AttributeExpr{Type: tv}
m := &expr.Map{KeyType: &kat, ElemType: &vat}
if len(fn) == 1 {
mat := expr.AttributeExpr{Type: m}
eval.Execute(fn[0], &mat)
}
return m
}
// Key makes it possible to specify validations for map keys.
//
// Example:
//
// Attribute("map", MapOf(String, Int), func() {
// Key(func() {
// Format(FormatDateTime) // map keys are timestamps
// })
// })
//
func Key(fn func()) {
at, ok := eval.Current().(*expr.AttributeExpr)
if !ok {
eval.IncompatibleDSL()
return
}
if m, ok := at.Type.(*expr.Map); ok {
eval.Execute(fn, m.KeyType)
return
}
eval.IncompatibleDSL()
}
// Elem makes it possible to specify validations for array and map values.
//
// Example:
//
// Attribute("array", ArrayOf(Int), func() {
// Elem(func() {
// Enum(1, 2, 3, 4, 5) // list possible values for array elements
// })
// })
//
// Attribute("map", MapOf(String, Int), func() {
// Elem(func() {
// Minimum(1)
// Maximum(100)
// })
// })
//
func Elem(fn func()) {
at, ok := eval.Current().(*expr.AttributeExpr)
if !ok {
eval.IncompatibleDSL()
return
}
switch e := at.Type.(type) {
case *expr.Array:
eval.Execute(fn, e.ElemType)
case *expr.Map:
eval.Execute(fn, e.ElemType)
default:
eval.IncompatibleDSL()
}
}