Skip to content

Commit

Permalink
Introduce MessageDescriptor.IsMapType in C# reflection to make it e…
Browse files Browse the repository at this point in the history
…asier to tell whether a given message was generated by protoc for a map field.

PiperOrigin-RevId: 628087118
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 25, 2024
1 parent 9ea682d commit 047fc76
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public void MessageDescriptor()
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()
{
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Ty
/// <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.
Expand Down
6 changes: 6 additions & 0 deletions csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ internal bool IsExtensionsInitialized(IMessage message)
/// </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>
Expand Down

0 comments on commit 047fc76

Please sign in to comment.