forked from goadesign/plugins
/
generate.go
194 lines (181 loc) · 6.83 KB
/
generate.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
package goakit
import (
"path"
"regexp"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/eval"
"goa.design/goa/expr"
httpcodegen "goa.design/goa/http/codegen"
)
// Register the plugin Generator functions.
func init() {
codegen.RegisterPluginFirst("goakit", "gen", nil, Generate)
codegen.RegisterPluginLast("goakit-goakitify", "gen", nil, Goakitify)
codegen.RegisterPluginLast("goakit-goakitify-example", "example", nil, GoakitifyExample)
}
// Generate generates go-kit specific decoders and encoders.
func Generate(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error) {
for _, root := range roots {
if r, ok := root.(*expr.RootExpr); ok {
files = append(files, EncodeDecodeFiles(genpkg, r)...)
files = append(files, MountFiles(r)...)
}
}
return files, nil
}
// Goakitify modifies all the previously generated files by adding go-kit
// imports and replacing the following instances
// * "goa.Endpoint" with "github.com/go-kit/kit/endpoint".Endpoint
// * "log.Logger" with "github.com/go-kit/kit/log".Logger
//
// Goakitify also wraps instances of endpoint.Endpoint into instances of
// goa.Endpoint when used as argument of either goagrpc.NewStreamHandler or
// goagrpc.NewUnaryHandler.
func Goakitify(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error) {
for _, f := range files {
goakitify(f)
}
return files, nil
}
func goakitify(f *codegen.File) {
var hasEndpoint bool
for _, s := range f.SectionTemplates {
if !hasEndpoint {
hasEndpoint = goaEndpointRegexp.MatchString(s.Source)
}
s.Source = goaEndpointRegexp.ReplaceAllString(s.Source, "${1}endpoint.Endpoint${2}")
if s.Name == "grpc-handler-init" {
s.Source = strings.Replace(s.Source, "Handler(endpoint, ", "Handler(goa.Endpoint(endpoint), ", 1)
}
}
if hasEndpoint {
codegen.AddImport(
f.SectionTemplates[0],
&codegen.ImportSpec{Path: "github.com/go-kit/kit/endpoint"},
)
}
}
// GoakitifyExample modifies all the previously generated example files by
// adding go-kit imports.
func GoakitifyExample(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error) {
for _, f := range files {
gokitifyExampleServer(genpkg, f)
}
return files, nil
}
// goaEndpointRegexp matches occurrences of the "goa.Endpoint" type in Go code.
var goaEndpointRegexp = regexp.MustCompile(`([^\p{L}_])goa\.Endpoint([^\p{L}_])`)
// goaLoggerRegexp matches occurrences of "logger.<function>" in Go code.
var goaLoggerRegexp = regexp.MustCompile(`logger\.\w+\((.*)\)`)
// gokitifyExampleServer imports gokit endpoint, logger, and transport
// packages in the example server implementation. It also replaces every stdlib
// logger with gokit logger.
func gokitifyExampleServer(genpkg string, file *codegen.File) {
goakitify(file)
var hasLogger bool
for _, s := range file.SectionTemplates {
if !hasLogger {
hasLogger = strings.Contains(s.Source, "*log.Logger")
}
s.Source = strings.Replace(s.Source, "*log.Logger", "log.Logger", -1)
codegen.AddImport(file.SectionTemplates[0], &codegen.ImportSpec{Path: "fmt"})
s.Source = goaLoggerRegexp.ReplaceAllString(s.Source, "logger.Log(\"info\", fmt.Sprintf(${1}))")
switch s.Name {
case "server-main-logger":
codegen.AddImport(file.SectionTemplates[0], &codegen.ImportSpec{Path: "github.com/go-kit/kit/log"})
s.Source = gokitLoggerT
case "server-http-logger":
s.Source = ""
case "server-http-middleware":
s.Source = strings.Replace(s.Source, "adapter", "logger", -1)
case "server-http-init":
codegen.AddImport(file.SectionTemplates[0], &codegen.ImportSpec{Path: "github.com/go-kit/kit/transport/http", Name: "kithttp"})
codegen.AddImport(file.SectionTemplates[0], &codegen.ImportSpec{Path: "github.com/go-kit/kit/endpoint"})
data := s.Data.(map[string]interface{})
svcs := data["Services"].([]*httpcodegen.ServiceData)
for _, svc := range svcs {
pkgName := httpcodegen.HTTPServices.Get(svc.Service.Name).Service.PkgName
codegen.AddImport(file.SectionTemplates[0], &codegen.ImportSpec{
Path: path.Join(genpkg, "http", svc.Service.Name, "kitserver"),
Name: pkgName + "kitsvr",
})
}
s.Source = gokitServerInitT
}
}
if hasLogger {
// Replace existing stdlib logger with gokit logger in imports
if data, ok := file.SectionTemplates[0].Data.(map[string]interface{}); ok {
if imports, ok := data["Imports"]; ok {
specs := imports.([]*codegen.ImportSpec)
for _, s := range specs {
if s.Path == "log" {
s.Path = "github.com/go-kit/kit/log"
}
}
}
}
}
}
const gokitLoggerT = `
// Setup gokit logger.
var (
logger log.Logger
)
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
`
const gokitServerInitT = `
// Wrap the endpoints with the transport specific layers. The generated
// server packages contains code generated from the design which maps
// the service input and output data structures to HTTP requests and
// responses.
var (
{{- range .Services }}
{{- range .Endpoints }}
{{ .ServiceVarName }}{{ .Method.VarName }}Handler *kithttp.Server
{{- end }}
{{ .Service.VarName }}Server *{{.Service.PkgName}}svr.Server
{{- end }}
)
{
eh := errorHandler(logger)
{{- if needStream .Services }}
upgrader := &websocket.Upgrader{}
{{- end }}
{{- range .Services }}
{{- if .Endpoints }}
{{- range .Endpoints }}
{{ .ServiceVarName }}{{ .Method.VarName }}Handler = kithttp.NewServer(
endpoint.Endpoint({{ .ServiceVarName }}Endpoints.{{ .Method.VarName }}),
{{- if .Payload.Ref }}
{{ .ServicePkgName}}kitsvr.{{ .RequestDecoder }}(mux, dec),
{{- else }}
func(context.Context, *http.Request) (request interface{}, err error) { return nil, nil },
{{- end }}
{{ .ServicePkgName}}kitsvr.{{ .ResponseEncoder }}(enc),
{{- if .Errors }}
kithttp.ServerErrorEncoder({{ .ServicePkgName}}kitsvr.{{ .ErrorEncoder }}(enc, nil)),
{{- end }}
)
{{- end }}
{{ .Service.VarName }}Server = {{ .Service.PkgName }}svr.New({{ .Service.VarName }}Endpoints, mux, dec, enc, eh, nil{{ if needStream $.Services }}, upgrader, nil{{ end }}{{ range .Endpoints }}{{ if .MultipartRequestDecoder }}, {{ $.APIPkg }}.{{ .MultipartRequestDecoder.FuncName }}{{ end }}{{ end }})
{{- else }}
{{ .Service.VarName }}Server = {{ .Service.PkgName }}svr.New(nil, mux, dec, enc, eh, nil)
{{- end }}
{{- end }}
}
// Configure the mux.
{{- range .Services }}{{ $service := . }}
{{- range .Endpoints }}
{{ .ServicePkgName}}kitsvr.{{ .MountHandler }}(mux, {{ .ServiceVarName }}{{ .Method.VarName }}Handler)
{{- end }}
{{- range .FileServers }}
{{ $service.Service.PkgName}}kitsvr.{{ .MountHandler }}(mux)
{{- end }}
{{- end }}
`