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
pull/16647/head
Protobuf Team Bot 2024-04-25 08:39:30 -07:00 committed by Copybara-Service
parent 9ea682d1ee
commit 047fc7673e
3 changed files with 15 additions and 1 deletions

View File

@ -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()
{

View File

@ -242,7 +242,7 @@ namespace Google.Protobuf.Reflection
/// <summary>
/// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise.
/// </summary>
public bool IsMap => FieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry;
public bool IsMap => FieldType == FieldType.Message && messageType.IsMapEntry;
/// <summary>
/// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise.

View File

@ -202,6 +202,12 @@ namespace Google.Protobuf.Reflection
/// </summary>
internal bool IsWrapperType => File.Package == "google.protobuf" && File.Name == "google/protobuf/wrappers.proto";
/// <summary>
/// Returns whether this message was synthetically-created to store key/value pairs in a
/// map field.
/// </summary>
public bool IsMapEntry => Proto.Options?.MapEntry == true;
/// <value>
/// If this is a nested type, get the outer descriptor, otherwise null.
/// </value>