-
Notifications
You must be signed in to change notification settings - Fork 320
/
decls.go
266 lines (223 loc) · 6.56 KB
/
decls.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
package codegen
import (
"fmt"
"io"
"slices"
"strings"
"github.com/dave/jennifer/jen"
"encr.dev/pkg/fns"
"encr.dev/pkg/paths"
"encr.dev/v2/internals/pkginfo"
)
var importNames = map[string]string{
"github.com/felixge/httpsnoop": "httpsnoop",
"github.com/json-iterator/go": "jsoniter",
"github.com/julienschmidt/httprouter": "httprouter",
"encore.dev/appruntime/apisdk/api": "__api",
"encore.dev/appruntime/apisdk/app": "__app",
"encore.dev/appruntime/infrasdk/config": "__config",
"encore.dev/appruntime/shared/etype": "__etype",
"encore.dev/appruntime/exported/model": "__model",
"encore.dev/appruntime/shared/serde": "__serde",
"encore.dev/appruntime/apisdk/service": "__service",
"encore.dev/beta/errs": "errs",
"encore.dev/storage/sqldb": "sqldb",
"encore.dev/types/uuid": "uuid",
}
func newFile(pkg *pkginfo.Package, baseName, shortName string) *File {
return newFileForPath(pkg.ImportPath, pkg.Name, pkg.FSPath, baseName, shortName)
}
func newFileForPath(pkgPath paths.Pkg, pkgName string, pkgDir paths.FS, baseName, shortName string) *File {
jenFile := jen.NewFilePathName(pkgPath.String(), pkgName)
for pkgPath, alias := range importNames {
jenFile.ImportAlias(pkgPath, alias)
}
return &File{
Jen: jenFile,
dir: pkgDir,
pkgPath: pkgPath,
baseName: baseName,
shortName: shortName,
}
}
// File represents a generated file for a specific package.
type File struct {
Jen *jen.File // the jen file we're generating
// dir is the filesystem directory where the file should exist
// within the application source tree. It need not match
// any existing physical directory in the case of overlays.
dir paths.FS
pkgPath paths.Pkg // the package the file belongs to
baseName string // the file's base name
// shortName is the short version of the base name, for generated files.
// For example if the base name is "encore_internal__metrics.go",
// shortName is "metrics".
shortName string
decls []any // ordered list of Decl or jen.Code
}
// ImportAnon adds an anonymous ("_"-prefixed) import of the given packages.
func (f *File) ImportAnon(pkgs ...paths.Pkg) {
f.Jen.Anon(fns.Map(pkgs, func(pkg paths.Pkg) string {
return pkg.String()
})...)
}
// name returns the computed file name.
func (f *File) name() string {
return f.baseName
}
// Add adds a declaration to the file.
func (f *File) Add(code jen.Code) {
if code != nil {
f.decls = append(f.decls, code)
}
}
// Render renders the file to the given writer.
func (f *File) Render(w io.Writer) error {
for i, d := range f.decls {
if i > 0 {
f.Jen.Line()
}
switch d := d.(type) {
case Decl:
f.Jen.Add(d.Code())
case jen.Code:
f.Jen.Add(d)
default:
panic(fmt.Sprintf("internal error: unknown decl type: %T", d))
}
}
return f.Jen.Render(w)
}
type Decl interface {
Name() string
Qual() *jen.Statement
Code() *jen.Statement
}
func (f *File) HasDecl(nameParts ...string) bool {
for _, decl := range f.decls {
switch decl := decl.(type) {
case *FuncDecl:
if slices.Equal(decl.nameParts, nameParts) {
return true
}
case *VarDecl:
if slices.Equal(decl.nameParts, nameParts) {
return true
}
}
}
return false
}
func (f *File) FuncDecl(nameParts ...string) *FuncDecl {
if len(nameParts) == 0 {
panic("gen.VarDecl: empty nameParts")
}
d := &FuncDecl{
File: f,
nameParts: nameParts,
}
f.decls = append(f.decls, d)
return d
}
func (f *File) VarDecl(nameParts ...string) *VarDecl {
if len(nameParts) == 0 {
panic("gen.VarDecl: empty nameParts")
}
d := &VarDecl{
File: f,
nameParts: nameParts,
}
f.decls = append(f.decls, d)
return d
}
// FuncDecl represents a generated declaration.
type FuncDecl struct {
File *File // file the declaration belongs to.
// nameParts are the suffix parts of the generated name.
// For example, if the parts are ["foo", "bar"] and the
// file shortName is "metrics", the generated declaration name
// is "EncoreInternal_metrics_foo_bar".
nameParts []string
// typeParams are the type parameters of the generated function.
typeParams []jen.Code
// params are the parameters of the generated function.
params []jen.Code
// results are the results of the generated function.
results []jen.Code
// body is the body of the generated function.
body *jen.Statement
}
// Name returns the package-level name of the declaration.
func (d *FuncDecl) Name() string {
return "EncoreInternal_" + d.File.shortName + "_" + strings.Join(d.nameParts, "_")
}
// Qual returns the qualified name of the declaration.
func (d *FuncDecl) Qual() *jen.Statement {
return jen.Qual(d.File.pkgPath.String(), d.Name())
}
// Code returns the generated code.
func (d *FuncDecl) Code() *jen.Statement {
s := jen.Func().Id(d.Name())
if len(d.typeParams) > 0 {
s = s.Types(d.typeParams...)
}
if len(d.params) > 0 {
s = s.Params(d.params...)
}
if len(d.results) > 0 {
s = s.Params(d.results...)
}
if d.body != nil {
s = s.Add(d.body)
} else {
s = s.Block()
}
return s
}
// TypeParams appends to the type parameters of the generated function.
func (d *FuncDecl) TypeParams(params ...jen.Code) *FuncDecl {
d.typeParams = append(d.typeParams, params...)
return d
}
// Params appends to the parameters of the generated function.
func (d *FuncDecl) Params(params ...jen.Code) *FuncDecl {
d.params = append(d.params, params...)
return d
}
// Results appends to the results of the generated function.
func (d *FuncDecl) Results(results ...jen.Code) *FuncDecl {
d.results = append(d.results, results...)
return d
}
// Body sets the body of the generated function.
func (d *FuncDecl) Body(code ...jen.Code) *FuncDecl {
d.body = jen.Block(code...)
return d
}
// BodyFunc computes the body of the generated function.
func (d *FuncDecl) BodyFunc(fn func(g *jen.Group)) *FuncDecl {
d.body = jen.BlockFunc(fn)
return d
}
type VarDecl struct {
File *File // file the declaration belongs to.
// nameParts are the suffix parts of the generated name.
// For example, if the parts are ["foo", "bar"] and the
// file shortName is "metrics", the generated declaration name
// is "EncoreInternal_metrics_foo_bar".
nameParts []string
value *jen.Statement
}
func (d *VarDecl) Value(code ...jen.Code) *VarDecl {
d.value = jen.Add(code...)
return d
}
func (d *VarDecl) Code() *jen.Statement {
return jen.Var().Id(d.Name()).Op("=").Add(d.value)
}
func (d *VarDecl) Name() string {
return "EncoreInternal_" + d.File.shortName + "_" + strings.Join(d.nameParts, "_")
}
func (d *VarDecl) Qual() *jen.Statement {
return jen.Qual(d.File.pkgPath.String(), d.Name())
}