forked from bufbuild/protocompile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
445 lines (430 loc) · 13.6 KB
/
walk.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package walk provides helper functions for traversing all elements in a
// protobuf file descriptor. There are versions both for traversing "rich"
// descriptors (protoreflect.Descriptor) and for traversing the underlying
// "raw" descriptor protos.
//
// # Enter And Exit
//
// This package includes variants of the functions that accept two callback
// functions. These variants have names ending with "EnterAndExit". One function
// is called as each element is visited ("enter") and the other is called after
// the element and all of its descendants have been visited ("exit"). This
// can be useful when you need to track state that is scoped to the visitation
// of a single element.
//
// # Source Path
//
// When traversing raw descriptor protos, this package include variants whose
// callback accepts a protoreflect.SourcePath. These variants have names that
// include "WithPath". This path can be used to locate corresponding data in the
// file's source code info (if present).
package walk
import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/kralicky/protocompile/protointernal"
)
// Descriptors walks all descriptors in the given file using a depth-first
// traversal, calling the given function for each descriptor in the hierarchy.
// The walk ends when traversal is complete or when the function returns an
// error. If the function returns an error, that is returned as the result of the
// walk operation.
//
// Descriptors are visited using a pre-order traversal, where the function is
// called for a descriptor before it is called for any of its descendants.
func Descriptors(file protoreflect.FileDescriptor, fn func(protoreflect.Descriptor) error) error {
return DescriptorsEnterAndExit(file, fn, nil)
}
// DescriptorsEnterAndExit walks all descriptors in the given file using a
// depth-first traversal, calling the given functions on entry and on exit
// for each descriptor in the hierarchy. The walk ends when traversal is
// complete or when a function returns an error. If a function returns an error,
// that is returned as the result of the walk operation.
//
// The enter function is called using a pre-order traversal, where the function
// is called for a descriptor before it is called for any of its descendants.
// The exit function is called using a post-order traversal, where the function
// is called for a descriptor only after it is called for any descendants.
func DescriptorsEnterAndExit(file protoreflect.FileDescriptor, enter, exit func(protoreflect.Descriptor) error) error {
for i := 0; i < file.Messages().Len(); i++ {
msg := file.Messages().Get(i)
if err := messageDescriptor(msg, enter, exit); err != nil {
return err
}
}
for i := 0; i < file.Enums().Len(); i++ {
en := file.Enums().Get(i)
if err := enumDescriptor(en, enter, exit); err != nil {
return err
}
}
for i := 0; i < file.Extensions().Len(); i++ {
ext := file.Extensions().Get(i)
if err := enter(ext); err != nil {
return err
}
if exit != nil {
if err := exit(ext); err != nil {
return err
}
}
}
for i := 0; i < file.Services().Len(); i++ {
svc := file.Services().Get(i)
if err := enter(svc); err != nil {
return err
}
for i := 0; i < svc.Methods().Len(); i++ {
mtd := svc.Methods().Get(i)
if err := enter(mtd); err != nil {
return err
}
if exit != nil {
if err := exit(mtd); err != nil {
return err
}
}
}
if exit != nil {
if err := exit(svc); err != nil {
return err
}
}
}
return nil
}
func messageDescriptor(msg protoreflect.MessageDescriptor, enter, exit func(protoreflect.Descriptor) error) error {
if err := enter(msg); err != nil {
return err
}
for i := 0; i < msg.Fields().Len(); i++ {
fld := msg.Fields().Get(i)
if err := enter(fld); err != nil {
return err
}
if exit != nil {
if err := exit(fld); err != nil {
return err
}
}
}
for i := 0; i < msg.Oneofs().Len(); i++ {
oo := msg.Oneofs().Get(i)
if err := enter(oo); err != nil {
return err
}
if exit != nil {
if err := exit(oo); err != nil {
return err
}
}
}
for i := 0; i < msg.Messages().Len(); i++ {
nested := msg.Messages().Get(i)
if err := messageDescriptor(nested, enter, exit); err != nil {
return err
}
}
for i := 0; i < msg.Enums().Len(); i++ {
en := msg.Enums().Get(i)
if err := enumDescriptor(en, enter, exit); err != nil {
return err
}
}
for i := 0; i < msg.Extensions().Len(); i++ {
ext := msg.Extensions().Get(i)
if err := enter(ext); err != nil {
return err
}
if exit != nil {
if err := exit(ext); err != nil {
return err
}
}
}
if exit != nil {
if err := exit(msg); err != nil {
return err
}
}
return nil
}
func enumDescriptor(en protoreflect.EnumDescriptor, enter, exit func(protoreflect.Descriptor) error) error {
if err := enter(en); err != nil {
return err
}
for i := 0; i < en.Values().Len(); i++ {
enVal := en.Values().Get(i)
if err := enter(enVal); err != nil {
return err
}
if exit != nil {
if err := exit(enVal); err != nil {
return err
}
}
}
if exit != nil {
if err := exit(en); err != nil {
return err
}
}
return nil
}
// DescriptorProtosWithPath walks all descriptor protos in the given file using
// a depth-first traversal. This is the same as DescriptorProtos except that the
// callback function, fn, receives a protoreflect.SourcePath, that indicates the
// path for the element in the file's source code info.
func DescriptorProtosWithPath(file *descriptorpb.FileDescriptorProto, fn func(protoreflect.FullName, protoreflect.SourcePath, proto.Message) error) error {
return DescriptorProtosWithPathEnterAndExit(file, fn, nil)
}
// DescriptorProtosWithPathEnterAndExit walks all descriptor protos in the given
// file using a depth-first traversal. This is the same as
// DescriptorProtosEnterAndExit except that the callback function, fn, receives
// a protoreflect.SourcePath, that indicates the path for the element in the
// file's source code info.
func DescriptorProtosWithPathEnterAndExit(file *descriptorpb.FileDescriptorProto, enter, exit func(protoreflect.FullName, protoreflect.SourcePath, proto.Message) error) error {
w := &protoWalker{usePath: true, enter: enter, exit: exit}
return w.walkDescriptorProtos(file)
}
// DescriptorProtos walks all descriptor protos in the given file using a
// depth-first traversal, calling the given function for each descriptor proto
// in the hierarchy. The walk ends when traversal is complete or when the
// function returns an error. If the function returns an error, that is
// returned as the result of the walk operation.
//
// Descriptor protos are visited using a pre-order traversal, where the function
// is called for a descriptor before it is called for any of its descendants.
func DescriptorProtos(file *descriptorpb.FileDescriptorProto, fn func(protoreflect.FullName, proto.Message) error) error {
return DescriptorProtosEnterAndExit(file, fn, nil)
}
// DescriptorProtosEnterAndExit walks all descriptor protos in the given file
// using a depth-first traversal, calling the given functions on entry and on
// exit for each descriptor in the hierarchy. The walk ends when traversal is
// complete or when a function returns an error. If a function returns an error,
// that is returned as the result of the walk operation.
//
// The enter function is called using a pre-order traversal, where the function
// is called for a descriptor proto before it is called for any of its
// descendants. The exit function is called using a post-order traversal, where
// the function is called for a descriptor proto only after it is called for any
// descendants.
func DescriptorProtosEnterAndExit(file *descriptorpb.FileDescriptorProto, enter, exit func(protoreflect.FullName, proto.Message) error) error {
enterWithPath := func(n protoreflect.FullName, p protoreflect.SourcePath, m proto.Message) error {
return enter(n, m)
}
var exitWithPath func(n protoreflect.FullName, p protoreflect.SourcePath, m proto.Message) error
if exit != nil {
exitWithPath = func(n protoreflect.FullName, p protoreflect.SourcePath, m proto.Message) error {
return exit(n, m)
}
}
w := &protoWalker{
enter: enterWithPath,
exit: exitWithPath,
}
return w.walkDescriptorProtos(file)
}
type protoWalker struct {
usePath bool
enter, exit func(protoreflect.FullName, protoreflect.SourcePath, proto.Message) error
}
func (w *protoWalker) walkDescriptorProtos(file *descriptorpb.FileDescriptorProto) error {
prefix := file.GetPackage()
if prefix != "" {
prefix += "."
}
var path protoreflect.SourcePath
for i, msg := range file.MessageType {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.FileMessagesTag, int32(i))
}
if err := w.walkDescriptorProto(prefix, p, msg); err != nil {
return err
}
}
for i, en := range file.EnumType {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.FileEnumsTag, int32(i))
}
if err := w.walkEnumDescriptorProto(prefix, p, en); err != nil {
return err
}
}
for i, ext := range file.Extension {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.FileExtensionsTag, int32(i))
}
fqn := prefix + ext.GetName()
if err := w.enter(protoreflect.FullName(fqn), p, ext); err != nil {
return err
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), p, ext); err != nil {
return err
}
}
}
for i, svc := range file.Service {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.FileServicesTag, int32(i))
}
fqn := prefix + svc.GetName()
if err := w.enter(protoreflect.FullName(fqn), p, svc); err != nil {
return err
}
for j, mtd := range svc.Method {
var mp protoreflect.SourcePath
if w.usePath {
mp = p
mp = append(mp, protointernal.ServiceMethodsTag, int32(j))
}
mtdFqn := fqn + "." + mtd.GetName()
if err := w.enter(protoreflect.FullName(mtdFqn), mp, mtd); err != nil {
return err
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(mtdFqn), mp, mtd); err != nil {
return err
}
}
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), p, svc); err != nil {
return err
}
}
}
return nil
}
func (w *protoWalker) walkDescriptorProto(prefix string, path protoreflect.SourcePath, msg *descriptorpb.DescriptorProto) error {
fqn := prefix + msg.GetName()
if err := w.enter(protoreflect.FullName(fqn), path, msg); err != nil {
return err
}
prefix = fqn + "."
for i, fld := range msg.Field {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.MessageFieldsTag, int32(i))
}
fqn := prefix + fld.GetName()
if err := w.enter(protoreflect.FullName(fqn), p, fld); err != nil {
return err
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), p, fld); err != nil {
return err
}
}
}
for i, oo := range msg.OneofDecl {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.MessageOneofsTag, int32(i))
}
fqn := prefix + oo.GetName()
if err := w.enter(protoreflect.FullName(fqn), p, oo); err != nil {
return err
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), p, oo); err != nil {
return err
}
}
}
for i, nested := range msg.NestedType {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.MessageNestedMessagesTag, int32(i))
}
if err := w.walkDescriptorProto(prefix, p, nested); err != nil {
return err
}
}
for i, en := range msg.EnumType {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.MessageEnumsTag, int32(i))
}
if err := w.walkEnumDescriptorProto(prefix, p, en); err != nil {
return err
}
}
for i, ext := range msg.Extension {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.MessageExtensionsTag, int32(i))
}
fqn := prefix + ext.GetName()
if err := w.enter(protoreflect.FullName(fqn), p, ext); err != nil {
return err
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), p, ext); err != nil {
return err
}
}
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), path, msg); err != nil {
return err
}
}
return nil
}
func (w *protoWalker) walkEnumDescriptorProto(prefix string, path protoreflect.SourcePath, en *descriptorpb.EnumDescriptorProto) error {
fqn := prefix + en.GetName()
if err := w.enter(protoreflect.FullName(fqn), path, en); err != nil {
return err
}
for i, val := range en.Value {
var p protoreflect.SourcePath
if w.usePath {
p = path
p = append(p, protointernal.EnumValuesTag, int32(i))
}
fqn := prefix + val.GetName()
if err := w.enter(protoreflect.FullName(fqn), p, val); err != nil {
return err
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), p, val); err != nil {
return err
}
}
}
if w.exit != nil {
if err := w.exit(protoreflect.FullName(fqn), path, en); err != nil {
return err
}
}
return nil
}