From acacf8158c9a307051c92dc233966e8324facd45 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 2 Mar 2021 17:38:46 -0800 Subject: [PATCH] all: rely on protodesc.ToFileDescriptorProto (#1214) 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. --- descriptor/descriptor.go | 11 +++-------- proto/registry.go | 10 ++-------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/descriptor/descriptor.go b/descriptor/descriptor.go index c3c4a2b87e..ffde8a6508 100644 --- a/descriptor/descriptor.go +++ b/descriptor/descriptor.go @@ -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 { diff --git a/proto/registry.go b/proto/registry.go index 1e7ff64205..066b4323b4 100644 --- a/proto/registry.go +++ b/proto/registry.go @@ -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" @@ -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.