-
Notifications
You must be signed in to change notification settings - Fork 31
/
parser.go
289 lines (233 loc) · 8.55 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package protokit
import (
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/plugin"
"context"
"fmt"
"strings"
)
const (
// tag numbers in FileDescriptorProto
packageCommentPath = 2
messageCommentPath = 4
enumCommentPath = 5
serviceCommentPath = 6
extensionCommentPath = 7
syntaxCommentPath = 12
// tag numbers in DescriptorProto
messageFieldCommentPath = 2 // field
messageMessageCommentPath = 3 // nested_type
messageEnumCommentPath = 4 // enum_type
messageExtensionCommentPath = 6 // extension
// tag numbers in EnumDescriptorProto
enumValueCommentPath = 2 // value
// tag numbers in ServiceDescriptorProto
serviceMethodCommentPath = 2
)
// ParseCodeGenRequest parses the given request into `FileDescriptor` objects. Only the `req.FilesToGenerate` will be
// returned.
//
// For example, given the following invocation, only booking.proto will be returned even if it imports other protos:
//
// protoc --plugin=protoc-gen-test=./test -I. protos/booking.proto
func ParseCodeGenRequest(req *plugin_go.CodeGeneratorRequest) []*FileDescriptor {
allFiles := make(map[string]*FileDescriptor)
genFiles := make([]*FileDescriptor, len(req.GetFileToGenerate()))
for _, pf := range req.GetProtoFile() {
allFiles[pf.GetName()] = parseFile(context.Background(), pf)
}
for i, f := range req.GetFileToGenerate() {
genFiles[i] = allFiles[f]
parseImports(genFiles[i], allFiles)
}
return genFiles
}
func parseFile(ctx context.Context, fd *descriptor.FileDescriptorProto) *FileDescriptor {
comments := ParseComments(fd)
file := &FileDescriptor{
comments: comments,
FileDescriptorProto: fd,
Comments: comments.Get(fmt.Sprintf("%d", packageCommentPath)),
PackageComments: comments.Get(fmt.Sprintf("%d", packageCommentPath)),
SyntaxComments: comments.Get(fmt.Sprintf("%d", syntaxCommentPath)),
}
if fd.Options != nil {
file.setOptions(fd.Options)
}
fileCtx := ContextWithFileDescriptor(ctx, file)
file.Enums = parseEnums(fileCtx, fd.GetEnumType())
file.Extensions = parseExtensions(fileCtx, fd.GetExtension())
file.Messages = parseMessages(fileCtx, fd.GetMessageType())
file.Services = parseServices(fileCtx, fd.GetService())
return file
}
func parseEnums(ctx context.Context, protos []*descriptor.EnumDescriptorProto) []*EnumDescriptor {
enums := make([]*EnumDescriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
parent, hasParent := DescriptorFromContext(ctx)
for i, ed := range protos {
longName := ed.GetName()
commentPath := fmt.Sprintf("%d.%d", enumCommentPath, i)
if hasParent {
longName = fmt.Sprintf("%s.%s", parent.GetLongName(), longName)
commentPath = fmt.Sprintf("%s.%d.%d", parent.path, messageEnumCommentPath, i)
}
enums[i] = &EnumDescriptor{
common: newCommon(file, commentPath, longName),
EnumDescriptorProto: ed,
Comments: file.comments.Get(commentPath),
Parent: parent,
}
if ed.Options != nil {
enums[i].setOptions(ed.Options)
}
subCtx := ContextWithEnumDescriptor(ctx, enums[i])
enums[i].Values = parseEnumValues(subCtx, ed.GetValue())
}
return enums
}
func parseEnumValues(ctx context.Context, protos []*descriptor.EnumValueDescriptorProto) []*EnumValueDescriptor {
values := make([]*EnumValueDescriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
enum, _ := EnumDescriptorFromContext(ctx)
for i, vd := range protos {
longName := fmt.Sprintf("%s.%s", enum.GetLongName(), vd.GetName())
values[i] = &EnumValueDescriptor{
common: newCommon(file, "", longName),
EnumValueDescriptorProto: vd,
Enum: enum,
Comments: file.comments.Get(fmt.Sprintf("%s.%d.%d", enum.path, enumValueCommentPath, i)),
}
if vd.Options != nil {
values[i].setOptions(vd.Options)
}
}
return values
}
func parseExtensions(ctx context.Context, protos []*descriptor.FieldDescriptorProto) []*ExtensionDescriptor {
exts := make([]*ExtensionDescriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
parent, hasParent := DescriptorFromContext(ctx)
for i, ext := range protos {
commentPath := fmt.Sprintf("%d.%d", extensionCommentPath, i)
longName := fmt.Sprintf("%s.%s", ext.GetExtendee(), ext.GetName())
if strings.Contains(longName, file.GetPackage()) {
parts := strings.Split(ext.GetExtendee(), ".")
longName = fmt.Sprintf("%s.%s", parts[len(parts)-1], ext.GetName())
}
if hasParent {
commentPath = fmt.Sprintf("%s.%d.%d", parent.path, messageExtensionCommentPath, i)
}
exts[i] = &ExtensionDescriptor{
common: newCommon(file, commentPath, longName),
FieldDescriptorProto: ext,
Comments: file.comments.Get(commentPath),
Parent: parent,
}
if ext.Options != nil {
exts[i].setOptions(ext.Options)
}
}
return exts
}
func parseImports(fd *FileDescriptor, allFiles map[string]*FileDescriptor) {
fd.Imports = make([]*ImportedDescriptor, 0)
for _, index := range fd.GetPublicDependency() {
file := allFiles[fd.GetDependency()[index]]
for _, d := range file.GetMessages() {
// skip map entry objects
if !d.GetOptions().GetMapEntry() {
fd.Imports = append(fd.Imports, &ImportedDescriptor{d.common})
}
}
for _, e := range file.GetEnums() {
fd.Imports = append(fd.Imports, &ImportedDescriptor{e.common})
}
for _, ext := range file.GetExtensions() {
fd.Imports = append(fd.Imports, &ImportedDescriptor{ext.common})
}
}
}
func parseMessages(ctx context.Context, protos []*descriptor.DescriptorProto) []*Descriptor {
msgs := make([]*Descriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
parent, hasParent := DescriptorFromContext(ctx)
for i, md := range protos {
longName := md.GetName()
commentPath := fmt.Sprintf("%d.%d", messageCommentPath, i)
if hasParent {
longName = fmt.Sprintf("%s.%s", parent.GetLongName(), longName)
commentPath = fmt.Sprintf("%s.%d.%d", parent.path, messageMessageCommentPath, i)
}
msgs[i] = &Descriptor{
common: newCommon(file, commentPath, longName),
DescriptorProto: md,
Comments: file.comments.Get(commentPath),
Parent: parent,
}
if md.Options != nil {
msgs[i].setOptions(md.Options)
}
msgCtx := ContextWithDescriptor(ctx, msgs[i])
msgs[i].Enums = parseEnums(msgCtx, md.GetEnumType())
msgs[i].Extensions = parseExtensions(msgCtx, md.GetExtension())
msgs[i].Fields = parseMessageFields(msgCtx, md.GetField())
msgs[i].Messages = parseMessages(msgCtx, md.GetNestedType())
}
return msgs
}
func parseMessageFields(ctx context.Context, protos []*descriptor.FieldDescriptorProto) []*FieldDescriptor {
fields := make([]*FieldDescriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
message, _ := DescriptorFromContext(ctx)
for i, fd := range protos {
longName := fmt.Sprintf("%s.%s", message.GetLongName(), fd.GetName())
fields[i] = &FieldDescriptor{
common: newCommon(file, "", longName),
FieldDescriptorProto: fd,
Comments: file.comments.Get(fmt.Sprintf("%s.%d.%d", message.path, messageFieldCommentPath, i)),
Message: message,
}
if fd.Options != nil {
fields[i].setOptions(fd.Options)
}
}
return fields
}
func parseServices(ctx context.Context, protos []*descriptor.ServiceDescriptorProto) []*ServiceDescriptor {
svcs := make([]*ServiceDescriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
for i, sd := range protos {
longName := sd.GetName()
commentPath := fmt.Sprintf("%d.%d", serviceCommentPath, i)
svcs[i] = &ServiceDescriptor{
common: newCommon(file, commentPath, longName),
ServiceDescriptorProto: sd,
Comments: file.comments.Get(commentPath),
}
if sd.Options != nil {
svcs[i].setOptions(sd.Options)
}
svcCtx := ContextWithServiceDescriptor(ctx, svcs[i])
svcs[i].Methods = parseServiceMethods(svcCtx, sd.GetMethod())
}
return svcs
}
func parseServiceMethods(ctx context.Context, protos []*descriptor.MethodDescriptorProto) []*MethodDescriptor {
methods := make([]*MethodDescriptor, len(protos))
file, _ := FileDescriptorFromContext(ctx)
svc, _ := ServiceDescriptorFromContext(ctx)
for i, md := range protos {
longName := fmt.Sprintf("%s.%s", svc.GetLongName(), md.GetName())
methods[i] = &MethodDescriptor{
common: newCommon(file, "", longName),
MethodDescriptorProto: md,
Service: svc,
Comments: file.comments.Get(fmt.Sprintf("%s.%d.%d", svc.path, serviceMethodCommentPath, i)),
}
if md.Options != nil {
methods[i].setOptions(md.Options)
}
}
return methods
}