/
service.go
294 lines (276 loc) Β· 8.81 KB
/
service.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
package service
import (
"fmt"
"path/filepath"
"sort"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
// Files returns the generated files for the given service as well as a map
// indexing user type names by custom path as defined by the "struct:pkg:path"
// metadata. The map is built over each invocation of Files to avoid duplicate
// type definitions.
func Files(genpkg string, service *expr.ServiceExpr, userTypePkgs map[string][]string) []*codegen.File {
svc := Services.Get(service.Name)
svc.initUserTypeImports(genpkg)
svcName := svc.PathName
svcPath := filepath.Join(codegen.Gendir, svcName, "service.go")
seen := make(map[string]struct{})
typeDefSections := make(map[string]map[string]*codegen.SectionTemplate)
typesByPath := make(map[string][]string)
var svcSections []*codegen.SectionTemplate
addTypeDefSection := func(path, name string, section *codegen.SectionTemplate) {
if typeDefSections[path] == nil {
typeDefSections[path] = make(map[string]*codegen.SectionTemplate)
}
typeDefSections[path][name] = section
typesByPath[path] = append(typesByPath[path], name)
seen[name] = struct{}{}
}
for _, m := range svc.Methods {
payloadPath := pathWithDefault(m.PayloadLoc, svcPath)
resultPath := pathWithDefault(m.ResultLoc, svcPath)
if m.PayloadDef != "" {
if _, ok := seen[m.Payload]; !ok {
addTypeDefSection(payloadPath, m.Payload, &codegen.SectionTemplate{
Name: "service-payload",
Source: readTemplate("payload"),
Data: m,
})
}
}
if m.StreamingPayloadDef != "" {
if _, ok := seen[m.StreamingPayload]; !ok {
addTypeDefSection(payloadPath, m.StreamingPayload, &codegen.SectionTemplate{
Name: "service-streamig-payload",
Source: readTemplate("streaming_payload"),
Data: m,
})
}
}
if m.ResultDef != "" {
if _, ok := seen[m.Result]; !ok {
addTypeDefSection(resultPath, m.Result, &codegen.SectionTemplate{
Name: "service-result",
Source: readTemplate("result"),
Data: m,
})
}
}
}
for _, ut := range svc.userTypes {
if _, ok := seen[ut.VarName]; !ok {
addTypeDefSection(pathWithDefault(ut.Loc, svcPath), ut.VarName, &codegen.SectionTemplate{
Name: "service-user-type",
Source: readTemplate("user_type"),
Data: ut,
})
}
}
var errorTypes []*UserTypeData
seenErrs := make(map[string]struct{})
for _, et := range svc.errorTypes {
if et.Type == expr.ErrorResult {
continue
}
if _, ok := seenErrs[et.Name]; !ok {
seenErrs[et.Name] = struct{}{}
if _, ok := seen[et.Name]; !ok {
addTypeDefSection(pathWithDefault(et.Loc, svcPath), et.Name, &codegen.SectionTemplate{
Name: "error-user-type",
Source: readTemplate("user_type"),
Data: et,
})
}
errorTypes = append(errorTypes, et)
}
}
for _, m := range svc.unionValueMethods {
addTypeDefSection(pathWithDefault(m.Loc, svcPath), "~"+m.TypeRef+"."+m.Name, &codegen.SectionTemplate{
Name: "service-union-value-method",
Source: readTemplate("union_value_method"),
Data: m,
})
}
for _, et := range errorTypes {
// Don't override the section created for the error type
// declaration, make sure the key does not clash with existing
// type names, make it generated last.
key := "|" + et.Name
addTypeDefSection(pathWithDefault(et.Loc, svcPath), key, &codegen.SectionTemplate{
Name: "service-error",
Source: readTemplate("error"),
FuncMap: map[string]any{"errorName": errorName},
Data: et,
})
}
for _, er := range svc.errorInits {
svcSections = append(svcSections, &codegen.SectionTemplate{
Name: "error-init-func",
Source: readTemplate("error_init"),
Data: er,
})
}
// transform result type functions
for _, t := range svc.viewedResultTypes {
svcSections = append(svcSections, &codegen.SectionTemplate{
Name: "viewed-result-type-to-service-result-type",
Source: readTemplate("type_init"),
Data: t.ResultInit,
})
svcSections = append(svcSections, &codegen.SectionTemplate{
Name: "service-result-type-to-viewed-result-type",
Source: readTemplate("type_init"),
Data: t.Init,
})
}
var projh []*codegen.TransformFunctionData
for _, t := range svc.projectedTypes {
for _, i := range t.TypeInits {
projh = codegen.AppendHelpers(projh, i.Helpers)
svcSections = append(svcSections, &codegen.SectionTemplate{
Name: "projected-type-to-service-type",
Source: readTemplate("type_init"),
Data: i,
})
}
for _, i := range t.Projections {
projh = codegen.AppendHelpers(projh, i.Helpers)
svcSections = append(svcSections, &codegen.SectionTemplate{
Name: "service-type-to-projected-type",
Source: readTemplate("type_init"),
Data: i,
})
}
}
for _, h := range projh {
svcSections = append(svcSections, &codegen.SectionTemplate{
Name: "transform-helpers",
Source: readTemplate("transform_helper"),
Data: h,
})
}
imports := []*codegen.ImportSpec{
codegen.SimpleImport("context"),
codegen.SimpleImport("io"),
codegen.GoaImport(""),
codegen.GoaImport("security"),
codegen.NewImport(svc.ViewsPkg, genpkg+"/"+svcName+"/views"),
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" service", svc.PkgName, imports)
def := &codegen.SectionTemplate{
Name: "service",
Source: readTemplate("service"),
Data: svc,
FuncMap: map[string]any{"streamInterfaceFor": streamInterfaceFor},
}
// service.go
var sections []*codegen.SectionTemplate
{
sections = []*codegen.SectionTemplate{header, def}
names := make([]string, len(typeDefSections[svcPath]))
i := 0
for n := range typeDefSections[svcPath] {
names[i] = n
i++
}
sort.Strings(names)
for _, n := range names {
sections = append(sections, typeDefSections[svcPath][n])
}
sections = append(sections, svcSections...)
}
files := []*codegen.File{{Path: svcPath, SectionTemplates: sections}}
// user types
paths := make([]string, len(typeDefSections))
i := 0
for p := range typesByPath {
paths[i] = p
i++
}
sort.Strings(paths)
for _, p := range paths {
if p == svcPath {
continue
}
var secs []*codegen.SectionTemplate
ts := typesByPath[p]
sort.Strings(ts)
for _, name := range ts {
hasName := false
for _, n := range userTypePkgs[p] {
if hasName = n == name; hasName {
break
}
}
if hasName {
continue
}
userTypePkgs[p] = append(userTypePkgs[p], name)
secs = append(secs, typeDefSections[p][name])
}
if len(secs) == 0 {
continue
}
fullRelPath := filepath.Join(codegen.Gendir, p)
dir, _ := filepath.Split(fullRelPath)
h := codegen.Header("User types", codegen.Goify(filepath.Base(dir), false), nil)
sections := append([]*codegen.SectionTemplate{h}, secs...)
files = append(files, &codegen.File{Path: fullRelPath, SectionTemplates: sections})
}
return files
}
// AddServiceDataMetaTypeImports Adds all imports defined by struct:field:type from the service expr and the service data
func AddServiceDataMetaTypeImports(header *codegen.SectionTemplate, serviceE *expr.ServiceExpr) {
codegen.AddServiceMetaTypeImports(header, serviceE)
svc := Services.Get(serviceE.Name)
for _, ut := range svc.userTypes {
codegen.AddImport(header, codegen.GetMetaTypeImports(ut.Type.Attribute())...)
}
for _, et := range svc.errorTypes {
codegen.AddImport(header, codegen.GetMetaTypeImports(et.Type.Attribute())...)
}
for _, t := range svc.viewedResultTypes {
codegen.AddImport(header, codegen.GetMetaTypeImports(t.Type.Attribute())...)
}
for _, t := range svc.projectedTypes {
codegen.AddImport(header, codegen.GetMetaTypeImports(t.Type.Attribute())...)
}
}
func errorName(et *UserTypeData) string {
obj := expr.AsObject(et.Type)
if obj != nil {
for _, att := range *obj {
if _, ok := att.Attribute.Meta["struct:error:name"]; ok {
return fmt.Sprintf("e.%s", codegen.GoifyAtt(att.Attribute, att.Name, true))
}
}
}
// if error type is a custom user type and used by at most one error, then
// error Finalize should have added "struct:error:name" to the user type
// attribute's meta.
if v, ok := et.Type.Attribute().Meta["struct:error:name"]; ok {
return fmt.Sprintf("%q", v[0])
}
return fmt.Sprintf("%q", et.Name)
}
// streamInterfaceFor builds the data to generate the client and server stream
// interfaces for the given endpoint.
func streamInterfaceFor(typ string, m *MethodData, stream *StreamData) map[string]any {
return map[string]any{
"Type": typ,
"Endpoint": m.Name,
"Stream": stream,
// If a view is explicitly set (ViewName is not empty) in the Result
// expression, we can use that view to render the result type instead
// of iterating through the list of views defined in the result type.
"IsViewedResult": m.ViewedResult != nil && m.ViewedResult.ViewName == "",
}
}
func pathWithDefault(loc *codegen.Location, def string) string {
if loc == nil {
return def
}
return loc.FilePath
}