forked from uber/prototool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
471 lines (402 loc) · 15.1 KB
/
extract.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package extract is used to extract elements from reflect PackageSets.
package extract
import (
"fmt"
"github.com/uber/prototool/internal/protostrs"
reflectv1 "github.com/uber/prototool/internal/reflect/gen/uber/proto/reflect/v1"
)
// PackageSet is the Golang wrapper for the Protobuf PackageSet object.
type PackageSet struct {
protoMessage *reflectv1.PackageSet
packageNameToPackage map[string]*Package
}
// ProtoMessage returns the underlying Protobuf message.
func (p *PackageSet) ProtoMessage() *reflectv1.PackageSet {
return p.protoMessage
}
// PackageNameToPackage returns a map from package name to Package.
func (p *PackageSet) PackageNameToPackage() map[string]*Package {
return p.packageNameToPackage
}
// WithoutBeta makes a copy of the PackageSet without any beta packages.
//
// Note that field type names may still refer to beta packages.
func (p *PackageSet) WithoutBeta() (*PackageSet, error) {
return newPackageSet(p.protoMessage, true)
}
// Package is the Golang wrapper for the Protobuf Package object.
type Package struct {
protoMessage *reflectv1.Package
packageSet *PackageSet
dependencyNameToDependency map[string]*Package
importerNameToImporter map[string]*Package
enumNameToEnum map[string]*Enum
messageNameToMessage map[string]*Message
serviceNameToService map[string]*Service
}
// ProtoMessage returns the underlying Protobuf message.
func (p *Package) ProtoMessage() *reflectv1.Package {
return p.protoMessage
}
// FullyQualifiedName returns the fully-qualified name.
func (p *Package) FullyQualifiedName() string {
return p.protoMessage.Name
}
// PackageSet returns the parent PackageSet.
func (p *Package) PackageSet() *PackageSet {
return p.packageSet
}
// DependencyNameToDependency returns the direct dependencies of the given Package.
func (p *Package) DependencyNameToDependency() map[string]*Package {
return p.dependencyNameToDependency
}
// ImporterNameToImporter returns the direct importers of the given Package.
func (p *Package) ImporterNameToImporter() map[string]*Package {
return p.importerNameToImporter
}
// EnumNameToEnum returns the nested enums of the given Package.
func (p *Package) EnumNameToEnum() map[string]*Enum {
return p.enumNameToEnum
}
// MessageNameToMessage returns the nested messages of the given Package.
func (p *Package) MessageNameToMessage() map[string]*Message {
return p.messageNameToMessage
}
// ServiceNameToService returns the nested services of the given Package.
func (p *Package) ServiceNameToService() map[string]*Service {
return p.serviceNameToService
}
// Enum is the Golang wrapper for the Protobuf Enum object.
type Enum struct {
protoMessage *reflectv1.Enum
fullyQualifiedName string
valueNameToValue map[string]*EnumValue
valueNumberToValue map[int32]*EnumValue
}
// ProtoMessage returns the underlying Protobuf message.
func (e *Enum) ProtoMessage() *reflectv1.Enum {
return e.protoMessage
}
// FullyQualifiedName returns the fully-qualified name.
func (e *Enum) FullyQualifiedName() string {
return e.fullyQualifiedName
}
// ValueNameToValue returns the values of the given Enum.
func (e *Enum) ValueNameToValue() map[string]*EnumValue {
return e.valueNameToValue
}
// ValueNumberToValue returns the values of the given Enum.
func (e *Enum) ValueNumberToValue() map[int32]*EnumValue {
return e.valueNumberToValue
}
// EnumValue is the Golang wrapper for the Protobuf EnumValue object.
type EnumValue struct {
protoMessage *reflectv1.EnumValue
enum *Enum
}
// ProtoMessage returns the underlying Protobuf enum.
func (m *EnumValue) ProtoMessage() *reflectv1.EnumValue {
return m.protoMessage
}
// Enum returns the parent Enum.
func (m *EnumValue) Enum() *Enum {
return m.enum
}
// Message is the Golang wrapper for the Protobuf Message object.
type Message struct {
protoMessage *reflectv1.Message
fullyQualifiedName string
nestedEnumNameToEnum map[string]*Enum
nestedMessageNameToMessage map[string]*Message
fieldNameToField map[string]*MessageField
fieldNumberToField map[int32]*MessageField
oneofNameToOneof map[string]*MessageOneof
}
// ProtoMessage returns the underlying Protobuf message.
func (m *Message) ProtoMessage() *reflectv1.Message {
return m.protoMessage
}
// FullyQualifiedName returns the fully-qualified name.
func (m *Message) FullyQualifiedName() string {
return m.fullyQualifiedName
}
// NestedEnumNameToEnum returns the nested enums of the given Message.
func (m *Message) NestedEnumNameToEnum() map[string]*Enum {
return m.nestedEnumNameToEnum
}
// NestedMessageNameToMessage returns the nested messages of the given Message.
func (m *Message) NestedMessageNameToMessage() map[string]*Message {
return m.nestedMessageNameToMessage
}
// FieldNameToField returns the fields of the given Message.
func (m *Message) FieldNameToField() map[string]*MessageField {
return m.fieldNameToField
}
// FieldNumberToField returns the fields of the given Message.
func (m *Message) FieldNumberToField() map[int32]*MessageField {
return m.fieldNumberToField
}
// OneofNameToOneof returns the oneofs of the given Message.
func (m *Message) OneofNameToOneof() map[string]*MessageOneof {
return m.oneofNameToOneof
}
// MessageField is the Golang wrapper for the Protobuf MessageField object.
type MessageField struct {
protoMessage *reflectv1.MessageField
message *Message
messageOneof *MessageOneof
}
// ProtoMessage returns the underlying Protobuf message.
func (m *MessageField) ProtoMessage() *reflectv1.MessageField {
return m.protoMessage
}
// Message returns the parent Message.
func (m *MessageField) Message() *Message {
return m.message
}
// MessageOneof returns the parent MessageOneof.
//
// This will be nil if this field is not part of a oneof.
func (m *MessageField) MessageOneof() *MessageOneof {
return m.messageOneof
}
// MessageOneof is the Golang wrapper for the Protobuf MessageOneof object.
type MessageOneof struct {
protoMessage *reflectv1.MessageOneof
message *Message
fieldNameToField map[string]*MessageField
fieldNumberToField map[int32]*MessageField
}
// ProtoMessage returns the underlying Protobuf message.
func (m *MessageOneof) ProtoMessage() *reflectv1.MessageOneof {
return m.protoMessage
}
// Message returns the parent Message.
func (m *MessageOneof) Message() *Message {
return m.message
}
// FieldNameToField returns the fields of the given MessageOneof.
func (m *MessageOneof) FieldNameToField() map[string]*MessageField {
return m.fieldNameToField
}
// FieldNumberToField returns the fields of the given MessageOneof.
func (m *MessageOneof) FieldNumberToField() map[int32]*MessageField {
return m.fieldNumberToField
}
// Service is the Golang wrapper for the Protobuf Service object.
type Service struct {
protoMessage *reflectv1.Service
fullyQualifiedName string
methodNameToMethod map[string]*ServiceMethod
}
// ProtoMessage returns the underlying Protobuf message.
func (s *Service) ProtoMessage() *reflectv1.Service {
return s.protoMessage
}
// FullyQualifiedName returns the fully-qualified name.
func (s *Service) FullyQualifiedName() string {
return s.fullyQualifiedName
}
// MethodNameToMethod returns the methods of the given Service.
func (s *Service) MethodNameToMethod() map[string]*ServiceMethod {
return s.methodNameToMethod
}
// ServiceMethod is the Golang wrapper for the Protobuf ServiceMethod object.
type ServiceMethod struct {
protoMessage *reflectv1.ServiceMethod
service *Service
}
// ProtoMessage returns the underlying Protobuf service.
func (m *ServiceMethod) ProtoMessage() *reflectv1.ServiceMethod {
return m.protoMessage
}
// Service returns the parent Service.
func (m *ServiceMethod) Service() *Service {
return m.service
}
// NewPackageSet returns a new PackageSet for the given reflect PackageSet.
func NewPackageSet(protoMessage *reflectv1.PackageSet) (*PackageSet, error) {
return newPackageSet(protoMessage, false)
}
func newPackageSet(protoMessage *reflectv1.PackageSet, withoutBeta bool) (*PackageSet, error) {
packageSet := &PackageSet{
protoMessage: protoMessage,
packageNameToPackage: make(map[string]*Package),
}
for _, pkg := range packageSet.protoMessage.Packages {
if !ignorePackage(withoutBeta, pkg.Name) {
packageSet.packageNameToPackage[pkg.Name] = &Package{
protoMessage: pkg,
packageSet: packageSet,
dependencyNameToDependency: make(map[string]*Package),
importerNameToImporter: make(map[string]*Package),
enumNameToEnum: make(map[string]*Enum),
messageNameToMessage: make(map[string]*Message),
serviceNameToService: make(map[string]*Service),
}
}
}
for packageName, pkg := range packageSet.packageNameToPackage {
for _, dependencyName := range pkg.protoMessage.DependencyNames {
dependency, ok := packageSet.packageNameToPackage[dependencyName]
if !ok {
if ignorePackage(withoutBeta, dependencyName) {
continue
}
return nil, fmt.Errorf("no package for name %s", dependencyName)
}
pkg.dependencyNameToDependency[dependencyName] = dependency
dependency.importerNameToImporter[packageName] = pkg
}
}
for packageName, pkg := range packageSet.packageNameToPackage {
for _, enum := range pkg.protoMessage.Enums {
pkg.enumNameToEnum[enum.Name] = newEnum(enum, packageName)
}
for _, message := range pkg.protoMessage.Messages {
extractMessage, err := newMessage(message, packageName)
if err != nil {
return nil, err
}
pkg.messageNameToMessage[message.Name] = extractMessage
}
for _, service := range pkg.protoMessage.Services {
pkg.serviceNameToService[service.Name] = newService(service, packageName)
}
}
return packageSet, nil
}
func newEnum(protoMessage *reflectv1.Enum, encapsulatingFullyQualifiedName string) *Enum {
enum := &Enum{
protoMessage: protoMessage,
fullyQualifiedName: getFullyQualifiedName(encapsulatingFullyQualifiedName, protoMessage.Name),
valueNameToValue: make(map[string]*EnumValue),
valueNumberToValue: make(map[int32]*EnumValue),
}
for _, value := range protoMessage.EnumValues {
enumValue := newEnumValue(value, enum)
enum.valueNameToValue[value.Name] = enumValue
enum.valueNumberToValue[value.Number] = enumValue
}
return enum
}
func newEnumValue(protoMessage *reflectv1.EnumValue, enum *Enum) *EnumValue {
return &EnumValue{
protoMessage: protoMessage,
enum: enum,
}
}
func newMessage(protoMessage *reflectv1.Message, encapsulatingFullyQualifiedName string) (*Message, error) {
message := &Message{
protoMessage: protoMessage,
fullyQualifiedName: getFullyQualifiedName(encapsulatingFullyQualifiedName, protoMessage.Name),
nestedEnumNameToEnum: make(map[string]*Enum),
nestedMessageNameToMessage: make(map[string]*Message),
fieldNameToField: make(map[string]*MessageField),
fieldNumberToField: make(map[int32]*MessageField),
oneofNameToOneof: make(map[string]*MessageOneof),
}
for _, nestedEnum := range protoMessage.NestedEnums {
message.nestedEnumNameToEnum[nestedEnum.Name] = newEnum(nestedEnum, message.fullyQualifiedName)
}
for _, nestedMessage := range protoMessage.NestedMessages {
extractMessage, err := newMessage(nestedMessage, message.fullyQualifiedName)
if err != nil {
return nil, err
}
message.nestedMessageNameToMessage[nestedMessage.Name] = extractMessage
}
for _, field := range protoMessage.MessageFields {
messageField := newMessageField(field, message)
message.fieldNameToField[field.Name] = messageField
message.fieldNumberToField[field.Number] = messageField
}
for _, oneof := range protoMessage.MessageOneofs {
messageOneof := newMessageOneof(oneof, message)
message.oneofNameToOneof[oneof.Name] = messageOneof
}
if err := linkMessageFieldsOneofs(message.fieldNumberToField, message.oneofNameToOneof); err != nil {
return nil, err
}
return message, nil
}
func newMessageField(protoMessage *reflectv1.MessageField, message *Message) *MessageField {
return &MessageField{
protoMessage: protoMessage,
message: message,
}
}
func newMessageOneof(protoMessage *reflectv1.MessageOneof, message *Message) *MessageOneof {
return &MessageOneof{
protoMessage: protoMessage,
message: message,
fieldNameToField: make(map[string]*MessageField),
fieldNumberToField: make(map[int32]*MessageField),
}
}
func newService(protoMessage *reflectv1.Service, encapsulatingFullyQualifiedName string) *Service {
service := &Service{
protoMessage: protoMessage,
fullyQualifiedName: getFullyQualifiedName(encapsulatingFullyQualifiedName, protoMessage.Name),
methodNameToMethod: make(map[string]*ServiceMethod),
}
for _, method := range protoMessage.ServiceMethods {
serviceMethod := newServiceMethod(method, service)
service.methodNameToMethod[method.Name] = serviceMethod
}
return service
}
func newServiceMethod(protoMessage *reflectv1.ServiceMethod, service *Service) *ServiceMethod {
return &ServiceMethod{
protoMessage: protoMessage,
service: service,
}
}
func linkMessageFieldsOneofs(fieldNumberToField map[int32]*MessageField, oneofNameToOneof map[string]*MessageOneof) error {
for oneofName, oneof := range oneofNameToOneof {
for _, fieldNumber := range oneof.protoMessage.FieldNumbers {
field, ok := fieldNumberToField[fieldNumber]
if !ok {
return fmt.Errorf("oneof %s has field number %d which is not in the encapsulating message", oneofName, fieldNumber)
}
field.messageOneof = oneof
oneof.fieldNameToField[field.protoMessage.Name] = field
oneof.fieldNumberToField[field.protoMessage.Number] = field
}
}
return nil
}
func getFullyQualifiedName(encapsulatingFullyQualifiedName string, name string) string {
if encapsulatingFullyQualifiedName == "" {
return name
}
return encapsulatingFullyQualifiedName + "." + name
}
func ignorePackage(withoutBeta bool, packageName string) bool {
// if we are not ignoring beta packages, do not ignore
if !withoutBeta {
return false
}
// betaVersion is 0 if we can't parse this into a beta package
_, betaVersion, _ := protostrs.MajorBetaVersion(packageName)
return betaVersion > 0
}