forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_nilvisitor_test.go
64 lines (51 loc) · 1.03 KB
/
node_nilvisitor_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
package pgs
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
type enumPrinter struct {
Visitor
}
func EnumPrinter() Visitor { return enumPrinter{NilVisitor()} }
func (p enumPrinter) VisitMessage(m Message) (Visitor, error) { return p, nil }
func (p enumPrinter) VisitEnum(e Enum) (Visitor, error) {
fmt.Println(e.Name())
return nil, nil
}
func ExampleNilVisitor() {
n := enumNode()
p := EnumPrinter()
if err := Walk(p, n); err != nil {
panic(err)
}
// Output:
// Bar
// Foo
}
func enumNode() Node {
// simulating the following proto file:
//
// syntax="proto3";
//
// package fizz;
//
// message Gadget {
//
// enum Bar {
// // ...
// }
//
// message Gizmo {
// enum Foo {
// // ...
// }
// }
// }
sm := &msg{}
sm.addEnum(&enum{desc: &descriptor.EnumDescriptorProto{Name: proto.String("Foo")}})
m := &msg{}
m.addMessage(sm)
m.addEnum(&enum{desc: &descriptor.EnumDescriptorProto{Name: proto.String("Bar")}})
return m
}