-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobuf.go
158 lines (138 loc) · 3.1 KB
/
protobuf.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
package godin
import (
"os"
"strings"
"github.com/emicklei/proto"
)
func Parse(protoPath string) (*Context, error) {
reader, err := os.Open(protoPath)
if err != nil {
return nil, err
}
defer reader.Close()
parser := proto.NewParser(reader)
def, err := parser.Parse()
if err != nil {
return nil, err
}
ctx := &Context{}
proto.Walk(
def,
proto.WithService(parseService(ctx)),
proto.WithEnum(parseEnum(ctx)),
proto.WithMessage(parseMessage(ctx)),
WithPackage(parsePackage(ctx)),
)
return ctx, nil
}
func WithPackage(apply func(*proto.Package)) proto.Handler {
return func(v proto.Visitee) {
if s, ok := v.(*proto.Package); ok {
apply(s)
}
}
}
func parsePackage(ctx *Context) func(p *proto.Package) {
return func(p *proto.Package) {
ctx.Package = p.Name
}
}
func parseMessage(ctx *Context) func(m *proto.Message) {
return func(m *proto.Message) {
msg := Message{
Name: m.Name,
}
if m.Comment != nil {
for _, line := range m.Comment.Lines {
line = strings.TrimLeft(line, "// ")
line = strings.TrimRight(line, "// ")
msg.Comments = append(msg.Comments, line)
}
}
for _, elem := range m.Elements {
switch elem.(type) {
default:
continue
case *proto.Comment:
continue
case *proto.NormalField:
f := elem.(*proto.NormalField)
msg.Fields = append(msg.Fields, MessageField{
Name: f.Name,
Type: f.Type,
Order: f.Sequence,
Repeated: f.Repeated,
})
}
}
ctx.Messages = append(ctx.Messages, msg)
}
}
func parseEnum(ctx *Context) func(e *proto.Enum) {
return func(e *proto.Enum) {
enum := Enum{
Name: e.Name,
}
if e.Comment != nil {
for _, line := range e.Comment.Lines {
line = strings.TrimLeft(line, "// ")
line = strings.TrimRight(line, "// ")
enum.Comments = append(enum.Comments, line)
}
}
for _, elem := range e.Elements {
switch elem.(type) {
default:
continue
case *proto.Comment:
continue
case *proto.EnumField:
f := elem.(*proto.EnumField)
field := EnumField{
Name: f.Name,
Order: f.Integer,
}
enum.Fields = append(enum.Fields, field)
}
}
ctx.Enums = append(ctx.Enums, enum)
}
}
func parseService(ctx *Context) func(s *proto.Service) {
return func(s *proto.Service) {
svc := Service{
Name: s.Name,
}
if s.Comment != nil {
for _, line := range s.Comment.Lines {
line = strings.TrimLeft(line, "// ")
line = strings.TrimRight(line, "// ")
svc.Comments = append(svc.Comments, line)
}
}
for _, elem := range s.Elements {
switch elem.(type) {
default:
continue
case *proto.Comment:
continue
case *proto.RPC:
meth := elem.(*proto.RPC)
rpc := RPC{
Name: meth.Name,
RequestType: meth.RequestType,
ResponseType: meth.ReturnsType,
}
if meth.Comment != nil {
for _, line := range meth.Comment.Lines {
line = strings.TrimLeft(line, "// ")
line = strings.TrimRight(line, "// ")
rpc.Comments = append(rpc.Comments, line)
}
}
svc.RPCs = append(svc.RPCs, rpc)
}
}
ctx.Services = append(ctx.Services, svc)
}
}