aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs')
-rw-r--r--csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs33
1 files changed, 22 insertions, 11 deletions
diff --git a/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs
index 2f2c5806..7af69bbb 100644
--- a/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs
+++ b/csharp/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs
@@ -38,21 +38,20 @@ namespace Google.Protobuf.Descriptors
/// <summary>
/// Descriptor for a field or extension within a message in a .proto file.
/// </summary>
- public sealed class FieldDescriptor : IndexedDescriptorBase<FieldDescriptorProto, FieldOptions>,
- IComparable<FieldDescriptor>
+ public sealed class FieldDescriptor : DescriptorBase, IComparable<FieldDescriptor>
{
+ private readonly FieldDescriptorProto proto;
private EnumDescriptor enumType;
private MessageDescriptor messageType;
- private MessageDescriptor containingType;
- private OneofDescriptor containingOneof;
+ private readonly MessageDescriptor containingType;
+ private readonly OneofDescriptor containingOneof;
private FieldType fieldType;
- private readonly object optionsLock = new object();
-
internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
MessageDescriptor parent, int index)
- : base(proto, file, ComputeFullName(file, parent, proto.Name), index)
+ : base(file, file.ComputeFullName(parent, proto.Name), index)
{
+ this.proto = proto;
if (proto.Type != 0)
{
fieldType = GetFieldTypeFromProtoType(proto.Type);
@@ -64,7 +63,8 @@ namespace Google.Protobuf.Descriptors
"Field numbers must be positive integers.");
}
containingType = parent;
- if (proto.OneofIndex != 0)
+ // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction.
+ if (proto.OneofIndex != -1)
{
if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count)
{
@@ -72,13 +72,19 @@ namespace Google.Protobuf.Descriptors
"FieldDescriptorProto.oneof_index is out of range for type " + parent.Name);
}
containingOneof = parent.Oneofs[proto.OneofIndex];
- containingOneof.fieldCount ++;
}
file.DescriptorPool.AddSymbol(this);
}
/// <summary>
+ /// The brief name of the descriptor's target.
+ /// </summary>
+ public override string Name { get { return proto.Name; } }
+
+ internal FieldDescriptorProto Proto { get { return proto; } }
+
+ /// <summary>
/// Maps a field type as included in the .proto file to a FieldType.
/// </summary>
private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Type type)
@@ -131,9 +137,14 @@ namespace Google.Protobuf.Descriptors
get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REPEATED; }
}
+ public bool IsMap
+ {
+ get { return fieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry; }
+ }
+
public bool IsPacked
{
- get { return Proto.Options.Packed; }
+ get { return Proto.Options != null && Proto.Options.Packed; }
}
/// <summary>
@@ -273,7 +284,7 @@ namespace Google.Protobuf.Descriptors
File.DescriptorPool.AddFieldByNumber(this);
- if (containingType != null && containingType.Options != null && containingType.Options.MessageSetWireFormat)
+ if (containingType != null && containingType.Proto.Options != null && containingType.Proto.Options.MessageSetWireFormat)
{
throw new DescriptorValidationException(this, "MessageSet format is not supported.");
}