-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.go
60 lines (55 loc) · 1.46 KB
/
proto.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
)
func registerFileDescriptorSets(filenames []string) error {
for _, descSetFileName := range filenames {
b, err := ioutil.ReadFile(descSetFileName)
if os.IsNotExist(err) {
continue
}
if err != nil {
return err
}
var fdset descriptorpb.FileDescriptorSet
if err := proto.Unmarshal(b, &fdset); err != nil {
return err
}
for _, fdp := range fdset.File {
if err := registerFileDescriptor(fdp); err != nil {
return fmt.Errorf("fdset %s, file %s: %w", descSetFileName, fdp.GetName(), err)
}
}
}
return nil
}
func registerFileDescriptors(fds []*desc.FileDescriptor) error {
for _, fd := range fds {
if err := registerFileDescriptor(fd.AsFileDescriptorProto()); err != nil {
return err
}
}
return nil
}
func registerFileDescriptor(fdp *descriptorpb.FileDescriptorProto) error {
fdr, err := protodesc.NewFile(fdp, protoregistry.GlobalFiles)
if err != nil {
return err
}
if err := protoregistry.GlobalFiles.RegisterFile(fdr); err != nil {
return err
}
for i := 0; i < fdr.Messages().Len(); i++ {
mt := dynamicpb.NewMessageType(fdr.Messages().Get(i))
protoregistry.GlobalTypes.RegisterMessage(mt)
}
return nil
}