From 047fc7673eb2a7373f7ad8f5b094de201d737b36 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 25 Apr 2024 08:39:30 -0700 Subject: [PATCH] Introduce `MessageDescriptor.IsMapType` in C# reflection to make it easier to tell whether a given message was generated by protoc for a map field. PiperOrigin-RevId: 628087118 --- .../Google.Protobuf.Test/Reflection/DescriptorsTest.cs | 8 ++++++++ csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs | 2 +- .../src/Google.Protobuf/Reflection/MessageDescriptor.cs | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs index 4bd857aacb..f908b2815c 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs @@ -204,6 +204,14 @@ namespace Google.Protobuf.Reflection TestDescriptorToProto(messageType.ToProto, messageType.Proto); } + [Test] + public void MessageDescriptor_IsMapEntry() + { + var testMapMessage = TestMap.Descriptor; + Assert.False(testMapMessage.IsMapEntry); + Assert.True(testMapMessage.Fields[1].MessageType.IsMapEntry); + } + [Test] public void FieldDescriptor_GeneratedCode() { diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 9d41930838..d9bf6b373f 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -242,7 +242,7 @@ namespace Google.Protobuf.Reflection /// /// Returns true if this field is a map field; false otherwise. /// - public bool IsMap => FieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry; + public bool IsMap => FieldType == FieldType.Message && messageType.IsMapEntry; /// /// Returns true if this field is a packed, repeated field; false otherwise. diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index ead4676a5c..b4b69ad6e3 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -202,6 +202,12 @@ namespace Google.Protobuf.Reflection /// internal bool IsWrapperType => File.Package == "google.protobuf" && File.Name == "google/protobuf/wrappers.proto"; + /// + /// Returns whether this message was synthetically-created to store key/value pairs in a + /// map field. + /// + public bool IsMapEntry => Proto.Options?.MapEntry == true; + /// /// If this is a nested type, get the outer descriptor, otherwise null. ///