forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
185 lines (181 loc) · 4.92 KB
/
payload.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
package dsl
import (
"goa.design/goa/eval"
"goa.design/goa/expr"
)
// Payload defines the data type of an method input. Payload also makes the
// input required.
//
// Payload must appear in a Method expression.
//
// Payload takes one to three arguments. The first argument is either a type or
// a DSL function. If the first argument is a type then an optional description
// may be passed as second argument. Finally a DSL may be passed as last
// argument that further specializes the type by providing additional
// validations (e.g. list of required attributes)
//
// The valid usage for Payload are thus:
//
// Payload(Type)
//
// Payload(func())
//
// Payload(Type, "description")
//
// Payload(Type, func())
//
// Payload(Type, "description", func())
//
// Examples:
//
// Method("upper"), func() {
// // Use primitive type.
// Payload(String)
// }
//
// Method("upper"), func() {
// // Use primitive type.and description
// Payload(String, "string to convert to uppercase")
// }
//
// Method("upper"), func() {
// // Use primitive type, description and validations
// Payload(String, "string to convert to uppercase", func() {
// Pattern("^[a-z]")
// })
// }
//
// Method("add", func() {
// // Define payload data structure inline
// Payload(func() {
// Description("Left and right operands to add")
// Attribute("left", Int32, "Left operand")
// Attribute("right", Int32, "Left operand")
// Required("left", "right")
// })
// })
//
// Method("add", func() {
// // Define payload type by reference to user type
// Payload(Operands)
// })
//
// Method("divide", func() {
// // Specify additional required attributes on user type.
// Payload(Operands, func() {
// Required("left", "right")
// })
// })
//
func Payload(val interface{}, args ...interface{}) {
if len(args) > 2 {
eval.ReportError("too many arguments")
}
e, ok := eval.Current().(*expr.MethodExpr)
if !ok {
eval.IncompatibleDSL()
return
}
e.Payload = methodDSL("Payload", val, args...)
}
// StreamingPayload defines a method that accepts a stream of instances of the
// given type.
//
// StreamingPayload must appear in a Method expression.
//
// The arguments to a StreamingPayload DSL is same as the Payload DSL.
//
// Examples:
//
// // Method payload is the JWT token and the method streaming payload is a
// // stream of strings.
// Method("upper", func() {
// Payload(func() {
// Token("token", String, func() {
// Description("JWT used for authentication")
// })
// })
// StreamingPayload(String)
// })
//
// // Method streaming payload is a stream of string with validation set
// // on each
// Method("upper"), func() {
// StreamingPayload(String, "string to convert to uppercase", func() {
// Pattern("^[a-z]")
// })
// }
//
// // Method payload is a stream of objects defined inline
// Method("add", func() {
// StreamingPayload(func() {
// Description("Left and right operands to add")
// Attribute("left", Int32, "Left operand")
// Attribute("right", Int32, "Left operand")
// Required("left", "right")
// })
// })
//
// // Method payload is a stream of user type
// Method("add", func() {
// StreamingPayload(Operands)
// })
//
func StreamingPayload(val interface{}, args ...interface{}) {
if len(args) > 2 {
eval.ReportError("too many arguments")
}
e, ok := eval.Current().(*expr.MethodExpr)
if !ok {
eval.IncompatibleDSL()
return
}
e.StreamingPayload = methodDSL("StreamingPayload", val, args...)
if e.Stream == expr.ServerStreamKind {
e.Stream = expr.BidirectionalStreamKind
} else {
e.Stream = expr.ClientStreamKind
}
}
func methodDSL(suffix string, p interface{}, args ...interface{}) *expr.AttributeExpr {
var (
att *expr.AttributeExpr
fn func()
)
switch actual := p.(type) {
case func():
fn = actual
att = &expr.AttributeExpr{Type: &expr.Object{}}
case expr.UserType:
if len(args) == 0 {
// Do not duplicate type if it is not customized
return &expr.AttributeExpr{Type: actual}
}
att = &expr.AttributeExpr{Type: expr.Dup(actual)}
case expr.DataType:
att = &expr.AttributeExpr{Type: actual}
default:
eval.ReportError("invalid %s argument, must be a type or a function", suffix)
return nil
}
if len(args) >= 1 {
if f, ok := args[len(args)-1].(func()); ok {
if fn != nil {
eval.ReportError("invalid arguments in %s call, must be (type), (func), (type, func), (type, desc) or (type, desc, func)", suffix)
}
fn = f
}
if d, ok := args[0].(string); ok {
att.Description = d
}
}
if fn != nil {
eval.Execute(fn, att)
if obj, ok := att.Type.(*expr.Object); ok {
if len(*obj) == 0 {
att.Type = expr.Empty
}
}
}
return att
}