-
Notifications
You must be signed in to change notification settings - Fork 316
/
parser.go
252 lines (219 loc) · 7.79 KB
/
parser.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
package parser
import (
"cmp"
"slices"
"sync"
"encr.dev/pkg/paths"
"encr.dev/v2/internals/parsectx"
"encr.dev/v2/internals/pkginfo"
"encr.dev/v2/internals/scan"
"encr.dev/v2/internals/schema"
"encr.dev/v2/parser/apis"
"encr.dev/v2/parser/apis/api"
"encr.dev/v2/parser/apis/authhandler"
"encr.dev/v2/parser/apis/servicestruct"
"encr.dev/v2/parser/infra/caches"
"encr.dev/v2/parser/infra/config"
"encr.dev/v2/parser/infra/crons"
"encr.dev/v2/parser/infra/metrics"
"encr.dev/v2/parser/infra/pubsub"
"encr.dev/v2/parser/infra/secrets"
"encr.dev/v2/parser/infra/sqldb"
"encr.dev/v2/parser/resource"
"encr.dev/v2/parser/resource/resourceparser"
"encr.dev/v2/parser/resource/usage"
)
func NewParser(c *parsectx.Context) *Parser {
loader := pkginfo.New(c)
schemaParser := schema.NewParser(c, loader)
return &Parser{
c: c,
loader: loader,
schemaParser: schemaParser,
registry: resourceparser.NewRegistry(allParsers),
usageResolver: newUsageResolver(),
}
}
type Parser struct {
c *parsectx.Context
loader *pkginfo.Loader
schemaParser *schema.Parser
registry *resourceparser.Registry
usageResolver *usage.Resolver
}
func (p *Parser) MainModule() *pkginfo.Module {
return p.loader.MainModule()
}
func (p *Parser) RuntimeModule() *pkginfo.Module {
return p.loader.RuntimeModule()
}
// Parse parses the given application for uses of the Encore API Framework
// and the Encore infrastructure SDK.
func (p *Parser) Parse() *Result {
var (
mu sync.Mutex
pkgs []*pkginfo.Package
resources []resource.Resource
binds []resource.Bind
)
scan.ProcessModule(p.c.Errs, p.loader, p.c.MainModuleDir, func(pkg *pkginfo.Package) {
if pkg.Name == "main" {
// Ignore main packages that aren't the main package we're building, if any.
if mainPkg, ok := p.c.Build.MainPkg.Get(); !ok || pkg.ImportPath != mainPkg {
return
}
}
pass := &resourceparser.Pass{
Context: p.c,
SchemaParser: p.schemaParser,
Pkg: pkg,
}
interested := p.registry.InterestedParsers(pkg)
for _, p := range interested {
p.Run(pass)
}
mu.Lock()
pkgs = append(pkgs, pkg)
resources = append(resources, pass.Resources()...)
binds = append(binds, pass.Binds()...)
mu.Unlock()
})
// Normally every resource is independent, but in the case of implicit sqldb
// databases that come up as a result of having a "migrations" folder
// we can end up with duplicate resources if we also have a sqldb.NewDatabase call.
// Deduplicate them for now.
resources, binds = deduplicateSQLDBResources(resources, binds)
// Sort resources by package and position so the array is stable between runs
// as we process modules in parallel we can't rely on the order of the
// resources being stable coming into this function.
slices.SortFunc(resources, func(a, b resource.Resource) int {
p1, p2 := p.c.FS.Position(a.Pos()), p.c.FS.Position(b.Pos())
if n := cmp.Compare(p1.Filename, p2.Filename); n != 0 {
return n
} else if n := cmp.Compare(p1.Line, p2.Line); n != 0 {
return n
} else if n := cmp.Compare(p1.Column, p2.Column); n != 0 {
return n
}
return cmp.Compare(a.Pos(), b.Pos())
})
// Then sort the binds
slices.SortFunc(binds, func(a, b resource.Bind) int {
if a.Package() != b.Package() {
return cmp.Compare(a.Package().FSPath, b.Package().FSPath)
}
return cmp.Compare(a.Pos(), b.Pos())
})
// Finally, sort the packages
slices.SortFunc(pkgs, func(a, b *pkginfo.Package) int {
return cmp.Compare(a.FSPath, b.FSPath)
})
// Because we've ordered pkgs and binds, usageExprs will be stable
usageExprs := usage.ParseExprs(p.schemaParser, pkgs, binds)
// Add the implicit sqldb usages.
usageExprs = append(usageExprs, sqldb.ComputeImplicitUsage(p.c.Errs, pkgs, binds)...)
return computeResult(p.c.Errs, p.MainModule(), p.usageResolver, pkgs, resources, binds, usageExprs)
}
// allParsers are all the resource parsers we support.
var allParsers = []*resourceparser.Parser{
apis.Parser,
caches.ClusterParser,
caches.KeyspaceParser,
config.LoadParser,
crons.JobParser,
metrics.MetricParser,
pubsub.TopicParser,
pubsub.SubscriptionParser,
secrets.SecretsParser,
sqldb.DatabaseParser,
sqldb.MigrationParser,
sqldb.NamedParser,
}
func newUsageResolver() *usage.Resolver {
r := usage.NewResolver()
// Infrastructure SDK
usage.RegisterUsageResolver[*caches.Keyspace](r, caches.ResolveKeyspaceUsage)
usage.RegisterUsageResolver[*config.Load](r, config.ResolveConfigUsage)
usage.RegisterUsageResolver[*pubsub.Topic](r, pubsub.ResolveTopicUsage)
usage.RegisterUsageResolver[*sqldb.Database](r, sqldb.ResolveDatabaseUsage)
// API Framework
usage.RegisterUsageResolver[*api.Endpoint](r, api.ResolveEndpointUsage)
usage.RegisterUsageResolver[*authhandler.AuthHandler](r, authhandler.ResolveAuthHandlerUsage)
usage.RegisterUsageResolver[*servicestruct.ServiceStruct](r, servicestruct.ResolveServiceStructUsage)
return r
}
// deduplicateSQLDBResources deduplicates SQL Database resources and their associated binds
// in the case where we have multiple SQL Database resources with the same name,
// as a result of having an explicit bind (via sqldb.NewDatabase) and an implicit bind (via a "migrations" folder).
func deduplicateSQLDBResources(resources []resource.Resource, binds []resource.Bind) ([]resource.Resource, []resource.Bind) {
bindsPerDB := make(map[string][]resource.Bind)
bindsPerMigrationDir := make(map[paths.MainModuleRelSlash][]resource.Bind)
for _, b := range binds {
// All the binds we're interested in contain a resource and not a path.
r := b.ResourceRef().Resource
if db, ok := r.(*sqldb.Database); ok {
bindsPerDB[db.Name] = append(bindsPerDB[db.Name], b)
bindsPerMigrationDir[db.MigrationDir] = append(bindsPerMigrationDir[db.MigrationDir], b)
}
}
resourcesToRemove := make(map[resource.Resource]bool)
bindsToRemove := make(map[resource.Bind]bool)
for _, binds := range bindsPerDB {
implicitIdx := slices.IndexFunc(binds, func(b resource.Bind) bool {
_, ok := b.(*resource.ImplicitBind)
return ok
})
explicitIdx := slices.IndexFunc(binds, func(b resource.Bind) bool {
_, ok := b.(*resource.ImplicitBind)
return !ok
})
if implicitIdx >= 0 && explicitIdx >= 0 {
// We have both types of binds. Delete the implicit one.
implicit := binds[implicitIdx]
bindsToRemove[implicit] = true
// If they refer to different underlying resources, delete the implicit resource.
explicit := binds[explicitIdx]
if res := implicit.ResourceRef().Resource; res != nil && res != explicit.ResourceRef().Resource {
resourcesToRemove[res] = true
}
}
}
// Now do the same based on migration dir
for _, binds := range bindsPerMigrationDir {
implicitIdx := slices.IndexFunc(binds, func(b resource.Bind) bool {
_, ok := b.(*resource.ImplicitBind)
return ok
})
explicitIdx := slices.IndexFunc(binds, func(b resource.Bind) bool {
_, ok := b.(*resource.ImplicitBind)
return !ok
})
if implicitIdx >= 0 && explicitIdx >= 0 {
// We have both types of binds. Delete the implicit one.
implicit := binds[implicitIdx]
bindsToRemove[implicit] = true
// If they refer to different underlying resources, delete the implicit resource.
explicit := binds[explicitIdx]
if res := implicit.ResourceRef().Resource; res != nil && res != explicit.ResourceRef().Resource {
resourcesToRemove[res] = true
}
}
}
if len(resourcesToRemove) == 0 && len(bindsToRemove) == 0 {
// Nothing to do.
return resources, binds
}
updatedResources := make([]resource.Resource, 0, len(resources))
updatedBinds := make([]resource.Bind, 0, len(binds))
for _, r := range resources {
if !resourcesToRemove[r] {
updatedResources = append(updatedResources, r)
}
}
for _, b := range binds {
if !bindsToRemove[b] {
updatedBinds = append(updatedBinds, b)
}
}
return updatedResources, updatedBinds
}