forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_passvisitor_test.go
67 lines (53 loc) · 1.02 KB
/
node_passvisitor_test.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
package pgs
import (
"fmt"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)
type fieldPrinter struct {
Visitor
}
func FieldPrinter() Visitor {
p := &fieldPrinter{}
p.Visitor = PassThroughVisitor(p)
return p
}
func (p fieldPrinter) VisitField(f Field) (Visitor, error) {
fmt.Println(f.Name())
return nil, nil
}
func ExamplePassThroughVisitor() {
n := fieldNode()
p := FieldPrinter()
if err := Walk(p, n); err != nil {
panic(err)
}
// Output:
// Foo
// Bar
}
func fieldNode() Node {
// simulating the following proto file:
//
// syntax="proto3";
//
// package fizz;
//
// message Gadget {
// string Bar = 1;
//
// message Gizmo {
// int Foo = 1;
// }
// }
sm := &msg{}
sm.addField(&field{desc: &descriptor.FieldDescriptorProto{Name: proto.String("Foo")}})
m := &msg{}
m.addMessage(sm)
m.addField(&field{desc: &descriptor.FieldDescriptorProto{Name: proto.String("Bar")}})
f := &file{}
f.addMessage(m)
p := &pkg{}
p.addFile(f)
return p
}