-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.go
48 lines (43 loc) · 1.46 KB
/
parse.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
package collector
import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
type DescriptorWithPath struct {
*descriptor.FileDescriptorProto
ProtoFilePath string
}
func (file *DescriptorWithPath) GetMessage(typeName string) *descriptor.DescriptorProto {
for _, msg := range file.GetMessageType() {
if msg.GetName() == typeName {
return msg
}
}
return nil
}
func filterDuplicateDescriptors(descriptors []*DescriptorWithPath) []*DescriptorWithPath {
var uniqueDescriptors []*DescriptorWithPath
for _, desc := range descriptors {
unique, matchingDesc := isUnique(desc, uniqueDescriptors)
// if this proto file first came in as an import, but later as a solo-kit project proto,
// ensure the original proto gets updated with the correct proto file path
if !unique && matchingDesc.ProtoFilePath == "" {
matchingDesc.ProtoFilePath = desc.ProtoFilePath
}
if unique {
uniqueDescriptors = append(uniqueDescriptors, desc)
}
}
return uniqueDescriptors
}
// If it finds a matching proto, also returns the matching proto's file descriptor
func isUnique(desc *DescriptorWithPath, descriptors []*DescriptorWithPath) (bool, *DescriptorWithPath) {
for _, existing := range descriptors {
existingCopy := proto.Clone(existing.FileDescriptorProto).(*descriptor.FileDescriptorProto)
existingCopy.Name = desc.Name
if proto.Equal(existingCopy, desc.FileDescriptorProto) {
return false, existing
}
}
return true, nil
}