-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
280 lines (266 loc) · 7.39 KB
/
helpers.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
package protoutil
import (
"errors"
"fmt"
"github.com/emicklei/proto"
)
// AddAfterSyntax tries to add the given Visitee after the 'syntax' statement.
// If no syntax statement is found, returns an error.
func AddAfterSyntax(f *proto.Proto, v proto.Visitee) error {
// return false to immediately stop
inserted := false
Apply(f, nil, func(c *Cursor) bool {
if _, ok := c.Node().(*proto.Syntax); ok {
c.InsertAfter(v)
inserted = true
return false
}
// Continue until we insert.
return true
})
if inserted {
return nil
}
return errors.New("could not find syntax statement")
}
// AddAfterPackage tries to add the given Visitee after the 'package' statement.
// If no package statement is found, returns an error.
func AddAfterPackage(f *proto.Proto, v proto.Visitee) error {
inserted := false
Apply(f, nil, func(c *Cursor) bool {
if _, ok := c.Node().(*proto.Package); ok {
c.InsertAfter(v)
inserted = true
return false
}
// Continue until we insert.
return true
})
if inserted {
return nil
}
return errors.New("could not find package statement")
}
// Fallback logic, try and use import after a package and if that fails
// attempts to use it after a syntax statement.
// If that fails, returns an error.
func importFallback(f *proto.Proto, imp *proto.Import) error {
if err := AddAfterPackage(f, imp); err != nil {
if err = AddAfterSyntax(f, imp); err != nil {
return err
}
}
return nil
}
// AddImports attempts to add the given import *after* any other imports
// in the file.
//
// If fallback is supplied, attempts to add it after the 'package'
// statement and then the 'syntax' statement are made.
//
// If none of the attempts are successful, returns an error.
func AddImports(f *proto.Proto, fallback bool, imports ...*proto.Import) (err error) {
// No effect.
if len(imports) == 0 {
return nil
}
importMap, inserted := make(map[string]*proto.Import), false
for _, i := range imports {
importMap[i.Filename] = i
}
Apply(f, nil, func(c *Cursor) bool {
if i, ok := c.Node().(*proto.Import); ok {
delete(importMap, i.Filename)
if next, ok := c.Next(); ok {
if _, ok := next.(*proto.Import); ok {
return true
}
for _, imp := range importMap {
c.InsertAfter(imp)
}
inserted = true
return false
}
// We're at the end (no Next())
for _, imp := range importMap {
c.InsertAfter(imp)
}
inserted = true
return false
}
return true
})
// return if inserted.
if inserted {
return nil
}
// else fallback if defined.
if fallback {
// if the number of imports is > 1, we can try and insert the first after
// the package/syntax and then recurse into AddImport with the rest (which we'll)
// know that we can insert after an import since we just added it.
imports = []*proto.Import{}
for _, imp := range importMap {
imports = append(imports, imp)
}
if len(imports) == 0 {
return nil
}
if err := importFallback(f, imports[0]); err != nil {
return err
}
// recurse with the rest. (might be empty)
return AddImports(f, false, imports[1:]...)
}
return errors.New("unable to add import, no import statements found")
}
// NextUniqueID goes through the fields of the given Message and returns
// an id > max(fieldIds). It does not try to 'plug the holes' by selecting the
// least available id.
//
// // In 'example.proto' file
// syntax = "proto3"
//
// message Hello {
// string g = 1;
// string foo = 2;
// int32 bar = 3;
// int64 baz = 5;
// }
// f := ParseProtoPath("example.proto")
// m := GetMessageByName(f, "Hello")
// NextUniqueID(m) // 6
func NextUniqueID(m *proto.Message) int {
// Best to recurse through elements directly here since
// messages can embed other messages and the Apply could get
// hairy.
// if no elements exist => 1.
max := 0
for _, el := range m.Elements {
if f, ok := el.(*proto.NormalField); ok {
if f.Sequence > max {
max = f.Sequence
}
}
}
return max + 1
}
// GetMessageByName returns the message with the given name or nil if not found.
// Only traverses in proto.Proto and proto.Message since they are the only nodes
// that contain messages:
//
// f, _ := ParseProtoPath("foo.proto")
// m := GetMessageByName(f, "Foo")
// m.Name // "Foo"
func GetMessageByName(f *proto.Proto, name string) (node *proto.Message, err error) {
node, err = nil, nil
found := false
Apply(f,
func(c *Cursor) bool {
if m, ok := c.Node().(*proto.Message); ok {
if m.Name == name {
found = true
node = m
return false
}
// keep looking if we're in a Message
return true
}
// keep looking while we're in a proto.Proto.
_, ok := c.Node().(*proto.Proto)
return ok
},
// return immediately iff found.
func(c *Cursor) bool { return !found })
if found {
return
}
return nil, fmt.Errorf("message %s not found", name)
}
// GetServiceByName returns the service with the given name or nil if not found.
// Only traverses in proto.Proto since it is the only node that contain services:
//
// f, _ := ParseProtoPath("foo.proto")
// s := GetServiceByName(f, "FooSrv")
// s.Name // "FooSrv"
func GetServiceByName(f *proto.Proto, name string) (node *proto.Service, err error) {
node, err = nil, nil
found := false
Apply(f,
func(c *Cursor) bool {
if s, ok := c.Node().(*proto.Service); ok {
if s.Name == name {
found = true
node = s
}
// No nested services
return false
}
// keep looking while we're in a proto.Proto.
_, ok := c.Node().(*proto.Proto)
return ok
},
// return immediately iff found.
func(c *Cursor) bool { return !found })
if found {
return
}
return nil, fmt.Errorf("service %s not found", name)
}
// GetImportByPath returns the import with the given path or nil if not found.
// Only traverses in proto.Proto since it is the only node that contain imports:
//
// f, _ := ParseProtoPath("foo.proto")
// s := GetImportByPath(f, "other.proto")
// s.FileName // "other.proto"
func GetImportByPath(f *proto.Proto, path string) (node *proto.Import, err error) {
found := false
node, err = nil, nil
Apply(f,
func(c *Cursor) bool {
if i, ok := c.Node().(*proto.Import); ok {
if i.Filename == path {
found = true
node = i
}
// No nested imports
return false
}
// keep looking while we're in a proto.Proto.
_, ok := c.Node().(*proto.Proto)
return ok
},
// return immediately iff found.
func(c *Cursor) bool { return !found })
if found {
return
}
return nil, fmt.Errorf("import %s not found", path)
}
// HasMessage returns true if the given message is found in the given file.
//
// f, _ := ParseProtoPath("foo.proto")
// // true if 'foo.proto' contains message Foo { ... }
// r := HasMessage(f, "Foo")
func HasMessage(f *proto.Proto, name string) bool {
_, err := GetMessageByName(f, name)
return err == nil
}
// HasService returns true if the given service is found in the given file.
//
// f, _ := ParseProtoPath("foo.proto")
// // true if 'foo.proto' contains service FooSrv { ... }
// r := HasService(f, "FooSrv")
func HasService(f *proto.Proto, name string) bool {
_, err := GetServiceByName(f, name)
return err == nil
}
// HasImport returns true if the given import (by path) is found in the given file.
//
// f, _ := ParseProtoPath("foo.proto")
// // true if 'foo.proto' contains import "path.to.other.proto"
// r := HasImport(f, "path.to.other.proto")
func HasImport(f *proto.Proto, path string) bool {
_, err := GetImportByPath(f, path)
return err == nil
}