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 /src/google/protobuf/compiler/csharp/csharp_message_field.cc | |
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 'src/google/protobuf/compiler/csharp/csharp_message_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/csharp/csharp_message_field.cc | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_message_field.cc index cf1b4dbf..16714603 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message_field.cc @@ -49,11 +49,13 @@ namespace compiler { namespace csharp { MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options) - : FieldGeneratorBase(descriptor, fieldOrdinal, options) { - variables_["has_property_check"] = name() + "_ != null"; - variables_["has_not_property_check"] = name() + "_ == null"; + : FieldGeneratorBase(descriptor, presenceIndex, options) { + if (!IsProto2(descriptor_->file())) { + variables_["has_property_check"] = name() + "_ != null"; + variables_["has_not_property_check"] = name() + "_ == null"; + } } MessageFieldGenerator::~MessageFieldGenerator() { @@ -74,6 +76,26 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer) { " $name$_ = value;\n" " }\n" "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// <summary>Gets whether the $descriptor_name$ field is set</summary>\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $name$_ != null; }\n" + "}\n"); + printer->Print( + variables_, + "/// <summary>Clears the value of the $descriptor_name$ field</summary>\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " $name$_ = null;\n" + "}\n"); + } } void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) { @@ -81,7 +103,7 @@ void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) { variables_, "if (other.$has_property_check$) {\n" " if ($has_not_property_check$) {\n" - " $name$_ = new $type_name$();\n" + " $property_name$ = new $type_name$();\n" " }\n" " $property_name$.MergeFrom(other.$property_name$);\n" "}\n"); @@ -91,10 +113,9 @@ void MessageFieldGenerator::GenerateParsingCode(io::Printer* printer) { printer->Print( variables_, "if ($has_not_property_check$) {\n" - " $name$_ = new $type_name$();\n" + " $property_name$ = new $type_name$();\n" "}\n" - // TODO(jonskeet): Do we really need merging behaviour like this? - "input.ReadMessage($name$_);\n"); // No need to support TYPE_GROUP... + "input.ReadMessage($property_name$);\n"); } void MessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) { @@ -130,7 +151,6 @@ void MessageFieldGenerator::WriteToString(io::Printer* printer) { variables_, "PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n"); } - void MessageFieldGenerator::GenerateCloningCode(io::Printer* printer) { printer->Print(variables_, "$name$_ = other.$has_property_check$ ? other.$name$_.Clone() : null;\n"); @@ -147,9 +167,9 @@ void MessageFieldGenerator::GenerateCodecCode(io::Printer* printer) { MessageOneofFieldGenerator::MessageOneofFieldGenerator( const FieldDescriptor* descriptor, - int fieldOrdinal, + int presenceIndex, const Options *options) - : MessageFieldGenerator(descriptor, fieldOrdinal, options) { + : MessageFieldGenerator(descriptor, presenceIndex, options) { SetCommonOneofFieldVariables(&variables_); } @@ -169,6 +189,28 @@ void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) { " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None : $oneof_property_name$OneofCase.$property_name$;\n" " }\n" "}\n"); + if (IsProto2(descriptor_->file())) { + printer->Print( + variables_, + "/// <summary>Gets whether the \"$descriptor_name$\" field is set</summary>\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ bool Has$property_name$ {\n" + " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n" + "}\n"); + printer->Print( + variables_, + "/// <summary> Clears the value of the oneof if it's currently set to \"$descriptor_name$\" </summary>\n"); + AddPublicMemberAttributes(printer); + printer->Print( + variables_, + "$access_level$ void Clear$property_name$() {\n" + " if ($has_property_check$) {\n" + " Clear$oneof_property_name$();\n" + " }\n" + "}\n"); + } } void MessageOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) { @@ -187,7 +229,7 @@ void MessageOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) { "if ($has_property_check$) {\n" " subBuilder.MergeFrom($property_name$);\n" "}\n" - "input.ReadMessage(subBuilder);\n" // No support of TYPE_GROUP + "input.ReadMessage(subBuilder);\n" "$property_name$ = subBuilder;\n"); } |