-
Notifications
You must be signed in to change notification settings - Fork 232
/
validate_apis.go
123 lines (109 loc) · 3.59 KB
/
validate_apis.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
package app
import (
"fmt"
"encr.dev/pkg/errors"
"encr.dev/v2/app/apiframework"
"encr.dev/v2/internals/parsectx"
"encr.dev/v2/internals/resourcepaths"
"encr.dev/v2/internals/schema/schemautil"
"encr.dev/v2/parser"
"encr.dev/v2/parser/apis/api"
"encr.dev/v2/parser/apis/authhandler"
"encr.dev/v2/parser/apis/servicestruct"
"encr.dev/v2/parser/infra/crons"
"encr.dev/v2/parser/infra/pubsub"
"encr.dev/v2/parser/resource"
)
func (d *Desc) validateAPIs(pc *parsectx.Context, fw *apiframework.AppDesc, result *parser.Result) {
apiPaths := resourcepaths.NewSet()
for _, svc := range d.Services {
fwSvc, ok := svc.Framework.Get()
if !ok {
continue
}
svcStruct, hasSvcStruct := fwSvc.ServiceStruct.Get()
for _, ep := range fwSvc.Endpoints {
// Check if an auth handler is defined for an endpoint that requires auth.
if ep.Access == api.Auth && fw.AuthHandler.Empty() {
pc.Errs.Add(
errors.AtOptionalNode(authhandler.ErrNoAuthHandlerDefined, ep.AccessField),
)
}
// Check for duplicate paths by adding them to the set
// Note, errors will be reported automatically to pc.Errs
for _, method := range ep.HTTPMethods {
apiPaths.Add(pc.Errs, method, ep.Path)
}
if receiver, ok := ep.Recv.Get(); ok {
if !hasSvcStruct {
pc.Errs.Add(
servicestruct.ErrReceiverNotAServiceStruct.
AtGoNode(receiver.AST, errors.AsError("there are no service structs defined in this service")),
)
} else if receiver.Decl != svcStruct.Decl {
pc.Errs.Add(
servicestruct.ErrReceiverNotAServiceStruct.
AtGoNode(receiver.AST, errors.AsError(
fmt.Sprintf("try changing this to `*%s`", svcStruct.Decl.Name),
)).
AtGoNode(svcStruct.Decl.AST.Name, errors.AsHelp(
"this is the service struct for this service",
)),
)
}
}
if ep.Raw {
for _, rawUsage := range result.Usages(ep) {
pc.Errs.Add(
api.ErrRawEndpointsCannotBeCalled.
AtGoNode(rawUsage, errors.AsError("used here")).
AtGoNode(ep.Decl.AST.Name, errors.AsHelp("defined here")),
)
}
} else {
// If typed endpoint, validate the types of the request and response
if ep.Request != nil {
// The request is always the first parameter after any path params (and after the ctx)
field, _ := schemautil.GetArgument(ep.Decl.AST.Type.Params, len(ep.Path.Params())+1)
d.validateType(pc, field.Type, ep.Request)
}
if ep.Response != nil {
// The response is always the first return value
d.validateType(pc, ep.Decl.AST.Type.Results.List[0].Type, ep.Response)
}
}
// Check for usages outside of services
for _, invalidUsage := range d.ResourceUsageOutsideServices[ep] {
pc.Errs.Add(
api.ErrAPICalledOutsideService.
AtGoNode(invalidUsage, errors.AsError("called here")).
AtGoNode(ep.Decl.AST.Name, errors.AsHelp("defined here")),
)
}
// Check for invalid references
for _, usage := range result.Usages(ep) {
switch usage := usage.(type) {
case *api.ReferenceUsage:
// API's can only be referenced
isValid := result.ResourceConstructorContaining(usage).Contains(func(res resource.Resource) bool {
switch res := res.(type) {
case *pubsub.Subscription:
return res.Handler == usage.Ref
case *crons.Job:
return res.EndpointAST == usage.Ref
default:
return false
}
})
if !isValid {
pc.Errs.Add(
api.ErrInvalidEndpointUsage.
AtGoNode(usage, errors.AsError("used here")).
AtGoNode(ep.Decl.AST.Name, errors.AsHelp("defined here")),
)
}
}
}
}
}
}