...
1
2
3
4
5 package impl
6
7 import (
8 "bytes"
9 "compress/gzip"
10 "io/ioutil"
11 "sync"
12
13 "google.golang.org/protobuf/internal/filedesc"
14 "google.golang.org/protobuf/reflect/protoreflect"
15 "google.golang.org/protobuf/reflect/protoregistry"
16 )
17
18
19
20
21
22
23
24
25
26 type (
27 enumV1 interface {
28 EnumDescriptor() ([]byte, []int)
29 }
30 messageV1 interface {
31 Descriptor() ([]byte, []int)
32 }
33 )
34
35 var legacyFileDescCache sync.Map
36
37
38
39
40
41
42
43 func legacyLoadFileDesc(b []byte) protoreflect.FileDescriptor {
44
45 if fd, ok := legacyFileDescCache.Load(&b[0]); ok {
46 return fd.(protoreflect.FileDescriptor)
47 }
48
49
50 zr, err := gzip.NewReader(bytes.NewReader(b))
51 if err != nil {
52 panic(err)
53 }
54 b2, err := ioutil.ReadAll(zr)
55 if err != nil {
56 panic(err)
57 }
58
59 fd := filedesc.Builder{
60 RawDescriptor: b2,
61 FileRegistry: resolverOnly{protoregistry.GlobalFiles},
62 }.Build().File
63 if fd, ok := legacyFileDescCache.LoadOrStore(&b[0], fd); ok {
64 return fd.(protoreflect.FileDescriptor)
65 }
66 return fd
67 }
68
69 type resolverOnly struct {
70 reg *protoregistry.Files
71 }
72
73 func (r resolverOnly) FindFileByPath(path string) (protoreflect.FileDescriptor, error) {
74 return r.reg.FindFileByPath(path)
75 }
76 func (r resolverOnly) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
77 return r.reg.FindDescriptorByName(name)
78 }
79 func (resolverOnly) RegisterFile(protoreflect.FileDescriptor) error {
80 return nil
81 }
82
View as plain text