Skip to content

Commit 047fc76

Browse files
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
1 parent 9ea682d commit 047fc76

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ public void MessageDescriptor()
204204
TestDescriptorToProto(messageType.ToProto, messageType.Proto);
205205
}
206206

207+
[Test]
208+
public void MessageDescriptor_IsMapEntry()
209+
{
210+
var testMapMessage = TestMap.Descriptor;
211+
Assert.False(testMapMessage.IsMapEntry);
212+
Assert.True(testMapMessage.Fields[1].MessageType.IsMapEntry);
213+
}
214+
207215
[Test]
208216
public void FieldDescriptor_GeneratedCode()
209217
{

csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Ty
242242
/// <summary>
243243
/// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise.
244244
/// </summary>
245-
public bool IsMap => FieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry;
245+
public bool IsMap => FieldType == FieldType.Message && messageType.IsMapEntry;
246246

247247
/// <summary>
248248
/// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise.

csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ internal bool IsExtensionsInitialized(IMessage message)
202202
/// </summary>
203203
internal bool IsWrapperType => File.Package == "google.protobuf" && File.Name == "google/protobuf/wrappers.proto";
204204

205+
/// <summary>
206+
/// Returns whether this message was synthetically-created to store key/value pairs in a
207+
/// map field.
208+
/// </summary>
209+
public bool IsMapEntry => Proto.Options?.MapEntry == true;
210+
205211
/// <value>
206212
/// If this is a nested type, get the outer descriptor, otherwise null.
207213
/// </value>

0 commit comments

Comments
 (0)