diff options
author | Sydney Acksman <ObsidianMinor@users.noreply.github.com> | 2018-09-24 15:42:24 -0500 |
---|---|---|
committer | Jie Luo <anandolee@gmail.com> | 2018-09-24 13:42:24 -0700 |
commit | 54176b26a9be6c9903b375596b778f51f5947921 (patch) | |
tree | a441d2831ecdb3db5e1f867b8fabc94ed523de13 /csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs | |
parent | fb0a74b66076d6c55022a9bccabf6cdb08dbab83 (diff) | |
download | protobuf-54176b26a9be6c9903b375596b778f51f5947921.tar.gz protobuf-54176b26a9be6c9903b375596b778f51f5947921.tar.bz2 protobuf-54176b26a9be6c9903b375596b778f51f5947921.zip |
C# Proto2 feature : Field presence and default values (#4642)
* Compiler changes
* Generated code changes
* Library changes
* Compiler style changes
* Generated style changes
* Fix Windows build errors
* Implement changes from review
* Reintroduce proto2 check
* Compiler changes (required handling review)
* Generated code changes (required handling review)
* Library changes (required handling review
* Field presence rewrite (compiler changes)
* Field presence rewrite (generated code changes)
* Compiler comment
* IFieldAccessor.HasValue library implementation
* Remove Clear methods and default values from proto3 code (Compiler)
* Remove Clear methods and default values from proto3 code (Generated)
* Remove Clear methods and default values from proto3 code (Library)
* Fix distcheck error
* Rewrite default string values to use base64 and convert
* Library changes (IMessage2)
* Compiler changes (IMessage2)
* Generated changes (IMessage2)
* Rebased and regenerated
* Compiler changes (initialized extension)
* Generated changes (initialized extension)
* Library changes (initialized extension)
* Refactor MessageExtensions.IsRequired
* Move string default value creator and bytes default value creator back to seperate methods
* Dead code cleanup
* Fixed segmentation fault
Removed unused header method declarations
Diffstat (limited to 'csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs')
-rw-r--r-- | csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 152467d8..0a46f9eb 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -79,8 +79,7 @@ namespace Google.Protobuf.Reflection throw new DescriptorValidationException(this, "Field numbers must be positive integers."); } ContainingType = parent; - // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction. - if (proto.OneofIndex != -1) + if (proto.HasOneofIndex) { if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count) { @@ -184,6 +183,11 @@ namespace Google.Protobuf.Reflection /// </summary> public bool IsRepeated => Proto.Label == FieldDescriptorProto.Types.Label.Repeated; + /// <summary>
+ /// Returns <c>true</c> if this field is a required field; <c>false</c> otherwise.
+ /// </summary> + public bool IsRequired => Proto.Label == FieldDescriptorProto.Types.Label.Required; + /// <summary> /// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise. /// </summary> @@ -192,13 +196,8 @@ namespace Google.Protobuf.Reflection /// <summary> /// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise. /// </summary> - public bool IsPacked => - // Note the || rather than && here - we're effectively defaulting to packed, because that *is* - // the default in proto3, which is all we support. We may give the wrong result for the protos - // within descriptor.proto, but that's okay, as they're never exposed and we don't use IsPacked - // within the runtime. - Proto.Options == null || Proto.Options.Packed; - + public bool IsPacked => File.Proto.Syntax == "proto2" ? Proto.Options?.Packed ?? false : !Proto.Options.HasPacked || Proto.Options.Packed;
+
/// <summary> /// Returns the type of the field. /// </summary> @@ -247,9 +246,9 @@ namespace Google.Protobuf.Reflection { get { - if (fieldType != FieldType.Message) + if (fieldType != FieldType.Message && fieldType != FieldType.Group) { - throw new InvalidOperationException("MessageType is only valid for message fields."); + throw new InvalidOperationException("MessageType is only valid for message or group fields."); } return messageType; } @@ -265,12 +264,12 @@ namespace Google.Protobuf.Reflection /// </summary> internal void CrossLink() { - if (Proto.TypeName != "") + if (Proto.HasTypeName) { IDescriptor typeDescriptor = File.DescriptorPool.LookupSymbol(Proto.TypeName, this); - if (Proto.Type != 0) + if (Proto.HasType) { // Choose field type based on symbol. if (typeDescriptor is MessageDescriptor) @@ -287,7 +286,7 @@ namespace Google.Protobuf.Reflection } } - if (fieldType == FieldType.Message) + if (fieldType == FieldType.Message || fieldType == FieldType.Group) { if (!(typeDescriptor is MessageDescriptor)) { @@ -295,7 +294,7 @@ namespace Google.Protobuf.Reflection } messageType = (MessageDescriptor) typeDescriptor; - if (Proto.DefaultValue != "") + if (Proto.HasDefaultValue) { throw new DescriptorValidationException(this, "Messages can't have default values."); } @@ -325,7 +324,7 @@ namespace Google.Protobuf.Reflection File.DescriptorPool.AddFieldByNumber(this); - if (ContainingType != null && ContainingType.Proto.Options != null && ContainingType.Proto.Options.MessageSetWireFormat) + if (ContainingType != null && ContainingType.Proto.HasOptions && ContainingType.Proto.Options.MessageSetWireFormat) { throw new DescriptorValidationException(this, "MessageSet format is not supported."); } |