forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
147 lines (144 loc) · 3.68 KB
/
result.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
package dsl
import (
"goa.design/goa/v3/eval"
"goa.design/goa/v3/expr"
)
// Result defines the data type of a method output.
//
// Result must appear in a Method expression.
//
// Result 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 DSL may also specify a
// view when the first argument is a result type corresponding to the view
// rendered by this method. If no view is specified then the generated code
// defines response methods for all views.
//
// The valid syntax for Result is thus:
//
// Result(Type)
//
// Result(func())
//
// Result(Type, "description")
//
// Result(Type, func())
//
// Result(Type, "description", func())
//
// Examples:
//
// // Define result using primitive type
// Method("add", func() {
// Result(Int32)
// })
//
// // Define result using primitive type and description
// Method("add", func() {
// Result(Int32, "Resulting sum")
// })
//
// // Define result using primitive type, description and validations.
// Method("add", func() {
// Result(Int32, "Resulting sum", func() {
// Minimum(0)
// })
// })
//
// // Define result using object defined inline
// Method("add", func() {
// Result(func() {
// Description("Result defines a single field which is the sum.")
// Attribute("value", Int32, "Resulting sum")
// Required("value")
// })
// })
//
// // Define result type using user type
// Method("add", func() {
// Result(Sum)
// })
//
// // Specify view and required attributes on result type
// Method("add", func() {
// Result(Sum, func() {
// View("default")
// Required("value")
// })
// })
func Result(val any, args ...any) {
if len(args) > 2 {
eval.ReportError("too many arguments")
return
}
e, ok := eval.Current().(*expr.MethodExpr)
if !ok {
eval.IncompatibleDSL()
return
}
e.Result = methodDSL(e, "Result", val, args...)
}
// StreamingResult defines a method that streams instances of the given type.
//
// StreamingResult must appear in a Method expression.
//
// The arguments to a StreamingResult DSL is same as the Result DSL.
//
// Examples:
//
// // Method result is a stream of integers
// Method("add", func() {
// StreamingResult(Int32)
// })
//
// Method("add", func() {
// StreamingResult(Int32, "Resulting sum")
// })
//
// // Method result is a stream of integers with validation set on each
// Method("add", func() {
// StreamingResult(Int32, "Resulting sum", func() {
// Minimum(0)
// })
// })
//
// // Method result is a stream of objects defined inline
// Method("add", func() {
// StreamingResult(func() {
// Description("Result defines a single field which is the sum.")
// Attribute("value", Int32, "Resulting sum")
// Required("value")
// })
// })
//
// // Method result is a stream of user type
// Method("add", func() {
// StreamingResult(Sum)
// })
//
// // Method result is a stream of result type with a view
// Method("add", func() {
// StreamingResult(Sum, func() {
// View("default")
// Required("value")
// })
// })
func StreamingResult(val any, args ...any) {
if len(args) > 2 {
eval.ReportError("too many arguments")
return
}
e, ok := eval.Current().(*expr.MethodExpr)
if !ok {
eval.IncompatibleDSL()
return
}
e.Result = methodDSL(e, "Result", val, args...)
if e.Stream == expr.ClientStreamKind {
e.Stream = expr.BidirectionalStreamKind
} else {
e.Stream = expr.ServerStreamKind
}
}