forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
295 lines (263 loc) · 6.89 KB
/
root.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
package expr
import (
"sort"
"goa.design/goa/eval"
)
// Root is the root object built by the DSL.
var Root = &RootExpr{GeneratedTypes: &GeneratedRoot{}}
type (
// RootExpr is the struct built by the DSL on process start.
RootExpr struct {
// API contains the API expression built by the DSL.
API *APIExpr
// Services contains the list of services exposed by the API.
Services []*ServiceExpr
// Errors contains the list of errors returned by all the API
// methods.
Errors []*ErrorExpr
// Types contains the user types described in the DSL.
Types []UserType
// ResultTypes contains the result types described in the DSL.
ResultTypes []UserType
// GeneratedTypes contains the types generated during DSL
// execution.
GeneratedTypes *GeneratedRoot
// Conversions list the user type to external type mappings.
Conversions []*TypeMap
// Creations list the external type to user type mappings.
Creations []*TypeMap
// Schemes list the registered security schemes.
Schemes []*SchemeExpr
}
// MetaExpr is a set of key/value pairs
MetaExpr map[string][]string
// TypeMap defines a user to external type mapping.
TypeMap struct {
// User is the user type being converted or created.
User UserType
// External is an instance of the type being converted from or to.
External interface{}
}
)
// WalkSets returns the expressions in order of evaluation.
func (r *RootExpr) WalkSets(walk eval.SetWalker) {
if r.API == nil {
name := "API"
if len(r.Services) > 0 {
name = r.Services[0].Name
}
r.API = NewAPIExpr(name, func() {})
}
// Top level API DSL
walk(eval.ExpressionSet{r.API})
// Servers
servers := make(eval.ExpressionSet, len(r.API.Servers))
for i, s := range r.API.Servers {
servers[i] = s
}
walk(servers)
// User types
types := make(eval.ExpressionSet, len(r.Types))
for i, t := range r.Types {
types[i] = t.Attribute()
}
walk(types)
// Result types
mtypes := make(eval.ExpressionSet, len(r.ResultTypes))
for i, mt := range r.ResultTypes {
mtypes[i] = mt.(*ResultTypeExpr)
}
walk(mtypes)
// Services
services := make(eval.ExpressionSet, len(r.Services))
var methods eval.ExpressionSet
for i, s := range r.Services {
services[i] = s
}
walk(services)
// Methods (must be done after services)
for _, s := range r.Services {
for _, m := range s.Methods {
methods = append(methods, m)
}
}
walk(methods)
// HTTP services and endpoints
httpsvcs := make(eval.ExpressionSet, len(r.API.HTTP.Services))
sort.SliceStable(r.API.HTTP.Services, func(i, j int) bool {
return r.API.HTTP.Services[j].ParentName == r.API.HTTP.Services[i].Name()
})
var httpepts eval.ExpressionSet
var httpsvrs eval.ExpressionSet
for i, svc := range r.API.HTTP.Services {
httpsvcs[i] = svc
for _, e := range svc.HTTPEndpoints {
httpepts = append(httpepts, e)
}
for _, s := range svc.FileServers {
httpsvrs = append(httpsvrs, s)
}
}
walk(eval.ExpressionSet{r.API.HTTP})
walk(httpsvcs)
walk(httpepts)
walk(httpsvrs)
// GRPC services and endpoints
grpcsvcs := make(eval.ExpressionSet, len(r.API.GRPC.Services))
sort.SliceStable(r.API.GRPC.Services, func(i, j int) bool {
return r.API.GRPC.Services[j].ParentName == r.API.GRPC.Services[i].Name()
})
var grpcepts eval.ExpressionSet
for i, svc := range r.API.GRPC.Services {
grpcsvcs[i] = svc
for _, e := range svc.GRPCEndpoints {
grpcepts = append(grpcepts, e)
}
}
walk(eval.ExpressionSet{r.API.GRPC})
walk(grpcsvcs)
walk(grpcepts)
}
// DependsOn returns nil, the core DSL has no dependency.
func (r *RootExpr) DependsOn() []eval.Root { return nil }
// Packages returns the Go import path to this and the dsl packages.
func (r *RootExpr) Packages() []string {
return []string{
"goa.design/goa/expr",
"goa.design/goa/dsl",
}
}
// UserType returns the user type expression with the given name if found, nil otherwise.
func (r *RootExpr) UserType(name string) UserType {
for _, t := range r.Types {
if t.Name() == name {
return t
}
}
for _, t := range r.ResultTypes {
if t.Name() == name {
return t
}
}
return nil
}
// GeneratedResultType returns the generated result type expression with the given
// id, nil if there isn't one.
func (r *RootExpr) GeneratedResultType(id string) *ResultTypeExpr {
for _, t := range *r.GeneratedTypes {
mt := t.(*ResultTypeExpr)
if mt.Identifier == id {
return mt
}
}
return nil
}
// Service returns the service with the given name.
func (r *RootExpr) Service(name string) *ServiceExpr {
for _, s := range r.Services {
if s.Name == name {
return s
}
}
return nil
}
// Error returns the error with the given name.
func (r *RootExpr) Error(name string) *ErrorExpr {
for _, e := range r.Errors {
if e.Name == name {
return e
}
}
return nil
}
// HTTPService returns the HTTP service with the given name if any.
func (r *RootExpr) HTTPService(name string) *HTTPServiceExpr {
for _, res := range r.API.HTTP.Services {
if res.Name() == name {
return res
}
}
return nil
}
// HTTPServiceFor creates a new or returns the existing HTTP service definition
// for the given service.
func (r *RootExpr) HTTPServiceFor(s *ServiceExpr) *HTTPServiceExpr {
if res := r.HTTPService(s.Name); res != nil {
return res
}
res := &HTTPServiceExpr{
ServiceExpr: s,
}
r.API.HTTP.Services = append(r.API.HTTP.Services, res)
return res
}
// EvalName is the name of the DSL.
func (r *RootExpr) EvalName() string {
return "design"
}
// Validate makes sure the root expression is valid for code generation.
func (r *RootExpr) Validate() error {
var verr eval.ValidationErrors
if r.API == nil {
verr.Add(r, "Missing API declaration")
}
return &verr
}
// Finalize finalizes the server expressions.
func (r *RootExpr) Finalize() {
if r.API == nil {
r.API = &APIExpr{}
}
if len(r.API.Servers) == 0 {
r.API.Servers = []*ServerExpr{r.API.DefaultServer()}
}
for _, s := range r.API.Servers {
s.Finalize()
}
}
// Dup creates a new map from the given expression.
func (m MetaExpr) Dup() MetaExpr {
d := make(MetaExpr, len(m))
for k, v := range m {
d[k] = v
}
return d
}
// Merge merges src meta expression with m. If meta has intersecting set of
// keys on both m and src, then the values for those keys in src is appended
// to the values of the keys in m if not already existing.
func (m MetaExpr) Merge(src MetaExpr) {
for k, vals := range src {
if mvals, ok := m[k]; ok {
var found bool
for _, v := range vals {
found = false
for _, mv := range mvals {
if mv == v {
found = true
break
}
}
if !found {
mvals = append(mvals, v)
}
}
m[k] = mvals
} else {
m[k] = vals
}
}
}
// Last returns the last value for a specific key, if the key exists and has
// values; otherwise returns an empty string, with the "ok" flag set to false.
func (m MetaExpr) Last(key string) (string, bool) {
v, ok := m[key]
if !ok {
return "", false
}
l := len(v)
if l < 1 {
return "", false
}
return v[l-1], true
}