Refactor C# FieldDescriptor.FieldType for consistency.

PiperOrigin-RevId: 612337931
pull/16006/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 7a51eb5370
commit f7317e02e6
  1. 30
      csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs

@ -21,7 +21,6 @@ namespace Google.Protobuf.Reflection
private EnumDescriptor enumType; private EnumDescriptor enumType;
private MessageDescriptor extendeeType; private MessageDescriptor extendeeType;
private MessageDescriptor messageType; private MessageDescriptor messageType;
private FieldType fieldType;
private IFieldAccessor accessor; private IFieldAccessor accessor;
/// <summary> /// <summary>
@ -90,7 +89,12 @@ namespace Google.Protobuf.Reflection
Proto = proto; Proto = proto;
if (proto.HasType) 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) if (FieldNumber <= 0)
@ -238,7 +242,7 @@ namespace Google.Protobuf.Reflection
/// <summary> /// <summary>
/// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise. /// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise.
/// </summary> /// </summary>
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;
/// <summary> /// <summary>
/// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise. /// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise.
@ -253,8 +257,7 @@ namespace Google.Protobuf.Reflection
/// <summary> /// <summary>
/// Returns the type of the field. /// Returns the type of the field.
/// </summary> /// </summary>
public FieldType FieldType => fieldType == FieldType.Message && Features.MessageEncoding == FeatureSet.Types.MessageEncoding.Delimited public FieldType FieldType { get; private set; }
? FieldType.Group : fieldType;
/// <summary> /// <summary>
/// Returns the field number declared in the proto file. /// Returns the field number declared in the proto file.
@ -284,7 +287,7 @@ namespace Google.Protobuf.Reflection
{ {
get get
{ {
if (fieldType != FieldType.Enum) if (FieldType != FieldType.Enum)
{ {
throw new InvalidOperationException("EnumType is only valid for enum fields."); throw new InvalidOperationException("EnumType is only valid for enum fields.");
} }
@ -299,7 +302,7 @@ namespace Google.Protobuf.Reflection
{ {
get 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."); 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. // Choose field type based on symbol.
if (typeDescriptor is MessageDescriptor) if (typeDescriptor is MessageDescriptor)
{ {
fieldType = FieldType.Message; FieldType =
Features.MessageEncoding == FeatureSet.Types.MessageEncoding.Delimited
? FieldType.Group
: FieldType.Message;
} }
else if (typeDescriptor is EnumDescriptor) else if (typeDescriptor is EnumDescriptor)
{ {
fieldType = FieldType.Enum; FieldType = FieldType.Enum;
} }
else 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) if (typeDescriptor is not MessageDescriptor m)
{ {
@ -407,7 +413,7 @@ namespace Google.Protobuf.Reflection
throw new DescriptorValidationException(this, "Messages can't have default values."); 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) if (typeDescriptor is not EnumDescriptor e)
{ {
@ -422,7 +428,7 @@ namespace Google.Protobuf.Reflection
} }
else 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."); throw new DescriptorValidationException(this, "Field with message or enum type missing type_name.");
} }

Loading…
Cancel
Save