Make IsDescriptor* helpers lenient about descriptor name (#9127)

Our internal version of the codebase has a different path and package
name for descriptor.proto, so this change updates IsDescriptorProto()
and IsDescriptorOptionMessage() to be able to handle both the internal
and external descriptor types.
This commit is contained in:
Adam Cozzette 2021-10-21 11:47:05 -07:00 committed by GitHub
parent e6430dd4a0
commit 42db8a3f96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 10 deletions

View File

@ -30,8 +30,10 @@
#include <memory>
#include <google/protobuf/any.pb.h>
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/io/printer.h>
@ -63,6 +65,17 @@ TEST(CSharpEnumValue, PascalCasedPrefixStripping) {
EXPECT_EQ("_2", GetEnumValueName("Foo", "FOO___2"));
}
TEST(DescriptorProtoHelpers, IsDescriptorProto) {
EXPECT_TRUE(IsDescriptorProto(DescriptorProto::descriptor()->file()));
EXPECT_FALSE(IsDescriptorProto(Any::descriptor()->file()));
}
TEST(DescriptorProtoHelpers, IsDescriptorOptionMessage) {
EXPECT_TRUE(IsDescriptorOptionMessage(FileOptions::descriptor()));
EXPECT_FALSE(IsDescriptorOptionMessage(Any::descriptor()));
EXPECT_FALSE(IsDescriptorOptionMessage(DescriptorProto::descriptor()));
}
} // namespace
} // namespace csharp
} // namespace compiler

View File

@ -130,7 +130,8 @@ uint GetGroupEndTag(const Descriptor* descriptor);
// descriptors etc, for use in the runtime. This is the only type which is
// allowed to use proto2 syntax, and it generates internal classes.
inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
return descriptor->name() == "google/protobuf/descriptor.proto";
return descriptor->name() == "google/protobuf/descriptor.proto" ||
descriptor->name() == "net/proto2/proto/descriptor.proto";
}
// Determines whether the given message is an options message within descriptor.proto.
@ -138,15 +139,15 @@ inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) {
if (!IsDescriptorProto(descriptor->file())) {
return false;
}
const std::string name = descriptor->full_name();
return name == "google.protobuf.FileOptions" ||
name == "google.protobuf.MessageOptions" ||
name == "google.protobuf.FieldOptions" ||
name == "google.protobuf.OneofOptions" ||
name == "google.protobuf.EnumOptions" ||
name == "google.protobuf.EnumValueOptions" ||
name == "google.protobuf.ServiceOptions" ||
name == "google.protobuf.MethodOptions";
const std::string name = descriptor->name();
return name == "FileOptions" ||
name == "MessageOptions" ||
name == "FieldOptions" ||
name == "OneofOptions" ||
name == "EnumOptions" ||
name == "EnumValueOptions" ||
name == "ServiceOptions" ||
name == "MethodOptions";
}
inline bool IsWrapperType(const FieldDescriptor* descriptor) {