forked from encoredev/encore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_caches.go
82 lines (70 loc) · 1.89 KB
/
validate_caches.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
package app
import (
"fmt"
"encr.dev/pkg/errors"
"encr.dev/v2/internals/parsectx"
"encr.dev/v2/internals/pkginfo"
"encr.dev/v2/internals/resourcepaths"
"encr.dev/v2/parser"
"encr.dev/v2/parser/infra/caches"
)
func (d *Desc) validateCaches(pc *parsectx.Context, results *parser.Result) {
type cache struct {
resource *caches.Cluster
paths *resourcepaths.Set
}
found := make(map[string]cache)
byBinding := make(map[pkginfo.QualifiedName]string)
// First find all clusters
var keyspaces []*caches.Keyspace
for _, res := range d.Parse.Resources() {
switch res := res.(type) {
case *caches.Cluster:
if existing, ok := found[res.Name]; ok {
pc.Errs.Add(
caches.ErrDuplicateCacheCluster.
AtGoNode(existing.resource.AST.Args[0]).
AtGoNode(res.AST.Args[0]),
)
continue
}
found[res.Name] = cache{
resource: res,
paths: resourcepaths.NewSet(),
}
for _, bind := range d.Parse.PkgDeclBinds(res) {
byBinding[bind.QualifiedName()] = res.Name
}
case *caches.Keyspace:
keyspaces = append(keyspaces, res)
}
}
// Then verify all keyspaces
for _, ks := range keyspaces {
clusterName := byBinding[ks.Cluster]
cluster, ok := found[clusterName]
if !ok {
pc.Errs.Add(caches.ErrCouldNotResolveCacheCluster.AtGoNode(ks.AST.Args[0]))
continue
}
cluster.paths.Add(pc.Errs, "*", ks.Path)
svc, ok := d.ServiceForPath(ks.File.FSPath)
if !ok {
pc.Errs.Add(caches.ErrKeyspaceNotInService.AtGoNode(ks.AST.Fun))
continue
}
for _, use := range results.Usages(ks) {
errTxt := "used here"
useSvc, ok := d.ServiceForPath(use.DeclaredIn().FSPath)
if ok {
errTxt = fmt.Sprintf("used in %q", useSvc.Name)
}
if useSvc != svc {
pc.Errs.Add(caches.ErrKeyspaceUsedInOtherService.
AtGoNode(use, errors.AsError(errTxt)).
AtGoNode(ks, errors.AsHelp(fmt.Sprintf("declared in %q", svc.Name))),
)
}
}
}
}