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() {
}
void EnumFieldGenerator::GenerateMembers(Writer* writer) {
writer->WriteLine("private bool has$0$;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("private bool has$0$;", property_name());
}
writer->WriteLine("private $0$ $1$_ = $2$;", type_name(), name(),
default_value());
AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return has$0$; }", property_name());
writer->WriteLine("}");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return has$0$; }", property_name());
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$_; }", name());
......@@ -71,9 +75,11 @@ void EnumFieldGenerator::GenerateMembers(Writer* writer) {
void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) {
AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.has$0$; }", property_name());
writer->WriteLine("}");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.has$0$; }", property_name());
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$; }", property_name());
......@@ -83,21 +89,29 @@ void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) {
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(),
type_name());
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.has$0$ = true;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name());
}
writer->WriteLine(" result.$0$_ = value;", name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.has$0$ = false;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = false;", property_name());
}
writer->WriteLine(" result.$0$_ = $1$;", name(), default_value());
writer->WriteLine(" return this;");
writer->WriteLine("}");
}
void EnumFieldGenerator::GenerateMergingCode(Writer* writer) {
writer->WriteLine("if (other.Has$0$) {", property_name());
if (SupportFieldPresence(descriptor_->file())) {
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("}");
}
......@@ -110,7 +124,9 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
writer->WriteLine("object unknown;");
writer->WriteLine("if(input.ReadEnum(ref result.$0$_, out unknown)) {",
name());
writer->WriteLine(" result.has$0$ = true;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name());
}
writer->WriteLine("} else if(unknown is int) {");
if (!use_lite_runtime()) {
writer->WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now
......@@ -125,7 +141,11 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
}
void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
writer->WriteLine("if (has$0$) {", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(
" output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);", number(),
property_name(), field_ordinal());
......@@ -133,7 +153,11 @@ void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
}
void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
writer->WriteLine("if (has$0$) {", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("if (has$0$) {", property_name());
} else {
writer->WriteLine("if ($0$ != $1$) {", property_name(), default_value());
}
writer->WriteLine(
" size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);",
number(), property_name());
......@@ -141,17 +165,32 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
}
void EnumFieldGenerator::WriteHash(Writer* writer) {
writer->WriteLine("if (has$0$) hash ^= $1$_.GetHashCode();", property_name(),
name());
if (SupportFieldPresence(descriptor_->file())) {
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) {
writer->WriteLine(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;",
property_name(), name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;",
property_name(), name());
} else {
writer->WriteLine(
"if (!$0$_.Equals(other.$0$_)) return false;", name());
}
}
void EnumFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);",
descriptor_->name(), property_name(), name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);",
descriptor_->name(), property_name(), name());
} else {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);",
descriptor_->name(), name());
}
}
} // namespace csharp
......
......@@ -101,6 +101,10 @@ FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int
bool HasRequiredFields(const Descriptor* descriptor);
inline bool SupportFieldPresence(const FileDescriptor* file) {
return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
}
} // namespace csharp
} // namespace compiler
} // namespace protobuf
......
......@@ -279,7 +279,9 @@ void MessageGenerator::Generate(Writer* writer) {
}
if (optimize_speed()) {
GenerateIsInitialized(writer);
if (SupportFieldPresence(descriptor_->file())) {
GenerateIsInitialized(writer);
}
GenerateMessageSerializationMethods(writer);
}
if (use_lite_runtime()) {
......
......@@ -57,13 +57,17 @@ PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
}
void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) {
writer->WriteLine("private bool has$0$;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("private bool has$0$;", property_name());
}
writer->WriteLine("private $0$ $1$_$2$;", type_name(), name(),
has_default_value() ? " = " + default_value() : "");
AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return has$0$; }", property_name());
writer->WriteLine("}");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return has$0$; }", property_name());
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$_; }", name());
......@@ -72,9 +76,11 @@ void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) {
void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) {
AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.has$0$; }", property_name());
writer->WriteLine("}");
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.has$0$; }", property_name());
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$; }", property_name());
......@@ -85,21 +91,29 @@ void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) {
type_name());
AddNullCheck(writer);
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.has$0$ = true;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name());
}
writer->WriteLine(" result.$0$_ = value;", name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.has$0$ = false;", property_name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = false;", property_name());
}
writer->WriteLine(" result.$0$_ = $1$;", name(), default_value());
writer->WriteLine(" return this;");
writer->WriteLine("}");
}
void PrimitiveFieldGenerator::GenerateMergingCode(Writer* writer) {
writer->WriteLine("if (other.Has$0$) {", property_name());
if (SupportFieldPresence(descriptor_->file())) {
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("}");
}
......@@ -109,12 +123,21 @@ void PrimitiveFieldGenerator::GenerateBuildingCode(Writer* writer) {
}
void PrimitiveFieldGenerator::GenerateParsingCode(Writer* writer) {
writer->WriteLine("result.has$0$ = input.Read$1$(ref result.$2$_);",
property_name(), capitalized_type_name(), name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("result.has$0$ = input.Read$1$(ref result.$2$_);",
property_name(), capitalized_type_name(), name());
} else {
writer->WriteLine("input.Read$0$(ref result.$1$_);",
capitalized_type_name(), name());
}
}
void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
writer->WriteLine("if (has$0$) {", property_name());
if (SupportFieldPresence(descriptor_->file())) {
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$);",
capitalized_type_name(), number(), property_name(),
field_ordinal());
......@@ -122,24 +145,43 @@ void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
}
void PrimitiveFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
writer->WriteLine("if (has$0$) {", property_name());
if (SupportFieldPresence(descriptor_->file())) {
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$);",
capitalized_type_name(), number(), property_name());
writer->WriteLine("}");
}
void PrimitiveFieldGenerator::WriteHash(Writer* writer) {
writer->WriteLine("if (has$0$) hash ^= $1$_.GetHashCode();", property_name(),
name());
if (SupportFieldPresence(descriptor_->file())) {
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) {
writer->WriteLine(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;",
property_name(), name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;",
property_name(), name());
} else {
writer->WriteLine(
"if (!$0$_.Equals(other.$0$_)) return false;", name());
}
}
void PrimitiveFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);",
descriptor_->name(), property_name(), name());
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);",
descriptor_->name(), property_name(), name());
} else {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);",
descriptor_->name(), name());
}
}
} // 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