-
Notifications
You must be signed in to change notification settings - Fork 19
/
design.go
300 lines (289 loc) · 7.22 KB
/
design.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
package dsl
import (
"net/url"
"goa.design/goa/v3/eval"
"goa.design/model/expr"
// Register code generators for the model plugin
_ "goa.design/model/plugin"
)
// Design defines the architecture design containing the models and views.
// Design must appear exactly once.
//
// Design is a top-level DSL function.
//
// Design takes one to three arguments. The first argument is either a string
// or a function. If the first argument is a string then an optional description
// may be passed as second argument. The last argument must be a function that
// defines the models and views.
//
// The valid syntax for Design is thus:
//
// Design(func())
//
// Design("name", func())
//
// Design("name", "description", func())
//
// Examples:
//
// // Default workspace, no description
// var _ = Design(func() {
// SoftwareSystem("My Software System")
// })
//
// // Design with given name, no description
// var _ = Design("name", func() {
// SoftwareSystem("My Software System")
// })
//
// // Design with given name and description
// var _ = Design("My Design", "A great architecture.", func() {
// SoftwareSystem("My Software System")
// })
func Design(args ...any) *expr.Design {
_, ok := eval.Current().(eval.TopExpr)
if !ok {
eval.IncompatibleDSL()
return nil
}
nargs := len(args)
if nargs == 0 {
eval.ReportError("Design: missing child DSL")
return nil
}
dsl, ok := args[nargs-1].(func())
if !ok {
eval.ReportError("Design: missing child DSL (last argument must be a func)")
return nil
}
if nargs > 1 {
name, ok := args[0].(string)
if !ok {
eval.InvalidArgError("string", args[0])
}
expr.Root.Name = name
}
if nargs > 2 {
desc, ok := args[1].(string)
if !ok {
eval.InvalidArgError("string", args[1])
}
expr.Root.Description = desc
}
if nargs > 3 {
eval.ReportError("Design: too many arguments")
return nil
}
if !eval.Execute(dsl, expr.Root) {
return nil
}
return expr.Root
}
// Version specifies a version number for the design.
//
// Version must appear in a Design expression.
//
// Version takes exactly one argument: the version number.
//
// Example:
//
// var _ = Design(func() {
// Version("1.0")
// })
func Version(v string) {
w, ok := eval.Current().(*expr.Design)
if !ok {
eval.IncompatibleDSL()
} else {
w.Version = v
}
}
// Enterprise defines a named "enterprise" (e.g. an organisation). On System
// Landscape and System Context diagrams, an enterprise is represented as a
// dashed box. Only a single enterprise can be defined within a model.
//
// Enterprise must appear in a Design expression.
//
// Enterprise takes exactly one argument: the enterprise name.
//
// Example:
//
// var _ = Design(func() {
// Enterprise("Goa Design")
// })
func Enterprise(e string) {
w, ok := eval.Current().(*expr.Design)
if !ok {
eval.IncompatibleDSL()
} else {
w.Model.Enterprise = e
}
}
// AddImpliedRelationships creates implied relationships between all valid
// combinations of parent elements, unless the same relationship already exists
// between them. For example if a model includes two containers with one
// component each and the DSL defines a relationship between the two components
// (Component 1 to Component 2) then AddImpliedRelationships would add the
// following relationships:
//
// - Component 1 to Container 2
// - Container 1 to Component 2
// - Container 1 to Container 2
//
// AddImpliedRelationships must appear in Design.
func AddImpliedRelationships() {
w, ok := eval.Current().(*expr.Design)
if !ok {
eval.IncompatibleDSL()
} else {
w.Model.AddImpliedRelationships = true
}
}
// Tag defines a set of tags on the given element. Tags are used in views to
// identify group of elements that should be rendered together for example.
//
// Tag may appear in Person, SoftwareSystem, Container, Component,
// DeploymentNode, InfrastructureNode, ContainerInstance, Uses, InteractsWith or
// Delivers.
//
// Tag accepts the set of tag values as argument. Tag may appear multiple times
// in the same expression in which case the tags accumulate.
//
// Example:
//
// var _ = Design(func() {
// System("My system", func() {
// Tag("sharded", "critical")
// Tag("blue team")
// })
// })
func Tag(first string, t ...string) {
t = append([]string{first}, t...)
switch e := eval.Current().(type) {
case expr.ElementHolder:
e.GetElement().MergeTags(t...)
case *expr.Relationship:
e.MergeTags(t...)
default:
eval.IncompatibleDSL()
}
}
// URL where more information about this element can be found.
// Or URL of health check when used within a HealthCheck expression.
//
// URL may appear in Person, SoftwareSystem, Container, Component,
// DeploymentNode, InfrastructureNode or HealthCheck.
//
// URL takes exactly one argument: a valid URL.
//
// Example:
//
// var _ = Design(func() {
// System("My system", func() {
// URL("https://goa.design/docs/mysystem")
// })
// })
func URL(u string) {
if _, err := url.Parse(u); err != nil {
eval.ReportError("URL: invalid value %q: %s", u, err.Error())
}
switch e := eval.Current().(type) {
case *expr.Person:
e.URL = u
case *expr.SoftwareSystem:
e.URL = u
case *expr.Container:
e.URL = u
case *expr.Component:
e.URL = u
case *expr.DeploymentNode:
e.URL = u
case *expr.InfrastructureNode:
e.URL = u
case *expr.HealthCheck:
e.URL = u
default:
eval.IncompatibleDSL()
}
}
// External indicates the element is external to the enterprise.
//
// External may appear in Person or SoftwareSystem.
//
// Example:
//
// var _ = Design(func() {
// System("My system", func() {
// External()
// })
// })
func External() {
ext := expr.LocationExternal
switch e := eval.Current().(type) {
case *expr.Person:
e.Location = ext
case *expr.SoftwareSystem:
e.Location = ext
default:
eval.IncompatibleDSL()
}
}
// Prop defines arbitrary key-value pairs. They are shown in the diagram
// tooltip and can be used to store metadata (e.g. team name).
//
// Prop must appear in Person, SoftwareSystem, Container, Component,
// DeploymentNode, InfrastructureNode or ContainerInstance.
//
// Prop accepts two arguments: the name and value of a property.
//
// Example:
//
// var _ = Design(func() {
// SoftwareSystem("MySystem", func() {
// Prop("name", "value")
// })
// })
func Prop(name, value string) {
var props map[string]string
switch e := eval.Current().(type) {
case *expr.Person:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
case *expr.SoftwareSystem:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
case *expr.Container:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
case *expr.Component:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
case *expr.DeploymentNode:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
case *expr.InfrastructureNode:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
case *expr.ContainerInstance:
if e.Properties == nil {
e.Properties = make(map[string]string)
}
props = e.Properties
default:
eval.IncompatibleDSL()
return
}
props[name] = value
}