-
Notifications
You must be signed in to change notification settings - Fork 315
/
parser.go
131 lines (115 loc) · 2.82 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
package apis
import (
"go/ast"
"go/token"
"encr.dev/v2/parser/apis/api"
"encr.dev/v2/parser/apis/authhandler"
"encr.dev/v2/parser/apis/internal/directive"
"encr.dev/v2/parser/apis/middleware"
"encr.dev/v2/parser/apis/servicestruct"
"encr.dev/v2/parser/resource/resourceparser"
)
var Parser = &resourceparser.Parser{
Name: "APIs",
InterestingImports: resourceparser.RunAlways,
Run: func(p *resourceparser.Pass) {
for _, file := range p.Pkg.Files {
for _, decl := range file.AST().Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
if decl.Doc == nil {
continue
}
dir, doc, ok := directive.Parse(p.Errs, decl.Doc)
if !ok {
continue
} else if dir == nil {
continue
}
switch dir.Name {
case "api":
ep := api.Parse(api.ParseData{
Errs: p.Errs,
Schema: p.SchemaParser,
File: file,
Func: decl,
Dir: dir,
Doc: doc,
})
if ep != nil {
p.RegisterResource(ep)
// We unconditionally register a package-level bind here,
// even if the endpoint is defined on a service struct.
//
// This is the case because we generate a package-level
// wrapper function that forwards to the service struct
// method in that case.
p.AddBind(ep.Decl.AST.Name, ep)
}
case "authhandler":
ah := authhandler.Parse(authhandler.ParseData{
Errs: p.Errs,
Schema: p.SchemaParser,
File: file,
Func: decl,
Dir: dir,
Doc: doc,
})
if ah != nil {
p.RegisterResource(ah)
if ah.Recv.Empty() {
p.AddBind(ah.Decl.AST.Name, ah)
}
}
case "middleware":
mw := middleware.Parse(middleware.ParseData{
Errs: p.Errs,
Schema: p.SchemaParser,
File: file,
Func: decl,
Dir: dir,
Doc: doc,
})
if mw != nil {
p.RegisterResource(mw)
if mw.Recv.Empty() {
p.AddBind(mw.Decl.AST.Name, mw)
}
}
default:
p.Errs.Add(errUnexpectedDirective(dir.Name).AtGoNode(decl))
}
case *ast.GenDecl:
if decl.Tok != token.TYPE {
continue
} else if decl.Doc == nil {
continue
}
dir, doc, ok := directive.Parse(p.Errs, decl.Doc)
if !ok {
continue
} else if dir == nil {
continue
}
switch dir.Name {
case "service":
ss := servicestruct.Parse(servicestruct.ParseData{
Errs: p.Errs,
Schema: p.SchemaParser,
File: file,
Decl: decl,
Dir: dir,
Doc: doc,
})
if ss != nil {
p.RegisterResource(ss)
p.AddBind(ss.Decl.AST.Name, ss)
}
default:
p.Errs.Add(errUnexpectedDirective(dir.Name).AtGoNode(decl))
}
}
}
}
},
}