forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
71 lines (63 loc) · 1.76 KB
/
example.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
package generator
import (
"goa.design/goa/codegen"
"goa.design/goa/codegen/example"
"goa.design/goa/codegen/service"
"goa.design/goa/eval"
"goa.design/goa/expr"
grpccodegen "goa.design/goa/grpc/codegen"
httpcodegen "goa.design/goa/http/codegen"
)
// Example iterates through the roots and returns files that implement an
// example service, server, and client.
func Example(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
var files []*codegen.File
for _, root := range roots {
r, ok := root.(*expr.RootExpr)
if !ok {
continue // could be a plugin root expression
}
// example service implementation
if fs := service.ExampleServiceFiles(genpkg, r); len(fs) != 0 {
files = append(files, fs...)
}
// example auth file
if f := service.AuthFuncsFile(genpkg, r); f != nil {
files = append(files, f)
}
// server main
if fs := example.ServerFiles(genpkg, r); len(fs) != 0 {
files = append(files, fs...)
}
// CLI main
if fs := example.CLIFiles(genpkg, r); len(fs) != 0 {
files = append(files, fs...)
}
// HTTP
if len(r.API.HTTP.Services) > 0 {
if fs := httpcodegen.ExampleServerFiles(genpkg, r); len(fs) != 0 {
files = append(files, fs...)
}
if fs := httpcodegen.ExampleCLIFiles(genpkg, r); len(fs) != 0 {
files = append(files, fs...)
}
}
// GRPC
if len(r.API.GRPC.Services) > 0 {
if fs := grpccodegen.ExampleServerFiles(genpkg, r); len(fs) > 0 {
files = append(files, fs...)
}
if fs := grpccodegen.ExampleCLIFiles(genpkg, r); len(fs) > 0 {
files = append(files, fs...)
}
}
for _, f := range files {
if len(f.SectionTemplates) > 0 {
for _, s := range r.Services {
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s)
}
}
}
}
return files, nil
}