-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathexample_svc.go
171 lines (159 loc) Β· 5.24 KB
/
example_svc.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
package service
import (
"os"
"path"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/expr"
)
type (
// basicEndpointData contains the data needed to render a basic endpoint
// implementation in the example service file.
basicEndpointData struct {
*MethodData
// ServiceVarName is the service variable name.
ServiceVarName string
// PayloadFullRef is the fully qualified reference to the payload.
PayloadFullRef string
// ResultFullName is the fully qualified name of the result.
ResultFullName string
// ResultFullRef is the fully qualified reference to the result.
ResultFullRef string
// ResultIsStruct indicates that the result type is a struct.
ResultIsStruct bool
// ResultView is the view to render the result. It is set only if the
// result type uses views.
ResultView string
// StreamInterface is the stream interface in the service package used
// by the endpoint implementation.
StreamInterface string
}
)
// ExampleServiceFiles returns a basic service implementation for every
// service expression.
func ExampleServiceFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
// determine the unique API package name different from the service names
scope := codegen.NewNameScope()
for _, svc := range root.Services {
s := Services.Get(svc.Name)
if s == nil {
panic("unknown service, " + svc.Name) // bug
}
scope.Unique(s.PkgName)
}
apipkg := scope.Unique(strings.ToLower(codegen.Goify(root.API.Name, false)), "api")
var fw []*codegen.File
for _, svc := range root.Services {
if f := exampleServiceFile(genpkg, root, svc, apipkg); f != nil {
fw = append(fw, f)
}
}
return fw
}
// exampleServiceFile returns a basic implementation of the given service.
func exampleServiceFile(genpkg string, root *expr.RootExpr, svc *expr.ServiceExpr, apipkg string) *codegen.File {
data := Services.Get(svc.Name)
svcName := codegen.SnakeCase(data.VarName)
fpath := svcName + ".go"
if _, err := os.Stat(fpath); !os.IsNotExist(err) {
return nil // file already exists, skip it.
}
specs := []*codegen.ImportSpec{
{Path: "context"},
{Path: "log"},
{Path: "fmt"},
{Path: path.Join(genpkg, codegen.SnakeCase(svcName)), Name: data.PkgName},
{Path: "goa.design/goa/security"},
}
sections := []*codegen.SectionTemplate{
codegen.Header("", apipkg, specs),
{Name: "basic-service-struct", Source: svcStructT, Data: data},
{Name: "basic-service-init", Source: svcInitT, Data: data},
}
if len(data.Schemes) > 0 {
sections = append(sections, &codegen.SectionTemplate{
Name: "security-authfuncs",
Source: dummyAuthFuncsT,
Data: data,
})
}
for _, m := range svc.Methods {
sections = append(sections, basicEndpointSection(m, data))
}
return &codegen.File{
Path: fpath,
SectionTemplates: sections,
SkipExist: true,
}
}
// basicEndpointSection returns a section with a basic implementation for the
// given method.
func basicEndpointSection(m *expr.MethodExpr, svcData *Data) *codegen.SectionTemplate {
md := svcData.Method(m.Name)
ed := &basicEndpointData{
MethodData: md,
ServiceVarName: svcData.VarName,
}
if m.Payload.Type != expr.Empty {
ed.PayloadFullRef = svcData.Scope.GoFullTypeRef(m.Payload, svcData.PkgName)
}
if m.Result.Type != expr.Empty {
ed.ResultFullName = svcData.Scope.GoFullTypeName(m.Result, svcData.PkgName)
ed.ResultFullRef = svcData.Scope.GoFullTypeRef(m.Result, svcData.PkgName)
ed.ResultIsStruct = expr.IsObject(m.Result.Type)
if md.ViewedResult != nil {
view := "default"
if m.Result.Meta != nil {
if v, ok := m.Result.Meta["view"]; ok {
view = v[0]
}
}
ed.ResultView = view
}
}
if md.ServerStream != nil {
ed.StreamInterface = svcData.PkgName + "." + md.ServerStream.Interface
}
return &codegen.SectionTemplate{
Name: "basic-endpoint",
Source: endpointT,
Data: ed,
}
}
const (
// input: service.Data
svcStructT = `{{ printf "%s service example implementation.\nThe example methods log the requests and return zero values." .Name | comment }}
type {{ .VarName }}srvc struct {
logger *log.Logger
}
`
// input: service.Data
svcInitT = `{{ printf "New%s returns the %s service implementation." .StructName .Name | comment }}
func New{{ .StructName }}(logger *log.Logger) {{ .PkgName }}.Service {
return &{{ .VarName }}srvc{logger}
}
`
// input: basicEndpointData
endpointT = `{{ comment .Description }}
{{- if .ServerStream }}
func (s *{{ .ServiceVarName }}srvc) {{ .VarName }}(ctx context.Context{{ if .PayloadFullRef }}, p {{ .PayloadFullRef }}{{ end }}, stream {{ .StreamInterface }}) (err error) {
{{- else }}
func (s *{{ .ServiceVarName }}srvc) {{ .VarName }}(ctx context.Context{{ if .PayloadFullRef }}, p {{ .PayloadFullRef }}{{ end }}) ({{ if .ResultFullRef }}res {{ .ResultFullRef }}, {{ if .ViewedResult }}{{ if not .ViewedResult.ViewName }}view string, {{ end }}{{ end }} {{ end }}err error) {
{{- end }}
{{- if and (and .ResultFullRef .ResultIsStruct) (not .ServerStream) }}
res = &{{ .ResultFullName }}{}
{{- end }}
{{- if .ViewedResult }}
{{- if not .ViewedResult.ViewName }}
{{- if .ServerStream }}
stream.SetView({{ printf "%q" .ResultView }})
{{- else }}
view = {{ printf "%q" .ResultView }}
{{- end }}
{{- end }}
{{- end }}
s.logger.Print("{{ .ServiceVarName }}.{{ .Name }}")
return
}
`
)