-
Notifications
You must be signed in to change notification settings - Fork 230
/
codegen.go
412 lines (392 loc) · 11.7 KB
/
codegen.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package ai
import (
"bytes"
"context"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"golang.org/x/exp/maps"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
"encr.dev/cli/daemon/apps"
"encr.dev/internal/env"
"encr.dev/pkg/fns"
"encr.dev/pkg/paths"
"encr.dev/v2/codegen/rewrite"
"encr.dev/v2/internals/perr"
"encr.dev/v2/internals/pkginfo"
"encr.dev/v2/parser/apis/api/apienc"
"encr.dev/v2/parser/apis/directive"
)
const defAuthHandler = `package auth
import (
"context"
"encore.dev/beta/auth"
)
type Data struct {
Username string
}
//encore:authhandler
func AuthHandler(ctx context.Context, token string) (auth.UID, *Data, error) {
panic("not yet implemented")
}`
const (
PathDocPrefix = "Path Parameters"
ErrDocPrefix = "Errors"
)
func (p PathSegments) Render() (docPath string, goParams []string) {
var params []string
return "/" + path.Join(fns.Map(p, func(s PathSegment) string {
switch s.Type {
case SegmentTypeLiteral:
return *s.Value
case SegmentTypeParam:
params = append(params, fmt.Sprintf("%s %s", *s.Value, *s.ValueType))
return fmt.Sprintf(":%s", *s.Value)
case SegmentTypeWildcard:
params = append(params, fmt.Sprintf("%s %s", *s.Value, SegmentValueTypeString))
return fmt.Sprintf("*%s", *s.Value)
case SegmentTypeFallback:
params = append(params, fmt.Sprintf("%s %s", *s.Value, SegmentValueTypeString))
return fmt.Sprintf("!%s", *s.Value)
default:
panic(fmt.Sprintf("unknown path segment type: %s", s.Type))
}
})...), params
}
func (s *Type) Render() string {
rtn := strings.Builder{}
if s.Doc != "" {
rtn.WriteString(fmtComment(strings.TrimSpace(s.Doc), 0, 1))
}
rtn.WriteString(fmt.Sprintf("type %s struct {\n", s.Name))
for i, f := range s.Fields {
if i > 0 {
rtn.WriteString("\n")
}
if f.Doc != "" {
rtn.WriteString(fmtComment(strings.TrimSpace(f.Doc), 2, 1))
}
tags := ""
switch f.Location {
case apienc.Body:
tags = fmt.Sprintf(" `json:\"%s\"`", f.WireName)
case apienc.Query:
tags = fmt.Sprintf(" `query:\"%s\"`", f.WireName)
case apienc.Header:
tags = fmt.Sprintf(" `header:\"%s\"`", f.WireName)
}
rtn.WriteString(fmt.Sprintf(" %s %s%s\n", f.Name, f.Type, tags))
}
rtn.WriteString("}")
return rtn.String()
}
func (e *Endpoint) Render() string {
buf := strings.Builder{}
if e.Doc != "" {
buf.WriteString(fmtComment(strings.TrimSpace(e.Doc)+"\n", 0, 1))
}
buf.WriteString(renderDocList(PathDocPrefix, e.Path))
buf.WriteString(renderDocList(ErrDocPrefix, e.Errors))
pathStr, pathParams := e.Path.Render()
params := []string{"ctx context.Context"}
params = append(params, pathParams...)
if e.RequestType != "" {
params = append(params, "req *"+e.RequestType)
}
var rtnParams []string
if e.ResponseType != "" {
rtnParams = append(rtnParams, "*"+e.ResponseType)
}
rtnParams = append(rtnParams, "error")
buf.WriteString(fmtComment("encore:api %s method=%s path=%s", 0, 0, e.Visibility, e.Method, pathStr))
paramsStr := strings.Join(params, ", ")
rtnParamsStr := strings.Join(rtnParams, ", ")
if len(rtnParams) > 1 {
rtnParamsStr = fmt.Sprintf("(%s)", rtnParamsStr)
}
buf.WriteString(fmt.Sprintf("func %s(%s) %s", e.Name, paramsStr, rtnParamsStr))
return buf.String()
}
func indentItem(header, comment string) string {
buf := strings.Builder{}
buf.WriteString(header)
for i, line := range strings.Split(strings.TrimSpace(comment), "\n") {
indent := ""
if i > 0 {
indent = strings.Repeat(" ", len(header))
}
buf.WriteString(fmt.Sprintf("%s%s\n", indent, line))
}
return buf.String()
}
func renderDocList[T interface{ DocItem() (string, string) }](header string, items []T) string {
maxLen := 0
items = fns.Filter(items, func(p T) bool {
key, val := p.DocItem()
if val == "" {
return false
}
maxLen = max(maxLen, len(key))
return true
})
buf := strings.Builder{}
for i, item := range items {
if i == 0 {
buf.WriteString(header)
buf.WriteString(":\n")
}
key, value := item.DocItem()
spacing := strings.Repeat(" ", maxLen-len(key))
itemHeader := fmt.Sprintf(" - %s: %s", key, spacing)
buf.WriteString(indentItem(itemHeader, value))
}
return fmtComment(buf.String(), 0, 1)
}
// fmtComment prepends '//' to each line of the given comment and indents it with the given number of spaces.
func fmtComment(comment string, before, after int, args ...any) string {
if comment == "" {
return ""
}
prefix := fmt.Sprintf("%s//%s", strings.Repeat(" ", before), strings.Repeat(" ", after))
result := prefix + strings.ReplaceAll(comment, "\n", "\n"+prefix)
return fmt.Sprintf(result, args...) + "\n"
}
// generateSrcFiles generates source files for the given services.
func generateSrcFiles(services []Service, app *apps.Instance) (map[paths.RelSlash]string, error) {
svcPaths, err := newServicePaths(app)
if err != nil {
return nil, err
}
needAuth := fns.Any(fns.FlatMap(services, Service.GetEndpoints), (*Endpoint).Auth)
files := map[paths.RelSlash]string{}
if needAuth {
md, err := app.CachedMetadata()
if err != nil {
return nil, err
}
if md.AuthHandler == nil {
relFile, err := svcPaths.RelFileName("auth", "handler")
if err != nil {
return nil, err
}
file := paths.FS(app.Root()).JoinSlash(relFile)
err = os.MkdirAll(file.Dir().ToIO(), 0755)
if err != nil {
return nil, err
}
files[relFile] = string(defAuthHandler)
}
}
for _, s := range services {
if svcPaths.IsNew(s.Name) {
relFile, err := svcPaths.RelFileName(s.Name, s.Name)
if err != nil {
return nil, err
}
file := paths.FS(app.Root()).JoinSlash(relFile)
err = os.MkdirAll(file.Dir().ToIO(), 0755)
if err != nil {
return nil, err
}
files[relFile] = fmt.Sprintf("%spackage %s\n", fmtComment(s.Doc, 0, 1), strings.ToLower(s.Name))
}
for _, e := range s.Endpoints {
relFile, err := svcPaths.RelFileName(s.Name, e.Name)
if err != nil {
return nil, err
}
filePath := paths.FS(app.Root()).JoinSlash(relFile)
_, content := toSrcFile(filePath, s.Name, e.EndpointSource, e.TypeSource)
files[relFile], err = addMissingFuncBodies(content)
if err != nil {
return nil, err
}
}
}
return files, nil
}
// addMissingFuncBodies adds a panic statement to functions that are missing a body.
// This is used to generate a valid Go source file when the user has not implemented
// the body of the endpoint functions.
func addMissingFuncBodies(content []byte) (string, error) {
set := token.NewFileSet()
rewriter := rewrite.New(content, 0)
file, err := parser.ParseFile(set, "", content, parser.ParseComments|parser.AllErrors)
if err != nil {
return "", err
}
ast.Inspect(file, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.FuncDecl:
if n.Body != nil {
break
}
rewriter.Insert(n.End()-1, []byte(" {\n panic(\"not yet implemented\")\n}\n"))
}
return true
})
return string(rewriter.Data()), err
}
// writeFiles writes the generated source files to disk.
func writeFiles(services []Service, app *apps.Instance) ([]paths.RelSlash, error) {
files, err := generateSrcFiles(services, app)
if err != nil {
return nil, err
}
for fileName, content := range files {
root := paths.FS(app.Root())
err = os.WriteFile(root.JoinSlash(fileName).ToIO(), []byte(content), 0644)
if err != nil {
return nil, err
}
}
return maps.Keys(files), nil
}
// toSrcFile wraps a code fragment in a package declaration and adds missing imports
// using the goimports tool.
func toSrcFile(filePath paths.FS, svc string, srcs ...string) (offset token.Position, data []byte) {
const divider = "// @code-start\n"
header := fmt.Sprintf("package %s\n\n", strings.ToLower(svc))
src := []byte(header + divider + strings.Join(srcs, "\n"))
importedSrc, err := imports.Process(filePath.ToIO(), src, &imports.Options{
Comments: true,
TabIndent: false,
TabWidth: 4,
})
// We don't need to handle the error here, as we'll catch parser/scanner errors in a later
// phase. This is just a best effort to import missing packages.
if err == nil {
src = importedSrc
}
codeOffset := bytes.Index(src, []byte(divider))
// Remove the divider and any formatting made by the imports tool
src = append(src[:codeOffset], strings.Join(srcs, "\n")...)
// Compute offset of the user defined code
lines := strings.Split(string(src[:codeOffset]), "\n")
return token.Position{
Filename: filePath.ToIO(),
Offset: codeOffset,
Line: len(lines) - 1,
Column: 0,
}, src
}
// updateCode updates the source code fields of the EndpointInputs in the given services.
// if overwrite is set, the code will be regenerated from scratch and replace the existing code,
// otherwise, we'll modify the code in place
func updateCode(ctx context.Context, services []Service, app *apps.Instance, overwrite bool) (rtn *SyncResult, err error) {
overlays, err := newOverlays(app, overwrite, services...)
fset := token.NewFileSet()
perrs := perr.NewList(ctx, fset, overlays.ReadFile)
defer func() {
perr.CatchBailout(recover())
if rtn == nil {
rtn = &SyncResult{
Services: services,
}
}
rtn.Errors = overlays.validationErrors(perrs)
}()
for p, olay := range overlays.items {
astFile, err := parser.ParseFile(fset, p.ToIO(), olay.content, parser.ParseComments|parser.AllErrors)
if err != nil {
perrs.AddStd(err)
}
rewriter := rewrite.New(olay.content, int(astFile.FileStart))
typeByName := map[string]*ast.GenDecl{}
funcByName := map[string]*ast.FuncDecl{}
for _, decl := range astFile.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
if decl.Tok != token.TYPE {
continue
}
for _, spec := range decl.Specs {
typeSpec := spec.(*ast.TypeSpec)
typeByName[typeSpec.Name.Name] = decl
}
case *ast.FuncDecl:
funcByName[decl.Name.Name] = decl
}
}
if olay.codeType == CodeTypeEndpoint {
funcDecl, ok := funcByName[olay.endpoint.Name]
if !ok {
for _, f := range funcByName {
dir, _, _ := directive.Parse(perrs, f.Doc)
if dir != nil && dir.Name == "api" {
funcDecl = f
break
}
}
}
if funcDecl != nil {
start := funcDecl.Pos()
if funcDecl.Doc != nil {
start = funcDecl.Doc.Pos()
}
end := funcDecl.End()
if funcDecl.Body != nil {
end = funcDecl.Body.Lbrace
}
rewriter.Replace(start, end, []byte(olay.endpoint.Render()))
} else {
if len(funcByName) > 0 {
rewriter.Append([]byte("\n"))
}
rewriter.Append([]byte(olay.endpoint.Render()))
}
olay.content = rewriter.Data()
content := string(olay.content[olay.headerOffset.Offset:])
olay.endpoint.EndpointSource = strings.TrimSpace(content)
} else {
for _, typ := range olay.endpoint.Types {
typeSpec := typeByName[typ.Name]
code := typ.Render()
if typeSpec != nil {
start := typeSpec.Pos()
if typeSpec.Doc != nil {
start = typeSpec.Doc.Pos()
}
rewriter.Replace(start, typeSpec.End(), []byte(code))
} else {
rewriter.Append([]byte("\n\n" + code))
}
}
olay.content = rewriter.Data()
content := string(olay.content[olay.headerOffset.Offset:])
olay.endpoint.TypeSource = strings.TrimSpace(content)
}
}
goRoot := paths.RootedFSPath(env.EncoreGoRoot(), ".")
pkginfo.UpdateGoPath(goRoot)
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedTypes | packages.NeedSyntax,
Dir: app.Root(),
Env: append(os.Environ(),
"GOOS="+runtime.GOOS,
"GOARCH="+runtime.GOARCH,
"GOROOT="+goRoot.ToIO(),
"PATH="+goRoot.Join("bin").ToIO()+string(filepath.ListSeparator)+os.Getenv("PATH"),
),
Fset: fset,
Overlay: overlays.PkgOverlay(),
}, fns.Map(overlays.pkgPaths(), paths.Pkg.String)...)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
for _, err := range pkg.Errors {
perrs.AddStd(err)
}
}
return &SyncResult{
Services: services,
}, nil
}