From f7317e02e6f98573bc1f8cd21a7110bb43f5ebeb Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 4 Mar 2024 00:07:20 -0800 Subject: [PATCH] Refactor C# FieldDescriptor.FieldType for consistency. PiperOrigin-RevId: 612337931 --- .../Reflection/FieldDescriptor.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index c2c7239461..f46667ed07 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -21,7 +21,6 @@ namespace Google.Protobuf.Reflection private EnumDescriptor enumType; private MessageDescriptor extendeeType; private MessageDescriptor messageType; - private FieldType fieldType; private IFieldAccessor accessor; /// @@ -90,7 +89,12 @@ namespace Google.Protobuf.Reflection Proto = proto; if (proto.HasType) { - fieldType = GetFieldTypeFromProtoType(proto.Type); + FieldType = GetFieldTypeFromProtoType(proto.Type); + if (FieldType == FieldType.Message && + Features.MessageEncoding == FeatureSet.Types.MessageEncoding.Delimited) + { + FieldType = FieldType.Group; + } } if (FieldNumber <= 0) @@ -238,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.Proto.Options != null && messageType.Proto.Options.MapEntry; /// /// Returns true if this field is a packed, repeated field; false otherwise. @@ -253,8 +257,7 @@ namespace Google.Protobuf.Reflection /// /// Returns the type of the field. /// - public FieldType FieldType => fieldType == FieldType.Message && Features.MessageEncoding == FeatureSet.Types.MessageEncoding.Delimited - ? FieldType.Group : fieldType; + public FieldType FieldType { get; private set; } /// /// Returns the field number declared in the proto file. @@ -284,7 +287,7 @@ namespace Google.Protobuf.Reflection { get { - if (fieldType != FieldType.Enum) + if (FieldType != FieldType.Enum) { throw new InvalidOperationException("EnumType is only valid for enum fields."); } @@ -299,7 +302,7 @@ namespace Google.Protobuf.Reflection { get { - if (fieldType != FieldType.Message && fieldType != FieldType.Group) + if (FieldType != FieldType.Message && FieldType != FieldType.Group) { throw new InvalidOperationException("MessageType is only valid for message or group fields."); } @@ -382,11 +385,14 @@ namespace Google.Protobuf.Reflection // Choose field type based on symbol. if (typeDescriptor is MessageDescriptor) { - fieldType = FieldType.Message; + FieldType = + Features.MessageEncoding == FeatureSet.Types.MessageEncoding.Delimited + ? FieldType.Group + : FieldType.Message; } else if (typeDescriptor is EnumDescriptor) { - fieldType = FieldType.Enum; + FieldType = FieldType.Enum; } else { @@ -394,7 +400,7 @@ namespace Google.Protobuf.Reflection } } - if (fieldType == FieldType.Message || fieldType == FieldType.Group) + if (FieldType == FieldType.Message || FieldType == FieldType.Group) { if (typeDescriptor is not MessageDescriptor m) { @@ -407,7 +413,7 @@ namespace Google.Protobuf.Reflection throw new DescriptorValidationException(this, "Messages can't have default values."); } } - else if (fieldType == FieldType.Enum) + else if (FieldType == FieldType.Enum) { if (typeDescriptor is not EnumDescriptor e) { @@ -422,7 +428,7 @@ namespace Google.Protobuf.Reflection } else { - if (fieldType == FieldType.Message || fieldType == FieldType.Enum) + if (FieldType == FieldType.Message || FieldType == FieldType.Enum) { throw new DescriptorValidationException(this, "Field with message or enum type missing type_name."); }