-
Notifications
You must be signed in to change notification settings - Fork 231
/
validate_pubsub.go
142 lines (124 loc) · 4.25 KB
/
validate_pubsub.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
package app
import (
"go/ast"
"encr.dev/pkg/errors"
"encr.dev/pkg/option"
"encr.dev/v2/internals/parsectx"
"encr.dev/v2/internals/pkginfo"
"encr.dev/v2/parser"
"encr.dev/v2/parser/apis/api"
"encr.dev/v2/parser/infra/pubsub"
)
func (d *Desc) validatePubSub(pc *parsectx.Context, result *parser.Result) {
type topic struct {
resource *pubsub.Topic
subs map[string]*pubsub.Subscription
}
topics := make(map[string]topic)
topicsByBinding := make(map[pkginfo.QualifiedName]string)
var subs []*pubsub.Subscription
for _, res := range d.Parse.Resources() {
switch res := res.(type) {
case *pubsub.Topic:
if existing, ok := topics[res.Name]; ok {
pc.Errs.Add(pubsub.ErrTopicNameNotUnique.
AtGoNode(existing.resource.AST.Args[0], errors.AsHelp("originally defined here")).
AtGoNode(res.AST.Args[0], errors.AsError("duplicated here")),
)
} else {
topics[res.Name] = topic{
resource: res,
subs: make(map[string]*pubsub.Subscription),
}
for _, bind := range d.Parse.PkgDeclBinds(res) {
topicsByBinding[bind.QualifiedName()] = res.Name
}
}
// Make sure any TopicRef calls are within a service.
for _, use := range d.Parse.Usages(res) {
if _, ok := use.(*pubsub.RefUsage); ok {
errTxt := "used here"
if _, ok := d.ServiceForPath(use.DeclaredIn().FSPath); !ok && !use.DeclaredIn().TestFile {
pc.Errs.Add(pubsub.ErrTopicRefOutsideService.
AtGoNode(use, errors.AsError(errTxt)),
)
}
}
}
case *pubsub.Subscription:
subs = append(subs, res)
}
}
for _, sub := range subs {
topicName := topicsByBinding[sub.Topic]
topic, ok := topics[topicName]
if !ok {
pc.Errs.Add(pubsub.ErrSubscriptionTopicNotResource.AtGoNode(sub.AST.Args[0]))
continue
}
if existing, ok := topic.subs[sub.Name]; ok {
pc.Errs.Add(pubsub.ErrSubscriptionNameNotUnique.
AtGoNode(existing.AST.Args[1], errors.AsHelp("originally defined here")).
AtGoNode(sub.AST.Args[1], errors.AsError("duplicated here")),
)
} else {
topic.subs[sub.Name] = sub
}
subService, ok := d.ServiceForPath(sub.File.FSPath)
if !ok {
pc.Errs.Add(pubsub.ErrUnableToIdentifyServicesInvolved.AtGoNode(sub, errors.AsError("unable to identify service for subscription")))
}
// Verify the handler is ok
handlerIsAPIEndpoint := false
if handlerUsage, ok := result.UsageFromNode(sub.Handler).Get(); ok {
if endpointUsage, ok := handlerUsage.(*api.ReferenceUsage); ok {
ep := endpointUsage.Endpoint
endpointService, ok := d.ServiceForPath(ep.File.FSPath)
if !ok {
pc.Errs.Add(pubsub.ErrUnableToIdentifyServicesInvolved.AtGoNode(ep, errors.AsError("unable to identify service for endpoint")))
}
if endpointService != subService {
pc.Errs.Add(
pubsub.ErrSubscriptionHandlerNotDefinedInSameService.
AtGoNode(sub.Handler, errors.AsError("handler specified here")).
AtGoNode(ep.Decl.AST.Name, errors.AsHelp("endpoint defined here")),
)
}
handlerIsAPIEndpoint = true
}
}
if !handlerIsAPIEndpoint {
qn, ok := sub.File.Names().ResolvePkgLevelRef(sub.Handler)
if ok {
if pkg, ok := result.PackageAt(qn.PkgPath).Get(); ok {
funcName := option.Map(pkg.Names().FuncDecl(qn.Name), func(f *ast.FuncDecl) *ast.Ident {
return f.Name
}).GetOrElse(nil)
if svc, ok := d.ServiceForPath(pkg.FSPath); ok {
if svc != subService {
pc.Errs.Add(
pubsub.ErrSubscriptionHandlerNotDefinedInSameService.
AtGoNode(sub.Handler, errors.AsError("handler specified here")).
AtGoNode(funcName, errors.AsHelp("handler function defined here")),
)
}
} else {
pc.Errs.Add(
pubsub.ErrSubscriptionHandlerNotDefinedInSameService.
AtGoNode(sub.Handler, errors.AsError("handler specified here")).
AtGoNode(funcName, errors.AsHelp("handler function defined here")),
)
}
} else {
pc.Errs.Add(
pubsub.ErrUnableToIdentifyServicesInvolved.
AtGoNode(sub.Handler, errors.AsError("unable to identify package for this reference")),
)
}
} else {
// this is ok, because it means we're not dealing with an ident or selector - thus
// it can't be a valid function reference and the normal Go compiler will pick this up
}
}
}
}