-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalk.go
More file actions
36 lines (33 loc) · 995 Bytes
/
Copy pathwalk.go
File metadata and controls
36 lines (33 loc) · 995 Bytes
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
package qshape
import (
pg_query "github.com/pganalyze/pg_query_go/v6"
"google.golang.org/protobuf/reflect/protoreflect"
)
// WalkNodes visits n and every Node reachable from it, covering all
// pg_query node types via protobuf reflection. forEachChild stays for
// walks that must remain selective (stripSortClause).
func WalkNodes(n *pg_query.Node, visit func(*pg_query.Node)) {
if n == nil {
return
}
walkNodeMessage(n.ProtoReflect(), visit)
}
func walkNodeMessage(m protoreflect.Message, visit func(*pg_query.Node)) {
if node, ok := m.Interface().(*pg_query.Node); ok {
visit(node)
}
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
switch {
case fd.Kind() != protoreflect.MessageKind && fd.Kind() != protoreflect.GroupKind:
return true
case fd.IsList():
list := v.List()
for i := 0; i < list.Len(); i++ {
walkNodeMessage(list.Get(i).Message(), visit)
}
default:
walkNodeMessage(v.Message(), visit)
}
return true
})
}