-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_src.go
92 lines (83 loc) · 2.27 KB
/
proto_src.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
package xlsxcfg
import (
"fmt"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"google.golang.org/protobuf/types/descriptorpb"
)
type TypeProvidor interface {
MessageByName(string) *desc.MessageDescriptor
}
func LoadProtoFiles(importPaths []string, fileNames ...string) (tp TypeProvidor, err error) {
fileNames, err = protoparse.ResolveFilenames(importPaths, fileNames...)
if err != nil {
return
}
pr := protoparse.Parser{
ImportPaths: importPaths,
InferImportPaths: len(importPaths) == 0,
// IncludeSourceCodeInfo: true,
}
fds, err := pr.ParseFiles(fileNames...)
if err != nil {
err = fmt.Errorf("could not parse given proto files: %v", err)
return
}
tp, err = newTypeProvidor(fds...)
return
}
type protoTypeProvidor struct {
fds map[string]*desc.FileDescriptor
ss map[string]*desc.MessageDescriptor
}
func newTypeProvidor(fds ...*desc.FileDescriptor) (TypeProvidor, error) {
d := &protoTypeProvidor{
fds: make(map[string]*desc.FileDescriptor, len(fds)),
ss: make(map[string]*desc.MessageDescriptor, len(fds)),
}
for _, fd := range fds {
if err := addFile(fd, d.fds); err != nil {
return nil, err
}
for _, t := range fd.GetMessageTypes() {
d.ss[t.GetName()] = t
}
}
return d, nil
}
func addFile(fd *desc.FileDescriptor, fds map[string]*desc.FileDescriptor) error {
name := fd.GetName()
if existing, ok := fds[name]; ok {
// already added this file
if existing != fd {
// doh! duplicate files provided
return fmt.Errorf("given files include multiple copies of %s", name)
}
return nil
}
fds[name] = fd
for _, dep := range fd.GetDependencies() {
if err := addFile(dep, fds); err != nil {
return err
}
}
return nil
}
func (p *protoTypeProvidor) MessageByName(name string) *desc.MessageDescriptor {
return p.ss[name]
}
func IsStrField(md *desc.MessageDescriptor, path ...string) bool {
var fieldDesc *desc.FieldDescriptor
for _, fieldName := range path {
fieldDesc = md.FindFieldByName(fieldName)
if fieldDesc.IsMap() {
md = fieldDesc.GetMapValueType().GetMessageType()
} else if m := fieldDesc.GetMessageType(); m != nil {
md = m
}
}
if fieldDesc == nil {
return false
}
return *(fieldDesc.AsFieldDescriptorProto().Type) == descriptorpb.FieldDescriptorProto_TYPE_STRING
}