Commit 856ced5d authored by Jie Luo's avatar Jie Luo

Field Presence: remove has methods for optional non-message fields in proto3

parent 7646eec9
syntax = "proto3";
package field_presence_test;
option java_package = "com.google.protobuf";
option java_outer_classname = "FieldPresenceTestProto";
option java_generate_equals_and_hash = true;
message TestAllTypes {
enum NestedEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
}
message NestedMessage {
optional int32 value = 1;
}
optional int32 optional_int32 = 1;
optional string optional_string = 2;
optional bytes optional_bytes = 3;
optional NestedEnum optional_nested_enum = 4;
optional NestedMessage optional_nested_message = 5;
}
...@@ -56,13 +56,17 @@ EnumFieldGenerator::~EnumFieldGenerator() { ...@@ -56,13 +56,17 @@ EnumFieldGenerator::~EnumFieldGenerator() {
} }
void EnumFieldGenerator::GenerateMembers(Writer* writer) { void EnumFieldGenerator::GenerateMembers(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("private bool has$0$;", property_name()); writer->WriteLine("private bool has$0$;", property_name());
}
writer->WriteLine("private $0$ $1$_ = $2$;", type_name(), name(), writer->WriteLine("private $0$ $1$_ = $2$;", type_name(), name(),
default_value()); default_value());
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return has$0$; }", property_name()); writer->WriteLine(" get { return has$0$; }", property_name());
writer->WriteLine("}"); writer->WriteLine("}");
}
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$_; }", name()); writer->WriteLine(" get { return $0$_; }", name());
...@@ -71,9 +75,11 @@ void EnumFieldGenerator::GenerateMembers(Writer* writer) { ...@@ -71,9 +75,11 @@ void EnumFieldGenerator::GenerateMembers(Writer* writer) {
void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) { void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.has$0$; }", property_name()); writer->WriteLine(" get { return result.has$0$; }", property_name());
writer->WriteLine("}"); writer->WriteLine("}");
}
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$; }", property_name()); writer->WriteLine(" get { return result.$0$; }", property_name());
...@@ -83,21 +89,29 @@ void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) { ...@@ -83,21 +89,29 @@ void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) {
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(),
type_name()); type_name());
writer->WriteLine(" PrepareBuilder();"); writer->WriteLine(" PrepareBuilder();");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name()); writer->WriteLine(" result.has$0$ = true;", property_name());
}
writer->WriteLine(" result.$0$_ = value;", name()); writer->WriteLine(" result.$0$_ = value;", name());
writer->WriteLine(" return this;"); writer->WriteLine(" return this;");
writer->WriteLine("}"); writer->WriteLine("}");
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name()); writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" PrepareBuilder();"); writer->WriteLine(" PrepareBuilder();");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = false;", property_name()); writer->WriteLine(" result.has$0$ = false;", property_name());
}
writer->WriteLine(" result.$0$_ = $1$;", name(), default_value()); writer->WriteLine(" result.$0$_ = $1$;", name(), default_value());
writer->WriteLine(" return this;"); writer->WriteLine(" return this;");
writer->WriteLine("}"); writer->WriteLine("}");
} }
void EnumFieldGenerator::GenerateMergingCode(Writer* writer) { void EnumFieldGenerator::GenerateMergingCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (other.Has$0$) {", property_name()); writer->WriteLine("if (other.Has$0$) {", property_name());
} else {
writer->WriteLine("if (other.$0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(" $0$ = other.$0$;", property_name()); writer->WriteLine(" $0$ = other.$0$;", property_name());
writer->WriteLine("}"); writer->WriteLine("}");
} }
...@@ -110,7 +124,9 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) { ...@@ -110,7 +124,9 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
writer->WriteLine("object unknown;"); writer->WriteLine("object unknown;");
writer->WriteLine("if(input.ReadEnum(ref result.$0$_, out unknown)) {", writer->WriteLine("if(input.ReadEnum(ref result.$0$_, out unknown)) {",
name()); name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name()); writer->WriteLine(" result.has$0$ = true;", property_name());
}
writer->WriteLine("} else if(unknown is int) {"); writer->WriteLine("} else if(unknown is int) {");
if (!use_lite_runtime()) { if (!use_lite_runtime()) {
writer->WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now writer->WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now
...@@ -125,7 +141,11 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) { ...@@ -125,7 +141,11 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
} }
void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) { void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (has$0$) {", property_name()); writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine( writer->WriteLine(
" output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);", number(), " output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);", number(),
property_name(), field_ordinal()); property_name(), field_ordinal());
...@@ -133,7 +153,11 @@ void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) { ...@@ -133,7 +153,11 @@ void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
} }
void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (has$0$) {", property_name()); writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine( writer->WriteLine(
" size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);", " size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);",
number(), property_name()); number(), property_name());
...@@ -141,17 +165,32 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { ...@@ -141,17 +165,32 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
} }
void EnumFieldGenerator::WriteHash(Writer* writer) { void EnumFieldGenerator::WriteHash(Writer* writer) {
writer->WriteLine("if (has$0$) hash ^= $1$_.GetHashCode();", property_name(), if (SupportFieldPresence(descriptor_->file())) {
name()); writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(" hash ^= $1$_.GetHashCode();", name());
writer->WriteLine("}");
} }
void EnumFieldGenerator::WriteEquals(Writer* writer) { void EnumFieldGenerator::WriteEquals(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine( writer->WriteLine(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", "if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;",
property_name(), name()); property_name(), name());
} else {
writer->WriteLine(
"if (!$0$_.Equals(other.$0$_)) return false;", name());
}
} }
void EnumFieldGenerator::WriteToString(Writer* writer) { void EnumFieldGenerator::WriteToString(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);", writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);",
descriptor_->name(), property_name(), name()); descriptor_->name(), property_name(), name());
} else {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);",
descriptor_->name(), name());
}
} }
} // namespace csharp } // namespace csharp
......
...@@ -101,6 +101,10 @@ FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int ...@@ -101,6 +101,10 @@ FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int
bool HasRequiredFields(const Descriptor* descriptor); bool HasRequiredFields(const Descriptor* descriptor);
inline bool SupportFieldPresence(const FileDescriptor* file) {
return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
}
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf
......
...@@ -279,7 +279,9 @@ void MessageGenerator::Generate(Writer* writer) { ...@@ -279,7 +279,9 @@ void MessageGenerator::Generate(Writer* writer) {
} }
if (optimize_speed()) { if (optimize_speed()) {
if (SupportFieldPresence(descriptor_->file())) {
GenerateIsInitialized(writer); GenerateIsInitialized(writer);
}
GenerateMessageSerializationMethods(writer); GenerateMessageSerializationMethods(writer);
} }
if (use_lite_runtime()) { if (use_lite_runtime()) {
......
...@@ -57,13 +57,17 @@ PrimitiveFieldGenerator::~PrimitiveFieldGenerator() { ...@@ -57,13 +57,17 @@ PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
} }
void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) { void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("private bool has$0$;", property_name()); writer->WriteLine("private bool has$0$;", property_name());
}
writer->WriteLine("private $0$ $1$_$2$;", type_name(), name(), writer->WriteLine("private $0$ $1$_$2$;", type_name(), name(),
has_default_value() ? " = " + default_value() : ""); has_default_value() ? " = " + default_value() : "");
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return has$0$; }", property_name()); writer->WriteLine(" get { return has$0$; }", property_name());
writer->WriteLine("}"); writer->WriteLine("}");
}
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$_; }", name()); writer->WriteLine(" get { return $0$_; }", name());
...@@ -72,9 +76,11 @@ void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) { ...@@ -72,9 +76,11 @@ void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) {
void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) { void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.has$0$; }", property_name()); writer->WriteLine(" get { return result.has$0$; }", property_name());
writer->WriteLine("}"); writer->WriteLine("}");
}
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$; }", property_name()); writer->WriteLine(" get { return result.$0$; }", property_name());
...@@ -85,21 +91,29 @@ void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) { ...@@ -85,21 +91,29 @@ void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) {
type_name()); type_name());
AddNullCheck(writer); AddNullCheck(writer);
writer->WriteLine(" PrepareBuilder();"); writer->WriteLine(" PrepareBuilder();");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name()); writer->WriteLine(" result.has$0$ = true;", property_name());
}
writer->WriteLine(" result.$0$_ = value;", name()); writer->WriteLine(" result.$0$_ = value;", name());
writer->WriteLine(" return this;"); writer->WriteLine(" return this;");
writer->WriteLine("}"); writer->WriteLine("}");
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name()); writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" PrepareBuilder();"); writer->WriteLine(" PrepareBuilder();");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = false;", property_name()); writer->WriteLine(" result.has$0$ = false;", property_name());
}
writer->WriteLine(" result.$0$_ = $1$;", name(), default_value()); writer->WriteLine(" result.$0$_ = $1$;", name(), default_value());
writer->WriteLine(" return this;"); writer->WriteLine(" return this;");
writer->WriteLine("}"); writer->WriteLine("}");
} }
void PrimitiveFieldGenerator::GenerateMergingCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateMergingCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (other.Has$0$) {", property_name()); writer->WriteLine("if (other.Has$0$) {", property_name());
} else {
writer->WriteLine("if (other.$0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(" $0$ = other.$0$;", property_name()); writer->WriteLine(" $0$ = other.$0$;", property_name());
writer->WriteLine("}"); writer->WriteLine("}");
} }
...@@ -109,12 +123,21 @@ void PrimitiveFieldGenerator::GenerateBuildingCode(Writer* writer) { ...@@ -109,12 +123,21 @@ void PrimitiveFieldGenerator::GenerateBuildingCode(Writer* writer) {
} }
void PrimitiveFieldGenerator::GenerateParsingCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateParsingCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("result.has$0$ = input.Read$1$(ref result.$2$_);", writer->WriteLine("result.has$0$ = input.Read$1$(ref result.$2$_);",
property_name(), capitalized_type_name(), name()); property_name(), capitalized_type_name(), name());
} else {
writer->WriteLine("input.Read$0$(ref result.$1$_);",
capitalized_type_name(), name());
}
} }
void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (has$0$) {", property_name()); writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(" output.Write$0$($1$, field_names[$3$], $2$);", writer->WriteLine(" output.Write$0$($1$, field_names[$3$], $2$);",
capitalized_type_name(), number(), property_name(), capitalized_type_name(), number(), property_name(),
field_ordinal()); field_ordinal());
...@@ -122,24 +145,43 @@ void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) { ...@@ -122,24 +145,43 @@ void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
} }
void PrimitiveFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (has$0$) {", property_name()); writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);", writer->WriteLine(" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);",
capitalized_type_name(), number(), property_name()); capitalized_type_name(), number(), property_name());
writer->WriteLine("}"); writer->WriteLine("}");
} }
void PrimitiveFieldGenerator::WriteHash(Writer* writer) { void PrimitiveFieldGenerator::WriteHash(Writer* writer) {
writer->WriteLine("if (has$0$) hash ^= $1$_.GetHashCode();", property_name(), if (SupportFieldPresence(descriptor_->file())) {
name()); writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(" hash ^= $1$_.GetHashCode();", name());
writer->WriteLine("}");
} }
void PrimitiveFieldGenerator::WriteEquals(Writer* writer) { void PrimitiveFieldGenerator::WriteEquals(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine( writer->WriteLine(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", "if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;",
property_name(), name()); property_name(), name());
} else {
writer->WriteLine(
"if (!$0$_.Equals(other.$0$_)) return false;", name());
}
} }
void PrimitiveFieldGenerator::WriteToString(Writer* writer) { void PrimitiveFieldGenerator::WriteToString(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);", writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);",
descriptor_->name(), property_name(), name()); descriptor_->name(), property_name(), name());
} else {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);",
descriptor_->name(), name());
}
} }
} // namespace csharp } // namespace csharp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment