forked from bufbuild/protocompile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
439 lines (399 loc) · 10.4 KB
/
paths.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
package paths
import (
"slices"
"github.com/kralicky/protocompile/ast"
"google.golang.org/protobuf/reflect/protopath"
"google.golang.org/protobuf/reflect/protorange"
"google.golang.org/protobuf/reflect/protoreflect"
)
type PathIndex = struct {
Step protopath.Step
Value protoreflect.Value
}
// NodeIsConcrete returns true if the value at the given index in 'values' is
// a concrete node value, and false otherwise. A concrete node is a message that
// is not a wrapper node (or an array access to a wrapper node), and is not a
// list or map.
func NodeIsConcrete(values protopath.Values, index int) bool {
v := values.Index(index)
switch v.Step.Kind() {
case protopath.RootStep:
return true
case protopath.FieldAccessStep:
fld := v.Step.FieldDescriptor()
switch fld.Kind() {
case protoreflect.MessageKind:
if fld.IsList() || fld.IsMap() {
return false
}
switch v.Value.Message().Interface().(type) {
case ast.WrapperNode:
return false
case ast.Node:
return true
}
}
case protopath.ListIndexStep:
prev := values.Index(index - 1)
if prev.Step.Kind() == protopath.FieldAccessStep {
prevFld := prev.Step.FieldDescriptor()
if prevFld.Kind() != protoreflect.MessageKind {
return false
}
switch v.Value.Message().Interface().(type) {
case ast.WrapperNode:
return false
case ast.Node:
return true
}
}
}
return false
}
// Join returns a new path constructed by appending the steps of 'b' to 'a'.
// The first step in 'b' is skipped; it is assumed (but not checked for)
// that 'b' starts with a Root step matching the message type of the last
// step in path 'a'.
func Join[T protopath.Step, S ~[]T](a protopath.Path, b S) protopath.Path {
p := slices.Clone(a)
for _, step := range b[1:] {
p = append(p, protopath.Step(step))
}
return p
}
// NodeAt returns the node at the given index in a path.
func NodeAt[T ast.Node](idx PathIndex) (t T) {
t, _ = idx.Value.Message().Interface().(T)
return
}
// Slice returns a new path with both its path and values resliced by the given
// range. It is assumed that both slices are of the same length.
func Slice(from protopath.Values, start, end int) protopath.Values {
// if start is > 0, transform the first step into a Root step with the
// message type of the first value
if start > 0 {
rootStep := protopath.Root(from.Values[start].Message().Descriptor())
return protopath.Values{
Path: append(protopath.Path{rootStep}, from.Path[start+1:end]...),
Values: from.Values[start:end],
}
}
return protopath.Values{
Path: from.Path[start:end],
Values: from.Values[start:end],
}
}
// Dereference walks the given path from the root node and returns the node at
// the end of the path.
func Dereference(root ast.Node, path protopath.Path) ast.Node {
node := protoreflect.ValueOfMessage(root.ProtoReflect())
for _, step := range path {
switch step.Kind() {
case protopath.FieldAccessStep:
node = node.Message().Get(step.FieldDescriptor())
case protopath.ListIndexStep:
node = node.List().Get(step.ListIndex())
case protopath.RootStep:
// skip
}
}
return node.Message().Interface().(ast.Node)
}
// DereferenceAll walks the given path from the root node and returns all nodes
// visited. The first node in the returned slice is the root node. Not all
// path steps reference an actual node in the AST, so the returned slice may
// contain fewer than len(path) nodes.
func DereferenceAll(root ast.Node, path protopath.Path) []ast.Node {
list := []ast.Node{root}
node := protoreflect.ValueOfMessage(root.ProtoReflect())
for _, step := range path {
switch step.Kind() {
case protopath.FieldAccessStep:
fd := step.FieldDescriptor()
switch fd.Kind() {
case protoreflect.MessageKind:
node = node.Message().Get(step.FieldDescriptor())
if !fd.IsList() {
list = append(list, node.Message().Interface().(ast.Node))
}
}
case protopath.ListIndexStep:
node = node.List().Get(int(step.ListIndex()))
list = append(list, node.Message().Interface().(ast.Node))
case protopath.RootStep:
// skip; root is already in the list
}
}
return list
}
// ValuesToNodes returns a slice of nodes from the given values, filtering out
// wrapper nodes and other non-node values from the path.
func ValuesToNodes(values protopath.Values) (nodes []ast.Node) {
for i, v := range values.Values {
if NodeIsConcrete(values, i) {
nodes = append(nodes, v.Message().Interface().(ast.Node))
}
}
return
}
// NodeView returns a visitor suitable for use with [ast.WithBefore] and
// [ast.WithAfter] which calls the given function for each concrete node value
// encountered, ignoring wrapper nodes and other non-node values.
func NodeView(in func(v ast.Node)) func(v protopath.Values) error {
return func(v protopath.Values) error {
if NodeIsConcrete(v, -1) {
in(v.Index(-1).Value.Message().Interface().(ast.Node))
}
return nil
}
}
// AncestorTracker is used to track the path of nodes during a walk operation.
// By passing AsWalkOptions to a call to Walk, a visitor can inspect the path to
// the node being visited using this tracker.
type AncestorTracker struct {
ancestors protopath.Values
}
// AsWalkOptions returns WalkOption values that will cause this ancestor tracker
// to track the path through the AST during the walk operation.
func (t *AncestorTracker) AsWalkOptions() []ast.WalkOption {
return []ast.WalkOption{
ast.WithBefore(func(v protopath.Values) error {
t.ancestors = v
if NodeIsConcrete(v, -1) {
return nil
}
return protorange.Break
}),
}
}
// Path returns a slice of nodes that represents the path from the root of the
// walk operaiton to the currently visited node. The first element in the path
// is the root supplied to Walk. The last element in the path is the currently
// visited node.
func (t *AncestorTracker) Path() protopath.Path {
return slices.Clone(t.ancestors.Path)
}
// Values returns a copy of the tracked path and values at the currently visited
// node.
func (t *AncestorTracker) Values() protopath.Values {
return protopath.Values{
Path: slices.Clone(t.ancestors.Path),
Values: slices.Clone(t.ancestors.Values),
}
}
// Suffix2 inspects the given values and, if the path logically ends with the
// sequence of nodes 'T, U' (such that U is a message field of T), returns
// the two nodes and true. Otherwise, returns false. This method correctly
// handles non-node path steps such as list accesses. The returned nodes may
// not necessarily be the final 2 nodes in the path.
func Suffix2[T, U ast.Node](values protopath.Values) (
out struct {
T T
TIndex int
U U
UIndex int
},
ok bool,
) {
if len(values.Path) < 2 {
return
}
var tailIdx int
// find U
out.U, tailIdx, ok = initSuffixMatch[U](values)
if !ok {
return
}
out.UIndex = tailIdx
tailIdx--
// walk up to find T
out.T, ok = suffixMatchRev[T](values, &tailIdx)
if !ok {
return
}
out.TIndex = tailIdx
return
}
func Suffix3[T, U, V ast.Node](values protopath.Values) (
out struct {
T T
TIndex int
U U
UIndex int
V V
VIndex int
},
ok bool,
) {
if len(values.Path) < 3 {
return
}
var tailIdx int
// find V
out.V, tailIdx, ok = initSuffixMatch[V](values)
if !ok {
return
}
out.VIndex = tailIdx
tailIdx--
// walk up to find U
out.U, ok = suffixMatchRev[U](values, &tailIdx)
if !ok {
return
}
out.UIndex = tailIdx
tailIdx--
// walk up to find T
out.T, ok = suffixMatchRev[T](values, &tailIdx)
if !ok {
return
}
out.TIndex = tailIdx
return
}
func Suffix4[T, U, V, W ast.Node](values protopath.Values) (
out struct {
T T
TIndex int
U U
UIndex int
V V
VIndex int
W W
WIndex int
},
ok bool,
) {
if len(values.Path) < 4 {
return
}
var tailIdx int
// find W
out.W, tailIdx, ok = initSuffixMatch[W](values)
if !ok {
return
}
out.WIndex = tailIdx
tailIdx--
// walk up to find V
out.V, ok = suffixMatchRev[V](values, &tailIdx)
if !ok {
return
}
out.VIndex = tailIdx
tailIdx--
// walk up to find U
out.U, ok = suffixMatchRev[U](values, &tailIdx)
if !ok {
return
}
out.UIndex = tailIdx
tailIdx--
// walk up to find T
out.T, ok = suffixMatchRev[T](values, &tailIdx)
if !ok {
return
}
out.TIndex = tailIdx
return
}
func Suffix5[T, U, V, W, X ast.Node](values protopath.Values) (
out struct {
T T
TIndex int
U U
UIndex int
V V
VIndex int
W W
WIndex int
X X
XIndex int
},
ok bool,
) {
if len(values.Path) < 5 {
return
}
var tailIdx int
// find X
out.X, tailIdx, ok = initSuffixMatch[X](values)
if !ok {
return
}
out.XIndex = tailIdx
tailIdx--
// walk up to find W
out.W, ok = suffixMatchRev[W](values, &tailIdx)
if !ok {
return
}
out.WIndex = tailIdx
tailIdx--
// walk up to find V
out.V, ok = suffixMatchRev[V](values, &tailIdx)
if !ok {
return
}
out.VIndex = tailIdx
tailIdx--
// walk up to find U
out.U, ok = suffixMatchRev[U](values, &tailIdx)
if !ok {
return
}
out.UIndex = tailIdx
tailIdx--
// walk up to find T
out.T, ok = suffixMatchRev[T](values, &tailIdx)
if !ok {
return
}
out.TIndex = tailIdx
return
}
func initSuffixMatch[T ast.Node](values protopath.Values) (t T, tailIdx int, ok bool) {
last := values.Index(-1)
tailIdx = len(values.Path) - 1
switch last.Step.Kind() {
case protopath.FieldAccessStep:
// last.Value MUST be a message type, otherwise the given path is invalid
t, ok = last.Value.Message().Interface().(T)
case protopath.ListIndexStep:
t, ok = last.Value.Message().Interface().(T)
tailIdx-- // skip over the list index step
}
return
}
func suffixMatchRev[T ast.Node](values protopath.Values, tailIdx *int) (_ T, _ bool) {
for *tailIdx >= 0 {
prev := values.Index(*tailIdx)
var t protoreflect.ProtoMessage
switch prev.Step.Kind() {
case protopath.RootStep:
t = prev.Value.Message().Interface()
case protopath.FieldAccessStep:
fd := prev.Step.FieldDescriptor()
if fd.IsList() {
t = prev.Value.List().Get(int(prev.Step.ListIndex())).Message().Interface()
} else {
t = prev.Value.Message().Interface()
}
case protopath.ListIndexStep:
*tailIdx--
continue
default:
panic("unsupported AST step kind: " + prev.Step.Kind().String())
}
switch t := t.(type) {
case T:
return t, true
case ast.WrapperNode:
// if T is actually a wrapper type, the previous case will be chosen
*tailIdx--
default:
return // passed through a different node type before T
}
}
panic("malformed path: " + values.String())
}