Commit 447de3ad authored by Jie Luo's avatar Jie Luo

Merge pull request #321 from anandolee/csharp

Field Presence for Protocol Buffer C# Proto3
parents 0884b779 ea48104c
syntax = "proto3";
package Google.ProtocolBuffers.TestProtos.FieldPresence;
// TODO(jieluo): Add repeated fields, oneof, maps to TestAllTypes
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;
}
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2015 Google Inc. All rights reserved.
// Author: jieluo@google.com (Jie Luo)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos.FieldPresence;
using Xunit;
namespace Google.ProtocolBuffers
{
public class FieldPresenceTest
{
private void CheckHasMethodRemoved(Type proto2Type, Type proto3Type, string name)
{
Assert.NotNull(proto2Type.GetProperty(name));
Assert.NotNull(proto2Type.GetProperty("Has" + name));
Assert.NotNull(proto3Type.GetProperty(name));
Assert.Null(proto3Type.GetProperty("Has" + name));
}
[Fact]
public void TestHasMethod()
{
// Optional non-message fields don't have HasFoo method generated
Type proto2Type = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes);
Type proto3Type = typeof(TestAllTypes);
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
proto2Type = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes.Builder);
proto3Type = typeof(TestAllTypes.Builder);
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
// message fields still have the HasFoo method generated
Assert.False(TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage);
Assert.False(TestAllTypes.CreateBuilder().HasOptionalNestedMessage);
}
[Fact]
public void TestFieldPresence()
{
// Optional non-message fields set to their default value are treated the same
// way as not set.
// Serialization will ignore such fields.
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
builder.SetOptionalInt32(0);
builder.SetOptionalString("");
builder.SetOptionalBytes(ByteString.Empty);
builder.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO);
TestAllTypes message = builder.Build();
Assert.Equal(0, message.SerializedSize);
// Test merge
TestAllTypes.Builder a = TestAllTypes.CreateBuilder();
a.SetOptionalInt32(1);
a.SetOptionalString("x");
a.SetOptionalBytes(ByteString.CopyFromUtf8("y"));
a.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAR);
a.MergeFrom(message);
TestAllTypes messageA = a.Build();
Assert.Equal(1, messageA.OptionalInt32);
Assert.Equal("x", messageA.OptionalString);
Assert.Equal(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes);
Assert.Equal(TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum);
// equals/hashCode should produce the same results
TestAllTypes empty = TestAllTypes.CreateBuilder().Build();
Assert.True(empty.Equals(message));
Assert.True(message.Equals(empty));
Assert.Equal(empty.GetHashCode(), message.GetHashCode());
}
[Fact]
public void TestFieldPresenceReflection()
{
MessageDescriptor descriptor = TestAllTypes.Descriptor;
FieldDescriptor optionalInt32Field = descriptor.FindFieldByName("optional_int32");
FieldDescriptor optionalStringField = descriptor.FindFieldByName("optional_string");
FieldDescriptor optionalBytesField = descriptor.FindFieldByName("optional_bytes");
FieldDescriptor optionalNestedEnumField = descriptor.FindFieldByName("optional_nested_enum");
TestAllTypes message = TestAllTypes.CreateBuilder().Build();
Assert.False(message.HasField(optionalInt32Field));
Assert.False(message.HasField(optionalStringField));
Assert.False(message.HasField(optionalBytesField));
Assert.False(message.HasField(optionalNestedEnumField));
// Set to default value is seen as not present
message = TestAllTypes.CreateBuilder()
.SetOptionalInt32(0)
.SetOptionalString("")
.SetOptionalBytes(ByteString.Empty)
.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO)
.Build();
Assert.False(message.HasField(optionalInt32Field));
Assert.False(message.HasField(optionalStringField));
Assert.False(message.HasField(optionalBytesField));
Assert.False(message.HasField(optionalNestedEnumField));
Assert.Equal(0, message.AllFields.Count);
// Set t0 non-defalut value is seen as present
message = TestAllTypes.CreateBuilder()
.SetOptionalInt32(1)
.SetOptionalString("x")
.SetOptionalBytes(ByteString.CopyFromUtf8("y"))
.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAR)
.Build();
Assert.True(message.HasField(optionalInt32Field));
Assert.True(message.HasField(optionalStringField));
Assert.True(message.HasField(optionalBytesField));
Assert.True(message.HasField(optionalNestedEnumField));
Assert.Equal(4, message.AllFields.Count);
}
[Fact]
public void TestMessageField()
{
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
Assert.False(builder.HasOptionalNestedMessage);
Assert.False(builder.Build().HasOptionalNestedMessage);
// Unlike non-message fields, if we set default value to message field, the field
// shoule be seem as present.
builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance);
Assert.True(builder.HasOptionalNestedMessage);
Assert.True(builder.Build().HasOptionalNestedMessage);
}
[Fact]
public void TestSerializeAndParse()
{
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
builder.SetOptionalInt32(1234);
builder.SetOptionalString("hello");
builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance);
ByteString data = builder.Build().ToByteString();
TestAllTypes message = TestAllTypes.ParseFrom(data);
Assert.Equal(1234, message.OptionalInt32);
Assert.Equal("hello", message.OptionalString);
Assert.Equal(ByteString.Empty, message.OptionalBytes);
Assert.Equal(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
Assert.True(message.HasOptionalNestedMessage);
Assert.Equal(0, message.OptionalNestedMessage.Value);
}
}
}
...@@ -65,6 +65,7 @@ ...@@ -65,6 +65,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AbstractMessageTest.cs" /> <Compile Include="AbstractMessageTest.cs" />
<Compile Include="ByteStringTest.cs" /> <Compile Include="ByteStringTest.cs" />
<Compile Include="FieldPresenceTest.cs" />
<Compile Include="CodedInputStreamTest.cs" /> <Compile Include="CodedInputStreamTest.cs" />
<Compile Include="CodedOutputStreamTest.cs" /> <Compile Include="CodedOutputStreamTest.cs" />
<Compile Include="Collections\PopsicleListTest.cs" /> <Compile Include="Collections\PopsicleListTest.cs" />
...@@ -75,6 +76,7 @@ ...@@ -75,6 +76,7 @@
<Compile Include="Compatibility\TestResources.cs" /> <Compile Include="Compatibility\TestResources.cs" />
<Compile Include="Compatibility\TextCompatibilityTests.cs" /> <Compile Include="Compatibility\TextCompatibilityTests.cs" />
<Compile Include="Compatibility\XmlCompatibilityTests.cs" /> <Compile Include="Compatibility\XmlCompatibilityTests.cs" />
<Compile Include="TestProtos\FieldPresence.cs" />
<Compile Include="TestProtos\GoogleSize.cs" /> <Compile Include="TestProtos\GoogleSize.cs" />
<Compile Include="TestProtos\GoogleSpeed.cs" /> <Compile Include="TestProtos\GoogleSpeed.cs" />
<Compile Include="TestProtos\Unittest.cs" /> <Compile Include="TestProtos\Unittest.cs" />
......
This diff is collapsed.
...@@ -54,6 +54,17 @@ namespace Google.ProtocolBuffers.Descriptors ...@@ -54,6 +54,17 @@ namespace Google.ProtocolBuffers.Descriptors
private readonly IList<FileDescriptor> publicDependencies; private readonly IList<FileDescriptor> publicDependencies;
private readonly DescriptorPool pool; private readonly DescriptorPool pool;
public enum ProtoSyntax
{
Proto2,
Proto3
}
public ProtoSyntax Syntax
{
get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; }
}
private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies) private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies)
{ {
this.pool = pool; this.pool = pool;
......
...@@ -68,16 +68,17 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -68,16 +68,17 @@ namespace Google.ProtocolBuffers.FieldAccess
{ {
this.descriptor = descriptor; this.descriptor = descriptor;
accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count]; accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
bool supportFieldPresence = descriptor.File.Syntax == FileDescriptor.ProtoSyntax.Proto2;
for (int i = 0; i < accessors.Length; i++) for (int i = 0; i < accessors.Length; i++)
{ {
accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i]); accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence);
} }
} }
/// <summary> /// <summary>
/// Creates an accessor for a single field /// Creates an accessor for a single field
/// </summary> /// </summary>
private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name) private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name, bool supportFieldPresence)
{ {
if (field.IsRepeated) if (field.IsRepeated)
{ {
...@@ -98,9 +99,9 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -98,9 +99,9 @@ namespace Google.ProtocolBuffers.FieldAccess
case MappedType.Message: case MappedType.Message:
return new SingleMessageAccessor<TMessage, TBuilder>(name); return new SingleMessageAccessor<TMessage, TBuilder>(name);
case MappedType.Enum: case MappedType.Enum:
return new SingleEnumAccessor<TMessage, TBuilder>(field, name); return new SingleEnumAccessor<TMessage, TBuilder>(field, name, supportFieldPresence);
default: default:
return new SinglePrimitiveAccessor<TMessage, TBuilder>(name); return new SinglePrimitiveAccessor<TMessage, TBuilder>(field, name, supportFieldPresence);
} }
} }
} }
......
...@@ -42,7 +42,7 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -42,7 +42,7 @@ namespace Google.ProtocolBuffers.FieldAccess
{ {
private readonly EnumDescriptor enumDescriptor; private readonly EnumDescriptor enumDescriptor;
internal SingleEnumAccessor(FieldDescriptor field, string name) : base(name) internal SingleEnumAccessor(FieldDescriptor field, string name, bool supportFieldPresence) : base(field, name, supportFieldPresence)
{ {
enumDescriptor = field.EnumType; enumDescriptor = field.EnumType;
} }
......
...@@ -48,7 +48,7 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -48,7 +48,7 @@ namespace Google.ProtocolBuffers.FieldAccess
/// </summary> /// </summary>
private readonly Func<IBuilder> createBuilderDelegate; private readonly Func<IBuilder> createBuilderDelegate;
internal SingleMessageAccessor(string name) : base(name) internal SingleMessageAccessor(string name) : base(null, name, true)
{ {
MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", ReflectionUtil.EmptyTypes); MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", ReflectionUtil.EmptyTypes);
if (createBuilderMethod == null) if (createBuilderMethod == null)
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System; using System;
using System.Reflection; using System.Reflection;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.FieldAccess namespace Google.ProtocolBuffers.FieldAccess
{ {
...@@ -56,18 +57,30 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -56,18 +57,30 @@ namespace Google.ProtocolBuffers.FieldAccess
get { return clrType; } get { return clrType; }
} }
internal SinglePrimitiveAccessor(string name) internal SinglePrimitiveAccessor(FieldDescriptor fieldDescriptor, string name, bool supportFieldPresence)
{ {
PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes); PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes);
PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes); PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes);
PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name);
MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name); MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null) if (messageProperty == null || builderProperty == null || clearMethod == null)
{ {
throw new ArgumentException("Not all required properties/methods available"); throw new ArgumentException("Not all required properties/methods available");
} }
if (supportFieldPresence)
{
PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name);
if (hasProperty == null)
{
throw new ArgumentException("Has properties not available");
}
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
} else
{
hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue);
}
clrType = messageProperty.PropertyType; clrType = messageProperty.PropertyType;
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod); clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod);
getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod()); setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
......
...@@ -56,13 +56,17 @@ EnumFieldGenerator::~EnumFieldGenerator() { ...@@ -56,13 +56,17 @@ EnumFieldGenerator::~EnumFieldGenerator() {
} }
void EnumFieldGenerator::GenerateMembers(Writer* writer) { 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(), writer->WriteLine("private $0$ $1$_ = $2$;", type_name(), name(),
default_value()); default_value());
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name()); if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" get { return has$0$; }", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine("}"); writer->WriteLine(" get { return has$0$; }", property_name());
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);
writer->WriteLine("public bool Has$0$ {", property_name()); if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" get { return result.has$0$; }", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine("}"); writer->WriteLine(" get { return result.has$0$; }", property_name());
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();");
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(" 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();");
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(" 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) {
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(" $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());
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) {"); 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) {
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( 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) {
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( 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 ^= $0$_.GetHashCode();", name());
writer->WriteLine("}");
} }
void EnumFieldGenerator::WriteEquals(Writer* writer) { void EnumFieldGenerator::WriteEquals(Writer* writer) {
writer->WriteLine( if (SupportFieldPresence(descriptor_->file())) {
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", writer->WriteLine(
property_name(), name()); "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) { void EnumFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);", if (SupportFieldPresence(descriptor_->file())) {
descriptor_->name(), property_name(), name()); 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 } // 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()) {
GenerateIsInitialized(writer); if (SupportFieldPresence(descriptor_->file())) {
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) {
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(), writer->WriteLine("private $0$ $1$_$2$;", type_name(), name(),
has_default_value() ? " = " + default_value() : ""); has_default_value() ? " = " + default_value() : "");
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name()); if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" get { return has$0$; }", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine("}"); writer->WriteLine(" get { return has$0$; }", property_name());
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);
writer->WriteLine("public bool Has$0$ {", property_name()); if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" get { return result.has$0$; }", property_name()); writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine("}"); writer->WriteLine(" get { return result.has$0$; }", property_name());
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();");
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(" 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();");
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(" 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) {
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(" $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) {
writer->WriteLine("result.has$0$ = input.Read$1$(ref result.$2$_);", if (SupportFieldPresence(descriptor_->file())) {
property_name(), capitalized_type_name(), name()); 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) { 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$);", 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) {
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$);", 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 ^= $0$_.GetHashCode();", name());
writer->WriteLine("}");
} }
void PrimitiveFieldGenerator::WriteEquals(Writer* writer) { void PrimitiveFieldGenerator::WriteEquals(Writer* writer) {
writer->WriteLine( if (SupportFieldPresence(descriptor_->file())) {
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", writer->WriteLine(
property_name(), name()); "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) { void PrimitiveFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);", if (SupportFieldPresence(descriptor_->file())) {
descriptor_->name(), property_name(), name()); 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 } // 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