Skip to content

Commit

Permalink
all: rely on protodesc.ToFileDescriptorProto (#1214)
Browse files Browse the repository at this point in the history
Use protodesc.ToFileDescriptorProto to retrieve the raw descriptors
for legacy support instead of the undocumented ProtoLegacyRawDesc method
that we expect v2 to provide.

This change will cause the legacy proto package to incur a dependency
on the descriptorpb package.
  • Loading branch information
dsnet committed Mar 3, 2021
1 parent eccd77d commit acacf81
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
11 changes: 3 additions & 8 deletions descriptor/descriptor.go
Expand Up @@ -79,14 +79,9 @@ func deriveRawDescriptor(d protoreflect.Descriptor) ([]byte, []int) {
}

// Obtain the raw file descriptor.
var raw []byte
switch fd := d.(type) {
case interface{ ProtoLegacyRawDesc() []byte }:
raw = fd.ProtoLegacyRawDesc()
case protoreflect.FileDescriptor:
raw, _ = proto.Marshal(protodesc.ToFileDescriptorProto(fd))
}
file := protoimpl.X.CompressGZIP(raw)
fd := d.(protoreflect.FileDescriptor)
b, _ := proto.Marshal(protodesc.ToFileDescriptorProto(fd))
file := protoimpl.X.CompressGZIP(b)

// Reverse the indexes, since we populated it in reverse.
for i, j := 0, len(idxs)-1; i < j; i, j = i+1, j-1 {
Expand Down
10 changes: 2 additions & 8 deletions proto/registry.go
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"sync"

"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"
Expand Down Expand Up @@ -62,14 +63,7 @@ func FileDescriptor(s filePath) fileDescGZIP {
// Find the descriptor in the v2 registry.
var b []byte
if fd, _ := protoregistry.GlobalFiles.FindFileByPath(s); fd != nil {
if fd, ok := fd.(interface{ ProtoLegacyRawDesc() []byte }); ok {
b = fd.ProtoLegacyRawDesc()
} else {
// TODO: Use protodesc.ToFileDescriptorProto to construct
// a descriptorpb.FileDescriptorProto and marshal it.
// However, doing so causes the proto package to have a dependency
// on descriptorpb, leading to cyclic dependency issues.
}
b, _ = Marshal(protodesc.ToFileDescriptorProto(fd))
}

// Locally cache the raw descriptor form for the file.
Expand Down

0 comments on commit acacf81

Please sign in to comment.