Add oneof support for C#

This commit is contained in:
Jie Luo 2015-05-29 12:48:25 -07:00
parent 55df12194f
commit f7b417ddfe
39 changed files with 4724 additions and 2167 deletions

View File

@ -374,6 +374,16 @@ namespace Google.ProtocolBuffers
return wrappedMessage.HasField(field); return wrappedMessage.HasField(field);
} }
public override bool HasOneof(OneofDescriptor oneof)
{
return wrappedMessage.HasOneof(oneof);
}
public override FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof)
{
return wrappedMessage.OneofFieldDescriptor(oneof);
}
public override object this[FieldDescriptor field] public override object this[FieldDescriptor field]
{ {
get { return wrappedMessage[field]; } get { return wrappedMessage[field]; }
@ -461,6 +471,16 @@ namespace Google.ProtocolBuffers
return wrappedBuilder.HasField(field); return wrappedBuilder.HasField(field);
} }
public override bool HasOneof(OneofDescriptor oneof)
{
return wrappedBuilder.HasOneof(oneof);
}
public override FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof)
{
return wrappedBuilder.OneofFieldDescriptor(oneof);
}
public override UnknownFieldSet UnknownFields public override UnknownFieldSet UnknownFields
{ {
get { return wrappedBuilder.UnknownFields; } get { return wrappedBuilder.UnknownFields; }
@ -493,6 +513,12 @@ namespace Google.ProtocolBuffers
return this; return this;
} }
public override Builder ClearOneof(OneofDescriptor oneof)
{
wrappedBuilder.WeakClearOneof(oneof);
return this;
}
public override Builder AddRepeatedField(FieldDescriptor field, object value) public override Builder AddRepeatedField(FieldDescriptor field, object value)
{ {
wrappedBuilder.WeakAddRepeatedField(field, value); wrappedBuilder.WeakAddRepeatedField(field, value);

View File

@ -36,6 +36,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos; using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework; using NUnit.Framework;
@ -200,6 +201,21 @@ namespace Google.ProtocolBuffers
DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build(); DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
reflectionTester.AssertAllFieldsSetViaReflection(copy); reflectionTester.AssertAllFieldsSetViaReflection(copy);
// Oneof
FieldDescriptor bytesField =
TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
FieldDescriptor uint32Field =
TestAllTypes.Descriptor.FindFieldByName("oneof_uint32");
Assert.True(copy.HasField(bytesField));
Assert.False(copy.HasField(uint32Field));
DynamicMessage.Builder dynamicBuilder = DynamicMessage.CreateBuilder(message);
dynamicBuilder[uint32Field] = 123U;
DynamicMessage copy2 = dynamicBuilder.Build();
Assert.IsFalse(copy2.HasField(bytesField));
Assert.IsTrue(copy2.HasField(uint32Field));
Assert.AreEqual(123U, copy2[uint32Field]);
} }
[Test] [Test]
@ -223,5 +239,38 @@ namespace Google.ProtocolBuffers
Assert.AreEqual(1, values.Count); Assert.AreEqual(1, values.Count);
Assert.AreEqual(unknownFieldVal, values[0]); Assert.AreEqual(unknownFieldVal, values[0]);
} }
[Test]
public void DynamicOneofMessage()
{
DynamicMessage.Builder builder =
DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];
Assert.False(builder.HasOneof(oneof));
Assert.AreSame(null, builder.OneofFieldDescriptor(oneof));
reflectionTester.SetAllFieldsViaReflection(builder);
Assert.True(builder.HasOneof(oneof));
FieldDescriptor field = oneof.Field(3);
Assert.AreSame(field, builder.OneofFieldDescriptor(oneof));
Assert.AreEqual(TestUtil.ToBytes("604"), builder[field]);
DynamicMessage message = builder.BuildPartial();
Assert.IsTrue(message.HasOneof(oneof));
DynamicMessage.Builder mergedBuilder =
DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
FieldDescriptor mergedField = oneof.Field(0);
mergedBuilder[mergedField] = 123U;
Assert.IsTrue(mergedBuilder.HasField(mergedField));
mergedBuilder.MergeFrom(message);
Assert.IsTrue(mergedBuilder.HasField(field));
Assert.IsFalse(mergedBuilder.HasField(mergedField));
mergedBuilder.ClearOneof(oneof);
Assert.AreSame(null, mergedBuilder.OneofFieldDescriptor(oneof));
message = mergedBuilder.Build();
Assert.AreSame(null, message.OneofFieldDescriptor(oneof));
}
} }
} }

View File

@ -59,16 +59,25 @@ namespace Google.ProtocolBuffers
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum"); CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
proto2Type = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes.Builder); Type proto2BuilderType = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes.Builder);
proto3Type = typeof(TestAllTypes.Builder); Type proto3BuilderType = typeof(TestAllTypes.Builder);
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32"); CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalInt32");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString"); CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalString");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes"); CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalBytes");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum"); CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalNestedEnum");
// message fields still have the HasFoo method generated // message fields still have the HasFoo method generated
Assert.IsFalse(TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage); Assert.IsFalse(TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage);
Assert.IsFalse(TestAllTypes.CreateBuilder().HasOptionalNestedMessage); Assert.IsFalse(TestAllTypes.CreateBuilder().HasOptionalNestedMessage);
// oneof fields don't have the HasFoo method (even for message types)
CheckHasMethodRemoved(proto2Type, proto3Type, "OneofUint32");
CheckHasMethodRemoved(proto2Type, proto3Type, "OneofString");
CheckHasMethodRemoved(proto2Type, proto3Type, "OneofNestedMessage");
CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OneofUint32");
CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OneofString");
CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OneofNestedMessage");
} }
[Test] [Test]
@ -114,6 +123,7 @@ namespace Google.ProtocolBuffers
FieldDescriptor optionalStringField = descriptor.FindFieldByName("optional_string"); FieldDescriptor optionalStringField = descriptor.FindFieldByName("optional_string");
FieldDescriptor optionalBytesField = descriptor.FindFieldByName("optional_bytes"); FieldDescriptor optionalBytesField = descriptor.FindFieldByName("optional_bytes");
FieldDescriptor optionalNestedEnumField = descriptor.FindFieldByName("optional_nested_enum"); FieldDescriptor optionalNestedEnumField = descriptor.FindFieldByName("optional_nested_enum");
FieldDescriptor oneofUint32Field = descriptor.FindFieldByName("oneof_uint32");
TestAllTypes message = TestAllTypes.CreateBuilder().Build(); TestAllTypes message = TestAllTypes.CreateBuilder().Build();
Assert.IsFalse(message.HasField(optionalInt32Field)); Assert.IsFalse(message.HasField(optionalInt32Field));
@ -121,20 +131,23 @@ namespace Google.ProtocolBuffers
Assert.IsFalse(message.HasField(optionalBytesField)); Assert.IsFalse(message.HasField(optionalBytesField));
Assert.IsFalse(message.HasField(optionalNestedEnumField)); Assert.IsFalse(message.HasField(optionalNestedEnumField));
// Set to default value is seen as not present // Set to default value is seen as not present for optional fields.
// Set to default value is seen as present for oneof fields.
message = TestAllTypes.CreateBuilder() message = TestAllTypes.CreateBuilder()
.SetOptionalInt32(0) .SetOptionalInt32(0)
.SetOptionalString("") .SetOptionalString("")
.SetOptionalBytes(ByteString.Empty) .SetOptionalBytes(ByteString.Empty)
.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO) .SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO)
.SetOneofUint32(0U)
.Build(); .Build();
Assert.IsFalse(message.HasField(optionalInt32Field)); Assert.IsFalse(message.HasField(optionalInt32Field));
Assert.IsFalse(message.HasField(optionalStringField)); Assert.IsFalse(message.HasField(optionalStringField));
Assert.IsFalse(message.HasField(optionalBytesField)); Assert.IsFalse(message.HasField(optionalBytesField));
Assert.IsFalse(message.HasField(optionalNestedEnumField)); Assert.IsFalse(message.HasField(optionalNestedEnumField));
Assert.AreEqual(0, message.AllFields.Count); Assert.IsTrue(message.HasField(oneofUint32Field));
Assert.AreEqual(1, message.AllFields.Count);
// Set t0 non-defalut value is seen as present // Set to non-defalut value is seen as present
message = TestAllTypes.CreateBuilder() message = TestAllTypes.CreateBuilder()
.SetOptionalInt32(1) .SetOptionalInt32(1)
.SetOptionalString("x") .SetOptionalString("x")
@ -169,6 +182,7 @@ namespace Google.ProtocolBuffers
builder.SetOptionalInt32(1234); builder.SetOptionalInt32(1234);
builder.SetOptionalString("hello"); builder.SetOptionalString("hello");
builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance); builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance);
builder.SetOneofUint32(0U);
ByteString data = builder.Build().ToByteString(); ByteString data = builder.Build().ToByteString();
TestAllTypes message = TestAllTypes.ParseFrom(data); TestAllTypes message = TestAllTypes.ParseFrom(data);
@ -178,6 +192,7 @@ namespace Google.ProtocolBuffers
Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum); Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
Assert.IsTrue(message.HasOptionalNestedMessage); Assert.IsTrue(message.HasOptionalNestedMessage);
Assert.AreEqual(0, message.OptionalNestedMessage.Bb); Assert.AreEqual(0, message.OptionalNestedMessage.Bb);
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message.OneofFieldCase);
} }
} }
} }

View File

@ -36,6 +36,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.Collections; using Google.ProtocolBuffers.Collections;
using Google.ProtocolBuffers.TestProtos; using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework; using NUnit.Framework;
@ -292,6 +293,31 @@ namespace Google.ProtocolBuffers
}); });
} }
[Test]
public void ReflectionGetOneof()
{
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
reflectionTester.SetAllFieldsViaReflection(builder);
Descriptors.OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];
Descriptors.FieldDescriptor field = TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
Assert.AreSame(field, builder.OneofFieldDescriptor(oneof));
}
[Test]
public void ReflectionClearOneof()
{
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
reflectionTester.SetAllFieldsViaReflection(builder);
OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];
FieldDescriptor field = TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
Assert.IsTrue(builder.HasOneof(oneof));
Assert.IsTrue(builder.HasField(field));
builder.ClearOneof(oneof);
Assert.IsFalse(builder.HasOneof(oneof));
Assert.IsFalse(builder.HasField(field));
}
// ================================================================= // =================================================================
// Extensions. // Extensions.
@ -510,5 +536,241 @@ namespace Google.ProtocolBuffers
TestPackedTypes empty = new TestPackedTypes.Builder().Build(); TestPackedTypes empty = new TestPackedTypes.Builder().Build();
Assert.AreEqual(0, empty.SerializedSize); Assert.AreEqual(0, empty.SerializedSize);
} }
// oneof tests
[Test]
public void TestOneofEnumCase()
{
TestOneof2 message = TestOneof2.CreateBuilder()
.SetFooInt(123).SetFooString("foo").SetFooCord("bar").Build();
TestUtil.AssertAtMostOneFieldSetOneof(message);
}
[Test]
public void TestClearOneof()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder().SetFooInt(123);
Assert.AreEqual(TestOneof2.FooOneofCase.FooInt, builder.FooCase);
builder.ClearFoo();
Assert.AreEqual(TestOneof2.FooOneofCase.None, builder.FooCase);
}
[Test]
public void TestSetOneofClearsOthers()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message =
builder.SetFooInt(123).SetFooString("foo").Build();
Assert.IsTrue(message.HasFooString);
TestUtil.AssertAtMostOneFieldSetOneof(message);
message = builder.SetFooCord("bar").Build();
Assert.IsTrue(message.HasFooCord);
TestUtil.AssertAtMostOneFieldSetOneof(message);
message = builder.SetFooStringPiece("baz").Build();
Assert.IsTrue(message.HasFooStringPiece);
TestUtil.AssertAtMostOneFieldSetOneof(message);
message = builder.SetFooBytes(TestUtil.ToBytes("qux")).Build();
Assert.IsTrue(message.HasFooBytes);
TestUtil.AssertAtMostOneFieldSetOneof(message);
message = builder.SetFooEnum(TestOneof2.Types.NestedEnum.FOO).Build();
Assert.IsTrue(message.HasFooEnum);
TestUtil.AssertAtMostOneFieldSetOneof(message);
message = builder.SetFooMessage(
TestOneof2.Types.NestedMessage.CreateBuilder().SetQuxInt(234).Build()).Build();
Assert.IsTrue(message.HasFooMessage);
TestUtil.AssertAtMostOneFieldSetOneof(message);
message = builder.SetFooInt(123).Build();
Assert.IsTrue(message.HasFooInt);
TestUtil.AssertAtMostOneFieldSetOneof(message);
}
[Test]
public void TestOneofTypes_Primitive()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
Assert.AreEqual(builder.FooInt, 0);
Assert.IsFalse(builder.HasFooInt);
Assert.IsTrue(builder.SetFooInt(123).HasFooInt);
Assert.AreEqual(builder.FooInt, 123);
TestOneof2 message = builder.BuildPartial();
Assert.IsTrue(message.HasFooInt);
Assert.AreEqual(message.FooInt, 123);
Assert.IsFalse(builder.ClearFooInt().HasFooInt);
TestOneof2 message2 = builder.Build();
Assert.IsFalse(message2.HasFooInt);
Assert.AreEqual(message2.FooInt, 0);
}
[Test]
public void TestOneofTypes_Enum()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
Assert.AreEqual(builder.FooEnum, TestOneof2.Types.NestedEnum.FOO);
Assert.IsTrue(builder.SetFooEnum(TestOneof2.Types.NestedEnum.BAR).HasFooEnum);
Assert.AreEqual(builder.FooEnum, TestOneof2.Types.NestedEnum.BAR);
TestOneof2 message = builder.BuildPartial();
Assert.IsTrue(message.HasFooEnum);
Assert.AreEqual(message.FooEnum, TestOneof2.Types.NestedEnum.BAR);
Assert.IsFalse(builder.ClearFooEnum().HasFooEnum);
TestOneof2 message2 = builder.Build();
Assert.IsFalse(message2.HasFooEnum);
Assert.AreEqual(message2.FooEnum, TestOneof2.Types.NestedEnum.FOO);
}
[Test]
public void TestOneofTypes_String()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
Assert.AreEqual(builder.FooString, "");
Assert.IsTrue(builder.SetFooString("foo").HasFooString);
Assert.AreEqual(builder.FooString, "foo");
TestOneof2 message = builder.BuildPartial();
Assert.IsTrue(message.HasFooString);
Assert.AreEqual(message.FooString, "foo");
Assert.IsFalse(builder.ClearFooString().HasFooString);
TestOneof2 message2 = builder.Build();
Assert.IsFalse(message2.HasFooString);
Assert.AreEqual(message2.FooString, "");
builder.SetFooInt(123);
Assert.AreEqual(builder.FooString, "");
Assert.AreEqual(builder.FooInt, 123);
message = builder.Build();
Assert.AreEqual(message.FooString, "");
Assert.AreEqual(message.FooInt, 123);
}
[Test]
public void TestOneofTypes_Message()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
Assert.AreEqual(builder.FooMessage.QuxInt, 0);
builder.SetFooMessage(TestOneof2.Types.NestedMessage.CreateBuilder().SetQuxInt(234).Build());
Assert.IsTrue(builder.HasFooMessage);
Assert.AreEqual(builder.FooMessage.QuxInt, 234);
TestOneof2 message = builder.BuildPartial();
Assert.IsTrue(message.HasFooMessage);
Assert.AreEqual(message.FooMessage.QuxInt, 234);
Assert.IsFalse(builder.ClearFooMessage().HasFooMessage);
message = builder.Build();
Assert.IsFalse(message.HasFooMessage);
Assert.AreEqual(message.FooMessage.QuxInt, 0);
builder = TestOneof2.CreateBuilder();
Assert.IsFalse(builder.HasFooMessage);
builder.SetFooMessage(TestOneof2.Types.NestedMessage.CreateBuilder().SetQuxInt(123));
Assert.IsTrue(builder.HasFooMessage);
Assert.AreEqual(builder.FooMessage.QuxInt, 123);
message = builder.Build();
Assert.IsTrue(message.HasFooMessage);
Assert.AreEqual(message.FooMessage.QuxInt, 123);
}
[Test]
public void TestOneofMerge_Primitive()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooInt(123).Build();
TestOneof2 message2 = TestOneof2.CreateBuilder().MergeFrom(message).Build();
Assert.IsTrue(message2.HasFooInt);
Assert.AreEqual(message2.FooInt, 123);
}
[Test]
public void TestOneofMerge_String()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooString("foo").Build();
TestOneof2 message2 = TestOneof2.CreateBuilder().MergeFrom(message).Build();
Assert.IsTrue(message2.HasFooString);
Assert.AreEqual(message2.FooString, "foo");
}
[Test]
public void TestOneofMerge_Enum()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooEnum(TestOneof2.Types.NestedEnum.BAR).Build();
TestOneof2 message2 = TestOneof2.CreateBuilder().MergeFrom(message).Build();
Assert.IsTrue(message2.HasFooEnum);
Assert.AreEqual(message2.FooEnum, TestOneof2.Types.NestedEnum.BAR);
}
public void TestOneofMerge_message()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooMessage(
TestOneof2.Types.NestedMessage.CreateBuilder().SetQuxInt(234).Build()).Build();
TestOneof2 message2 = TestOneof2.CreateBuilder().MergeFrom(message).Build();
Assert.IsTrue(message2.HasFooMessage);
Assert.AreEqual(message2.FooMessage.QuxInt, 234);
}
[Test]
public void TestOneofMergeMixed()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooEnum(TestOneof2.Types.NestedEnum.BAR).Build();
TestOneof2 message2 =
TestOneof2.CreateBuilder().SetFooString("foo").MergeFrom(message).Build();
Assert.IsFalse(message2.HasFooString);
Assert.IsTrue(message2.HasFooEnum);
Assert.AreEqual(message2.FooEnum, TestOneof2.Types.NestedEnum.BAR);
}
[Test]
public void TestOneofSerialization_Primitive()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooInt(123).Build();
ByteString serialized = message.ToByteString();
TestOneof2 message2 = TestOneof2.ParseFrom(serialized);
Assert.IsTrue(message2.HasFooInt);
Assert.AreEqual(message2.FooInt, 123);
}
[Test]
public void TestOneofSerialization_String()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooString("foo").Build();
ByteString serialized = message.ToByteString();
TestOneof2 message2 = TestOneof2.ParseFrom(serialized);
Assert.IsTrue(message2.HasFooString);
Assert.AreEqual(message2.FooString, "foo");
}
[Test]
public void TestOneofSerialization_Enum()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooEnum(TestOneof2.Types.NestedEnum.BAR).Build();
ByteString serialized = message.ToByteString();
TestOneof2 message2 = TestOneof2.ParseFrom(serialized);
Assert.IsTrue(message2.HasFooEnum);
Assert.AreEqual(message2.FooEnum, TestOneof2.Types.NestedEnum.BAR);
}
[Test]
public void TestOneofSerialization_Message()
{
TestOneof2.Builder builder = TestOneof2.CreateBuilder();
TestOneof2 message = builder.SetFooMessage(
TestOneof2.Types.NestedMessage.CreateBuilder().SetQuxInt(234).Build()).Build();
ByteString serialized = message.ToByteString();
TestOneof2 message2 = TestOneof2.ParseFrom(serialized);
Assert.IsTrue(message2.HasFooMessage);
Assert.AreEqual(message2.FooMessage.QuxInt, 234);
}
} }
} }

View File

@ -380,6 +380,12 @@ namespace Google.ProtocolBuffers
message[f("default_string_piece")] = "424"; message[f("default_string_piece")] = "424";
message[f("default_cord")] = "425"; message[f("default_cord")] = "425";
message[f("oneof_uint32")] = 601U;
message[f("oneof_nested_message")] =
CreateBuilderForField(message, f("optional_nested_message")).SetField(nestedB, 602).WeakBuild();
message[f("oneof_string")] = "603";
message[f("oneof_bytes")] = TestUtil.ToBytes("604");
} }
/// <summary> /// <summary>
@ -642,6 +648,20 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("424", message[f("default_string_piece")]); Assert.AreEqual("424", message[f("default_string_piece")]);
Assert.AreEqual("425", message[f("default_cord")]); Assert.AreEqual("425", message[f("default_cord")]);
if (extensionRegistry == null)
{
Assert.IsFalse(message.HasField(f("oneof_uint32")));
Assert.IsFalse(message.HasField(f("oneof_nested_message")));
Assert.IsFalse(message.HasField(f("oneof_string")));
} else
{
Assert.IsTrue(message.HasField(f("oneof_uint32")));
Assert.IsTrue(message.HasField(f("oneof_nested_message")));
Assert.IsTrue(message.HasField(f("oneof_string")));
Assert.AreEqual(601U, message[f("oneof_uint32")]);
Assert.AreEqual(602, ((IMessage)message[f("oneof_nested_message")])[nestedB]);
Assert.AreEqual("603", message[f("oneof_string")]);
}
} }
/// <summary> /// <summary>
@ -793,6 +813,15 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("abc", message[f("default_string_piece")]); Assert.AreEqual("abc", message[f("default_string_piece")]);
Assert.AreEqual("123", message[f("default_cord")]); Assert.AreEqual("123", message[f("default_cord")]);
Assert.IsFalse(message.HasField(f("oneof_uint32")));
Assert.IsFalse(message.HasField(f("oneof_nested_message")));
Assert.IsFalse(message.HasField(f("oneof_string")));
Assert.IsFalse(message.HasField(f("oneof_bytes")));
Assert.AreEqual(0U, message[f("oneof_uint32")]);
Assert.AreEqual("", message[f("oneof_string")]);
Assert.AreEqual(TestUtil.ToBytes(""), message[f("oneof_bytes")]);
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------

File diff suppressed because it is too large Load Diff

View File

@ -97,7 +97,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
internal__static_proto2_nofieldpresence_unittest_TestAllTypes__Descriptor = Descriptor.MessageTypes[0]; internal__static_proto2_nofieldpresence_unittest_TestAllTypes__Descriptor = Descriptor.MessageTypes[0];
internal__static_proto2_nofieldpresence_unittest_TestAllTypes__FieldAccessorTable = internal__static_proto2_nofieldpresence_unittest_TestAllTypes__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes, global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Builder>(internal__static_proto2_nofieldpresence_unittest_TestAllTypes__Descriptor, new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes, global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Builder>(internal__static_proto2_nofieldpresence_unittest_TestAllTypes__Descriptor,
new string[] { "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalNestedMessage", "OptionalForeignMessage", "OptionalProto2Message", "OptionalNestedEnum", "OptionalForeignEnum", "OptionalStringPiece", "OptionalCord", "OptionalLazyMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedProto2Message", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedStringPiece", "RepeatedCord", "RepeatedLazyMessage", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofEnum", }); new string[] { "OptionalInt32", "OptionalInt64", "OptionalUint32", "OptionalUint64", "OptionalSint32", "OptionalSint64", "OptionalFixed32", "OptionalFixed64", "OptionalSfixed32", "OptionalSfixed64", "OptionalFloat", "OptionalDouble", "OptionalBool", "OptionalString", "OptionalBytes", "OptionalNestedMessage", "OptionalForeignMessage", "OptionalProto2Message", "OptionalNestedEnum", "OptionalForeignEnum", "OptionalStringPiece", "OptionalCord", "OptionalLazyMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedProto2Message", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedStringPiece", "RepeatedCord", "RepeatedLazyMessage", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofEnum", "OneofField", });
internal__static_proto2_nofieldpresence_unittest_TestAllTypes_NestedMessage__Descriptor = internal__static_proto2_nofieldpresence_unittest_TestAllTypes__Descriptor.NestedTypes[0]; internal__static_proto2_nofieldpresence_unittest_TestAllTypes_NestedMessage__Descriptor = internal__static_proto2_nofieldpresence_unittest_TestAllTypes__Descriptor.NestedTypes[0];
internal__static_proto2_nofieldpresence_unittest_TestAllTypes_NestedMessage__FieldAccessorTable = internal__static_proto2_nofieldpresence_unittest_TestAllTypes_NestedMessage__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage, global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.Builder>(internal__static_proto2_nofieldpresence_unittest_TestAllTypes_NestedMessage__Descriptor, new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage, global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.Builder>(internal__static_proto2_nofieldpresence_unittest_TestAllTypes_NestedMessage__Descriptor,
@ -431,6 +431,19 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
#endregion #endregion
private object oneofField_;
public enum OneofFieldOneofCase {
OneofUint32 = 111,
OneofNestedMessage = 112,
OneofString = 113,
OneofEnum = 114,
None = 0,
}
private OneofFieldOneofCase oneofFieldCase_ = OneofFieldOneofCase.None;
public OneofFieldOneofCase OneofFieldCase {
get { return oneofFieldCase_; }
}
public const int OptionalInt32FieldNumber = 1; public const int OptionalInt32FieldNumber = 1;
private int optionalInt32_; private int optionalInt32_;
public int OptionalInt32 { public int OptionalInt32 {
@ -586,6 +599,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedInt32FieldNumber = 31; public const int RepeatedInt32FieldNumber = 31;
private int repeatedInt32MemoizedSerializedSize;
private pbc::PopsicleList<int> repeatedInt32_ = new pbc::PopsicleList<int>(); private pbc::PopsicleList<int> repeatedInt32_ = new pbc::PopsicleList<int>();
public scg::IList<int> RepeatedInt32List { public scg::IList<int> RepeatedInt32List {
get { return pbc::Lists.AsReadOnly(repeatedInt32_); } get { return pbc::Lists.AsReadOnly(repeatedInt32_); }
@ -598,6 +612,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedInt64FieldNumber = 32; public const int RepeatedInt64FieldNumber = 32;
private int repeatedInt64MemoizedSerializedSize;
private pbc::PopsicleList<long> repeatedInt64_ = new pbc::PopsicleList<long>(); private pbc::PopsicleList<long> repeatedInt64_ = new pbc::PopsicleList<long>();
public scg::IList<long> RepeatedInt64List { public scg::IList<long> RepeatedInt64List {
get { return pbc::Lists.AsReadOnly(repeatedInt64_); } get { return pbc::Lists.AsReadOnly(repeatedInt64_); }
@ -610,6 +625,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedUint32FieldNumber = 33; public const int RepeatedUint32FieldNumber = 33;
private int repeatedUint32MemoizedSerializedSize;
private pbc::PopsicleList<uint> repeatedUint32_ = new pbc::PopsicleList<uint>(); private pbc::PopsicleList<uint> repeatedUint32_ = new pbc::PopsicleList<uint>();
public scg::IList<uint> RepeatedUint32List { public scg::IList<uint> RepeatedUint32List {
get { return pbc::Lists.AsReadOnly(repeatedUint32_); } get { return pbc::Lists.AsReadOnly(repeatedUint32_); }
@ -622,6 +638,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedUint64FieldNumber = 34; public const int RepeatedUint64FieldNumber = 34;
private int repeatedUint64MemoizedSerializedSize;
private pbc::PopsicleList<ulong> repeatedUint64_ = new pbc::PopsicleList<ulong>(); private pbc::PopsicleList<ulong> repeatedUint64_ = new pbc::PopsicleList<ulong>();
public scg::IList<ulong> RepeatedUint64List { public scg::IList<ulong> RepeatedUint64List {
get { return pbc::Lists.AsReadOnly(repeatedUint64_); } get { return pbc::Lists.AsReadOnly(repeatedUint64_); }
@ -634,6 +651,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedSint32FieldNumber = 35; public const int RepeatedSint32FieldNumber = 35;
private int repeatedSint32MemoizedSerializedSize;
private pbc::PopsicleList<int> repeatedSint32_ = new pbc::PopsicleList<int>(); private pbc::PopsicleList<int> repeatedSint32_ = new pbc::PopsicleList<int>();
public scg::IList<int> RepeatedSint32List { public scg::IList<int> RepeatedSint32List {
get { return pbc::Lists.AsReadOnly(repeatedSint32_); } get { return pbc::Lists.AsReadOnly(repeatedSint32_); }
@ -646,6 +664,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedSint64FieldNumber = 36; public const int RepeatedSint64FieldNumber = 36;
private int repeatedSint64MemoizedSerializedSize;
private pbc::PopsicleList<long> repeatedSint64_ = new pbc::PopsicleList<long>(); private pbc::PopsicleList<long> repeatedSint64_ = new pbc::PopsicleList<long>();
public scg::IList<long> RepeatedSint64List { public scg::IList<long> RepeatedSint64List {
get { return pbc::Lists.AsReadOnly(repeatedSint64_); } get { return pbc::Lists.AsReadOnly(repeatedSint64_); }
@ -658,6 +677,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedFixed32FieldNumber = 37; public const int RepeatedFixed32FieldNumber = 37;
private int repeatedFixed32MemoizedSerializedSize;
private pbc::PopsicleList<uint> repeatedFixed32_ = new pbc::PopsicleList<uint>(); private pbc::PopsicleList<uint> repeatedFixed32_ = new pbc::PopsicleList<uint>();
public scg::IList<uint> RepeatedFixed32List { public scg::IList<uint> RepeatedFixed32List {
get { return pbc::Lists.AsReadOnly(repeatedFixed32_); } get { return pbc::Lists.AsReadOnly(repeatedFixed32_); }
@ -670,6 +690,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedFixed64FieldNumber = 38; public const int RepeatedFixed64FieldNumber = 38;
private int repeatedFixed64MemoizedSerializedSize;
private pbc::PopsicleList<ulong> repeatedFixed64_ = new pbc::PopsicleList<ulong>(); private pbc::PopsicleList<ulong> repeatedFixed64_ = new pbc::PopsicleList<ulong>();
public scg::IList<ulong> RepeatedFixed64List { public scg::IList<ulong> RepeatedFixed64List {
get { return pbc::Lists.AsReadOnly(repeatedFixed64_); } get { return pbc::Lists.AsReadOnly(repeatedFixed64_); }
@ -682,6 +703,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedSfixed32FieldNumber = 39; public const int RepeatedSfixed32FieldNumber = 39;
private int repeatedSfixed32MemoizedSerializedSize;
private pbc::PopsicleList<int> repeatedSfixed32_ = new pbc::PopsicleList<int>(); private pbc::PopsicleList<int> repeatedSfixed32_ = new pbc::PopsicleList<int>();
public scg::IList<int> RepeatedSfixed32List { public scg::IList<int> RepeatedSfixed32List {
get { return pbc::Lists.AsReadOnly(repeatedSfixed32_); } get { return pbc::Lists.AsReadOnly(repeatedSfixed32_); }
@ -694,6 +716,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedSfixed64FieldNumber = 40; public const int RepeatedSfixed64FieldNumber = 40;
private int repeatedSfixed64MemoizedSerializedSize;
private pbc::PopsicleList<long> repeatedSfixed64_ = new pbc::PopsicleList<long>(); private pbc::PopsicleList<long> repeatedSfixed64_ = new pbc::PopsicleList<long>();
public scg::IList<long> RepeatedSfixed64List { public scg::IList<long> RepeatedSfixed64List {
get { return pbc::Lists.AsReadOnly(repeatedSfixed64_); } get { return pbc::Lists.AsReadOnly(repeatedSfixed64_); }
@ -706,6 +729,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedFloatFieldNumber = 41; public const int RepeatedFloatFieldNumber = 41;
private int repeatedFloatMemoizedSerializedSize;
private pbc::PopsicleList<float> repeatedFloat_ = new pbc::PopsicleList<float>(); private pbc::PopsicleList<float> repeatedFloat_ = new pbc::PopsicleList<float>();
public scg::IList<float> RepeatedFloatList { public scg::IList<float> RepeatedFloatList {
get { return pbc::Lists.AsReadOnly(repeatedFloat_); } get { return pbc::Lists.AsReadOnly(repeatedFloat_); }
@ -718,6 +742,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedDoubleFieldNumber = 42; public const int RepeatedDoubleFieldNumber = 42;
private int repeatedDoubleMemoizedSerializedSize;
private pbc::PopsicleList<double> repeatedDouble_ = new pbc::PopsicleList<double>(); private pbc::PopsicleList<double> repeatedDouble_ = new pbc::PopsicleList<double>();
public scg::IList<double> RepeatedDoubleList { public scg::IList<double> RepeatedDoubleList {
get { return pbc::Lists.AsReadOnly(repeatedDouble_); } get { return pbc::Lists.AsReadOnly(repeatedDouble_); }
@ -730,6 +755,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedBoolFieldNumber = 43; public const int RepeatedBoolFieldNumber = 43;
private int repeatedBoolMemoizedSerializedSize;
private pbc::PopsicleList<bool> repeatedBool_ = new pbc::PopsicleList<bool>(); private pbc::PopsicleList<bool> repeatedBool_ = new pbc::PopsicleList<bool>();
public scg::IList<bool> RepeatedBoolList { public scg::IList<bool> RepeatedBoolList {
get { return pbc::Lists.AsReadOnly(repeatedBool_); } get { return pbc::Lists.AsReadOnly(repeatedBool_); }
@ -802,6 +828,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedNestedEnumFieldNumber = 51; public const int RepeatedNestedEnumFieldNumber = 51;
private int repeatedNestedEnumMemoizedSerializedSize;
private pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum> repeatedNestedEnum_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum>(); private pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum> repeatedNestedEnum_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum>();
public scg::IList<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum> RepeatedNestedEnumList { public scg::IList<global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum> RepeatedNestedEnumList {
get { return pbc::Lists.AsReadOnly(repeatedNestedEnum_); } get { return pbc::Lists.AsReadOnly(repeatedNestedEnum_); }
@ -814,6 +841,7 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int RepeatedForeignEnumFieldNumber = 52; public const int RepeatedForeignEnumFieldNumber = 52;
private int repeatedForeignEnumMemoizedSerializedSize;
private pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.ForeignEnum> repeatedForeignEnum_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.ForeignEnum>(); private pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.ForeignEnum> repeatedForeignEnum_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.Proto3.ForeignEnum>();
public scg::IList<global::Google.ProtocolBuffers.TestProtos.Proto3.ForeignEnum> RepeatedForeignEnumList { public scg::IList<global::Google.ProtocolBuffers.TestProtos.Proto3.ForeignEnum> RepeatedForeignEnumList {
get { return pbc::Lists.AsReadOnly(repeatedForeignEnum_); } get { return pbc::Lists.AsReadOnly(repeatedForeignEnum_); }
@ -862,31 +890,23 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public const int OneofUint32FieldNumber = 111; public const int OneofUint32FieldNumber = 111;
private uint oneofUint32_;
public uint OneofUint32 { public uint OneofUint32 {
get { return oneofUint32_; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofUint32 ? (uint) oneofField_ : 0; }
} }
public const int OneofNestedMessageFieldNumber = 112; public const int OneofNestedMessageFieldNumber = 112;
private bool hasOneofNestedMessage;
private global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage oneofNestedMessage_;
public bool HasOneofNestedMessage {
get { return hasOneofNestedMessage; }
}
public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage OneofNestedMessage {
get { return oneofNestedMessage_ ?? global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.DefaultInstance; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage) oneofField_ : global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.DefaultInstance; }
} }
public const int OneofStringFieldNumber = 113; public const int OneofStringFieldNumber = 113;
private string oneofString_ = "";
public string OneofString { public string OneofString {
get { return oneofString_; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofString ? (string) oneofField_ : ""; }
} }
public const int OneofEnumFieldNumber = 114; public const int OneofEnumFieldNumber = 114;
private global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum oneofEnum_ = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO;
public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum OneofEnum { public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum OneofEnum {
get { return oneofEnum_; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofEnum ? (global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum) oneofField_ : global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO; }
} }
public override void WriteTo(pb::ICodedOutputStream output) { public override void WriteTo(pb::ICodedOutputStream output) {
@ -962,43 +982,43 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
output.WriteMessage(30, field_names[15], OptionalLazyMessage); output.WriteMessage(30, field_names[15], OptionalLazyMessage);
} }
if (repeatedInt32_.Count > 0) { if (repeatedInt32_.Count > 0) {
output.WriteInt32Array(31, field_names[36], repeatedInt32_); output.WritePackedInt32Array(31, field_names[36], repeatedInt32MemoizedSerializedSize, repeatedInt32_);
} }
if (repeatedInt64_.Count > 0) { if (repeatedInt64_.Count > 0) {
output.WriteInt64Array(32, field_names[37], repeatedInt64_); output.WritePackedInt64Array(32, field_names[37], repeatedInt64MemoizedSerializedSize, repeatedInt64_);
} }
if (repeatedUint32_.Count > 0) { if (repeatedUint32_.Count > 0) {
output.WriteUInt32Array(33, field_names[48], repeatedUint32_); output.WritePackedUInt32Array(33, field_names[48], repeatedUint32MemoizedSerializedSize, repeatedUint32_);
} }
if (repeatedUint64_.Count > 0) { if (repeatedUint64_.Count > 0) {
output.WriteUInt64Array(34, field_names[49], repeatedUint64_); output.WritePackedUInt64Array(34, field_names[49], repeatedUint64MemoizedSerializedSize, repeatedUint64_);
} }
if (repeatedSint32_.Count > 0) { if (repeatedSint32_.Count > 0) {
output.WriteSInt32Array(35, field_names[44], repeatedSint32_); output.WritePackedSInt32Array(35, field_names[44], repeatedSint32MemoizedSerializedSize, repeatedSint32_);
} }
if (repeatedSint64_.Count > 0) { if (repeatedSint64_.Count > 0) {
output.WriteSInt64Array(36, field_names[45], repeatedSint64_); output.WritePackedSInt64Array(36, field_names[45], repeatedSint64MemoizedSerializedSize, repeatedSint64_);
} }
if (repeatedFixed32_.Count > 0) { if (repeatedFixed32_.Count > 0) {
output.WriteFixed32Array(37, field_names[31], repeatedFixed32_); output.WritePackedFixed32Array(37, field_names[31], repeatedFixed32MemoizedSerializedSize, repeatedFixed32_);
} }
if (repeatedFixed64_.Count > 0) { if (repeatedFixed64_.Count > 0) {
output.WriteFixed64Array(38, field_names[32], repeatedFixed64_); output.WritePackedFixed64Array(38, field_names[32], repeatedFixed64MemoizedSerializedSize, repeatedFixed64_);
} }
if (repeatedSfixed32_.Count > 0) { if (repeatedSfixed32_.Count > 0) {
output.WriteSFixed32Array(39, field_names[42], repeatedSfixed32_); output.WritePackedSFixed32Array(39, field_names[42], repeatedSfixed32MemoizedSerializedSize, repeatedSfixed32_);
} }
if (repeatedSfixed64_.Count > 0) { if (repeatedSfixed64_.Count > 0) {
output.WriteSFixed64Array(40, field_names[43], repeatedSfixed64_); output.WritePackedSFixed64Array(40, field_names[43], repeatedSfixed64MemoizedSerializedSize, repeatedSfixed64_);
} }
if (repeatedFloat_.Count > 0) { if (repeatedFloat_.Count > 0) {
output.WriteFloatArray(41, field_names[33], repeatedFloat_); output.WritePackedFloatArray(41, field_names[33], repeatedFloatMemoizedSerializedSize, repeatedFloat_);
} }
if (repeatedDouble_.Count > 0) { if (repeatedDouble_.Count > 0) {
output.WriteDoubleArray(42, field_names[30], repeatedDouble_); output.WritePackedDoubleArray(42, field_names[30], repeatedDoubleMemoizedSerializedSize, repeatedDouble_);
} }
if (repeatedBool_.Count > 0) { if (repeatedBool_.Count > 0) {
output.WriteBoolArray(43, field_names[27], repeatedBool_); output.WritePackedBoolArray(43, field_names[27], repeatedBoolMemoizedSerializedSize, repeatedBool_);
} }
if (repeatedString_.Count > 0) { if (repeatedString_.Count > 0) {
output.WriteStringArray(44, field_names[46], repeatedString_); output.WriteStringArray(44, field_names[46], repeatedString_);
@ -1016,10 +1036,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
output.WriteMessageArray(50, field_names[41], repeatedProto2Message_); output.WriteMessageArray(50, field_names[41], repeatedProto2Message_);
} }
if (repeatedNestedEnum_.Count > 0) { if (repeatedNestedEnum_.Count > 0) {
output.WriteEnumArray(51, field_names[39], repeatedNestedEnum_); output.WritePackedEnumArray(51, field_names[39], repeatedNestedEnumMemoizedSerializedSize, repeatedNestedEnum_);
} }
if (repeatedForeignEnum_.Count > 0) { if (repeatedForeignEnum_.Count > 0) {
output.WriteEnumArray(52, field_names[34], repeatedForeignEnum_); output.WritePackedEnumArray(52, field_names[34], repeatedForeignEnumMemoizedSerializedSize, repeatedForeignEnum_);
} }
if (repeatedStringPiece_.Count > 0) { if (repeatedStringPiece_.Count > 0) {
output.WriteStringArray(54, field_names[47], repeatedStringPiece_); output.WriteStringArray(54, field_names[47], repeatedStringPiece_);
@ -1030,16 +1050,16 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
if (repeatedLazyMessage_.Count > 0) { if (repeatedLazyMessage_.Count > 0) {
output.WriteMessageArray(57, field_names[38], repeatedLazyMessage_); output.WriteMessageArray(57, field_names[38], repeatedLazyMessage_);
} }
if (OneofUint32 != 0) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofUint32) {
output.WriteUInt32(111, field_names[3], OneofUint32); output.WriteUInt32(111, field_names[3], OneofUint32);
} }
if (hasOneofNestedMessage) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
output.WriteMessage(112, field_names[1], OneofNestedMessage); output.WriteMessage(112, field_names[1], OneofNestedMessage);
} }
if (OneofString != "") { if (oneofFieldCase_ == OneofFieldOneofCase.OneofString) {
output.WriteString(113, field_names[2], OneofString); output.WriteString(113, field_names[2], OneofString);
} }
if (OneofEnum != global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofEnum) {
output.WriteEnum(114, field_names[0], (int) OneofEnum, OneofEnum); output.WriteEnum(114, field_names[0], (int) OneofEnum, OneofEnum);
} }
UnknownFields.WriteTo(output); UnknownFields.WriteTo(output);
@ -1134,7 +1154,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeInt32SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeInt32SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedInt32_.Count; if (repeatedInt32_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedInt32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1142,7 +1165,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeInt64SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeInt64SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedInt64_.Count; if (repeatedInt64_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedInt64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1150,7 +1176,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeUInt32SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeUInt32SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedUint32_.Count; if (repeatedUint32_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedUint32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1158,7 +1187,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeUInt64SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeUInt64SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedUint64_.Count; if (repeatedUint64_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedUint64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1166,7 +1198,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeSInt32SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeSInt32SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedSint32_.Count; if (repeatedSint32_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedSint32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1174,49 +1209,73 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeSInt64SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeSInt64SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedSint64_.Count; if (repeatedSint64_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedSint64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 4 * repeatedFixed32_.Count; dataSize = 4 * repeatedFixed32_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedFixed32_.Count; if (repeatedFixed32_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedFixed32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 8 * repeatedFixed64_.Count; dataSize = 8 * repeatedFixed64_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedFixed64_.Count; if (repeatedFixed64_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedFixed64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 4 * repeatedSfixed32_.Count; dataSize = 4 * repeatedSfixed32_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedSfixed32_.Count; if (repeatedSfixed32_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedSfixed32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 8 * repeatedSfixed64_.Count; dataSize = 8 * repeatedSfixed64_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedSfixed64_.Count; if (repeatedSfixed64_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedSfixed64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 4 * repeatedFloat_.Count; dataSize = 4 * repeatedFloat_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedFloat_.Count; if (repeatedFloat_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedFloatMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 8 * repeatedDouble_.Count; dataSize = 8 * repeatedDouble_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedDouble_.Count; if (repeatedDouble_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedDoubleMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 1 * repeatedBool_.Count; dataSize = 1 * repeatedBool_.Count;
size += dataSize; size += dataSize;
size += 2 * repeatedBool_.Count; if (repeatedBool_.Count != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
repeatedBoolMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1250,8 +1309,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element); dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedNestedEnum_.Count; size += 2;
size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);
} }
repeatedNestedEnumMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1260,8 +1321,10 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element); dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);
} }
size += dataSize; size += dataSize;
size += 2 * repeatedForeignEnum_.Count; size += 2;
size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);
} }
repeatedForeignEnumMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
@ -1282,16 +1345,16 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
foreach (global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage element in RepeatedLazyMessageList) { foreach (global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage element in RepeatedLazyMessageList) {
size += pb::CodedOutputStream.ComputeMessageSize(57, element); size += pb::CodedOutputStream.ComputeMessageSize(57, element);
} }
if (OneofUint32 != 0) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofUint32) {
size += pb::CodedOutputStream.ComputeUInt32Size(111, OneofUint32); size += pb::CodedOutputStream.ComputeUInt32Size(111, OneofUint32);
} }
if (hasOneofNestedMessage) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
size += pb::CodedOutputStream.ComputeMessageSize(112, OneofNestedMessage); size += pb::CodedOutputStream.ComputeMessageSize(112, OneofNestedMessage);
} }
if (OneofString != "") { if (oneofFieldCase_ == OneofFieldOneofCase.OneofString) {
size += pb::CodedOutputStream.ComputeStringSize(113, OneofString); size += pb::CodedOutputStream.ComputeStringSize(113, OneofString);
} }
if (OneofEnum != global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofEnum) {
size += pb::CodedOutputStream.ComputeEnumSize(114, (int) OneofEnum); size += pb::CodedOutputStream.ComputeEnumSize(114, (int) OneofEnum);
} }
size += UnknownFields.SerializedSize; size += UnknownFields.SerializedSize;
@ -1577,17 +1640,24 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
if (other.repeatedLazyMessage_.Count != 0) { if (other.repeatedLazyMessage_.Count != 0) {
result.repeatedLazyMessage_.Add(other.repeatedLazyMessage_); result.repeatedLazyMessage_.Add(other.repeatedLazyMessage_);
} }
if (other.OneofUint32 != 0) { switch (other.OneofFieldCase) {
OneofUint32 = other.OneofUint32; case OneofFieldOneofCase.OneofUint32: {
SetOneofUint32(other.OneofUint32);
break;
} }
if (other.HasOneofNestedMessage) { case OneofFieldOneofCase.OneofNestedMessage: {
MergeOneofNestedMessage(other.OneofNestedMessage); MergeOneofNestedMessage(other.OneofNestedMessage);
break;
} }
if (other.OneofString != "") { case OneofFieldOneofCase.OneofString: {
OneofString = other.OneofString; SetOneofString(other.OneofString);
break;
} }
if (other.OneofEnum != global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO) { case OneofFieldOneofCase.OneofEnum: {
OneofEnum = other.OneofEnum; SetOneofEnum(other.OneofEnum);
break;
}
case OneofFieldOneofCase.None: { break; }
} }
this.MergeUnknownFields(other.UnknownFields); this.MergeUnknownFields(other.UnknownFields);
return this; return this;
@ -1884,25 +1954,37 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
break; break;
} }
case 888: { case 888: {
input.ReadUInt32(ref result.oneofUint32_); uint value = 0;
if (input.ReadUInt32(ref value)) {
result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofUint32;
}
break; break;
} }
case 898: { case 898: {
global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.Builder subBuilder = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.CreateBuilder(); global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.Builder subBuilder = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.CreateBuilder();
if (result.hasOneofNestedMessage) { if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
subBuilder.MergeFrom(OneofNestedMessage); subBuilder.MergeFrom(OneofNestedMessage);
} }
input.ReadMessage(subBuilder, extensionRegistry); input.ReadMessage(subBuilder, extensionRegistry);
OneofNestedMessage = subBuilder.BuildPartial(); result.oneofField_ = subBuilder.BuildPartial();
result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
break; break;
} }
case 906: { case 906: {
input.ReadString(ref result.oneofString_); string value = "";
if (input.ReadString(ref value)) {
result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofString;
}
break; break;
} }
case 912: { case 912: {
object unknown; object unknown;
if(input.ReadEnum(ref result.oneofEnum_, out unknown)) { global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum enumValue = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO;
if(input.ReadEnum(ref enumValue, out unknown)) {
result.oneofField_ = enumValue;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofEnum;
} else if(unknown is int) { } else if(unknown is int) {
if (unknownFields == null) { if (unknownFields == null) {
unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields); unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);
@ -3125,88 +3207,106 @@ namespace Google.ProtocolBuffers.TestProtos.Proto3 {
} }
public uint OneofUint32 { public uint OneofUint32 {
get { return result.OneofUint32; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofUint32 ? (uint) result.oneofField_ : 0; }
set { SetOneofUint32(value); } set { SetOneofUint32(value); }
} }
public Builder SetOneofUint32(uint value) { public Builder SetOneofUint32(uint value) {
PrepareBuilder(); PrepareBuilder();
result.oneofUint32_ = value; result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofUint32;
return this; return this;
} }
public Builder ClearOneofUint32() { public Builder ClearOneofUint32() {
PrepareBuilder(); PrepareBuilder();
result.oneofUint32_ = 0; if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofUint32) {
result.oneofFieldCase_ = OneofFieldOneofCase.None;
}
return this; return this;
} }
public bool HasOneofNestedMessage {
get { return result.hasOneofNestedMessage; }
}
public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage OneofNestedMessage {
get { return result.OneofNestedMessage; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.DefaultInstance; }
set { SetOneofNestedMessage(value); } set { SetOneofNestedMessage(value); }
} }
public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage value) { public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
result.hasOneofNestedMessage = true; result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
result.oneofNestedMessage_ = value; result.oneofField_ = value;
return this; return this;
} }
public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.Builder builderForValue) { public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.Builder builderForValue) {
pb::ThrowHelper.ThrowIfNull(builderForValue, "builderForValue"); pb::ThrowHelper.ThrowIfNull(builderForValue, "builderForValue");
PrepareBuilder(); PrepareBuilder();
result.hasOneofNestedMessage = true; result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
result.oneofNestedMessage_ = builderForValue.Build(); result.oneofField_ = builderForValue.Build();
return this; return this;
} }
public Builder MergeOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage value) { public Builder MergeOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
if (result.hasOneofNestedMessage && if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage &&
result.oneofNestedMessage_ != global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.DefaultInstance) { result.OneofNestedMessage != global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.DefaultInstance) {
result.oneofNestedMessage_ = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.CreateBuilder(result.oneofNestedMessage_).MergeFrom(value).BuildPartial(); result.oneofField_ = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedMessage.CreateBuilder(result.OneofNestedMessage).MergeFrom(value).BuildPartial();
} else { } else {
result.oneofNestedMessage_ = value; result.oneofField_ = value;
} }
result.hasOneofNestedMessage = true; result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
return this; return this;
} }
public Builder ClearOneofNestedMessage() { public Builder ClearOneofNestedMessage() {
if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
PrepareBuilder(); PrepareBuilder();
result.hasOneofNestedMessage = false; result.oneofFieldCase_ = OneofFieldOneofCase.None;
result.oneofNestedMessage_ = null; result.oneofField_ = null;
}
return this; return this;
} }
public string OneofString { public string OneofString {
get { return result.OneofString; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofString ? (string) result.oneofField_ : ""; }
set { SetOneofString(value); } set { SetOneofString(value); }
} }
public Builder SetOneofString(string value) { public Builder SetOneofString(string value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
result.oneofString_ = value; result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofString;
return this; return this;
} }
public Builder ClearOneofString() { public Builder ClearOneofString() {
PrepareBuilder(); PrepareBuilder();
result.oneofString_ = ""; if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofString) {
result.oneofFieldCase_ = OneofFieldOneofCase.None;
}
return this; return this;
} }
public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum OneofEnum { public global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum OneofEnum {
get { return result.OneofEnum; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofEnum ? (global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO; }
set { SetOneofEnum(value); } set { SetOneofEnum(value); }
} }
public Builder SetOneofEnum(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum value) { public Builder SetOneofEnum(global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum value) {
PrepareBuilder(); PrepareBuilder();
result.oneofEnum_ = value; result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofEnum;
return this; return this;
} }
public Builder ClearOneofEnum() { public Builder ClearOneofEnum() {
PrepareBuilder(); PrepareBuilder();
result.oneofEnum_ = global::Google.ProtocolBuffers.TestProtos.Proto3.TestAllTypes.Types.NestedEnum.FOO; if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofEnum) {
result.oneofFieldCase_ = OneofFieldOneofCase.None;
}
return this;
}
public OneofFieldOneofCase OneofFieldCase {
get { return result.oneofFieldCase_; }
}
public Builder ClearOneofField() {
PrepareBuilder();
result.oneofField_ = null;
result.oneofFieldCase_ = OneofFieldOneofCase.None;
return this; return this;
} }
} }

View File

@ -54,7 +54,7 @@ namespace Google.ProtocolBuffers.TestProtos {
internal__static_protobuf_unittest_TestOptimizedForSize__Descriptor = Descriptor.MessageTypes[0]; internal__static_protobuf_unittest_TestOptimizedForSize__Descriptor = Descriptor.MessageTypes[0];
internal__static_protobuf_unittest_TestOptimizedForSize__FieldAccessorTable = internal__static_protobuf_unittest_TestOptimizedForSize__FieldAccessorTable =
new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize, global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.Builder>(internal__static_protobuf_unittest_TestOptimizedForSize__Descriptor, new pb::FieldAccess.FieldAccessorTable<global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize, global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.Builder>(internal__static_protobuf_unittest_TestOptimizedForSize__Descriptor,
new string[] { "I", "Msg", "IntegerField", "StringField", }); new string[] { "I", "Msg", "IntegerField", "StringField", "Foo", });
global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.TestExtension = pb::GeneratedSingleExtension<int>.CreateInstance(global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.Descriptor.Extensions[0]); global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.TestExtension = pb::GeneratedSingleExtension<int>.CreateInstance(global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.Descriptor.Extensions[0]);
global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.TestExtension2 = pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize>.CreateInstance(global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.Descriptor.Extensions[1]); global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.TestExtension2 = pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize>.CreateInstance(global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.Descriptor.Extensions[1]);
internal__static_protobuf_unittest_TestRequiredOptimizedForSize__Descriptor = Descriptor.MessageTypes[1]; internal__static_protobuf_unittest_TestRequiredOptimizedForSize__Descriptor = Descriptor.MessageTypes[1];
@ -107,6 +107,17 @@ namespace Google.ProtocolBuffers.TestProtos {
public static pb::GeneratedExtensionBase<int> TestExtension; public static pb::GeneratedExtensionBase<int> TestExtension;
public const int TestExtension2FieldNumber = 1235; public const int TestExtension2FieldNumber = 1235;
public static pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize> TestExtension2; public static pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize> TestExtension2;
private object foo_;
public enum FooOneofCase {
IntegerField = 2,
StringField = 3,
None = 0,
}
private FooOneofCase fooCase_ = FooOneofCase.None;
public FooOneofCase FooCase {
get { return fooCase_; }
}
public const int IFieldNumber = 1; public const int IFieldNumber = 1;
private bool hasI; private bool hasI;
private int i_; private int i_;
@ -128,23 +139,19 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public const int IntegerFieldFieldNumber = 2; public const int IntegerFieldFieldNumber = 2;
private bool hasIntegerField;
private int integerField_;
public bool HasIntegerField { public bool HasIntegerField {
get { return hasIntegerField; } get { return fooCase_ == FooOneofCase.IntegerField; }
} }
public int IntegerField { public int IntegerField {
get { return integerField_; } get { return fooCase_ == FooOneofCase.IntegerField ? (int) foo_ : 0; }
} }
public const int StringFieldFieldNumber = 3; public const int StringFieldFieldNumber = 3;
private bool hasStringField;
private string stringField_ = "";
public bool HasStringField { public bool HasStringField {
get { return hasStringField; } get { return fooCase_ == FooOneofCase.StringField; }
} }
public string StringField { public string StringField {
get { return stringField_; } get { return fooCase_ == FooOneofCase.StringField ? (string) foo_ : ""; }
} }
public static TestOptimizedForSize ParseFrom(pb::ByteString data) { public static TestOptimizedForSize ParseFrom(pb::ByteString data) {
@ -315,43 +322,55 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasIntegerField { public bool HasIntegerField {
get { return result.hasIntegerField; } get { return result.fooCase_ == FooOneofCase.IntegerField; }
} }
public int IntegerField { public int IntegerField {
get { return result.IntegerField; } get { return result.fooCase_ == FooOneofCase.IntegerField ? (int) result.foo_ : 0; }
set { SetIntegerField(value); } set { SetIntegerField(value); }
} }
public Builder SetIntegerField(int value) { public Builder SetIntegerField(int value) {
PrepareBuilder(); PrepareBuilder();
result.hasIntegerField = true; result.foo_ = value;
result.integerField_ = value; result.fooCase_ = FooOneofCase.IntegerField;
return this; return this;
} }
public Builder ClearIntegerField() { public Builder ClearIntegerField() {
PrepareBuilder(); PrepareBuilder();
result.hasIntegerField = false; if (result.fooCase_ == FooOneofCase.IntegerField) {
result.integerField_ = 0; result.fooCase_ = FooOneofCase.None;
}
return this; return this;
} }
public bool HasStringField { public bool HasStringField {
get { return result.hasStringField; } get { return result.fooCase_ == FooOneofCase.StringField; }
} }
public string StringField { public string StringField {
get { return result.StringField; } get { return result.fooCase_ == FooOneofCase.StringField ? (string) result.foo_ : ""; }
set { SetStringField(value); } set { SetStringField(value); }
} }
public Builder SetStringField(string value) { public Builder SetStringField(string value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
result.hasStringField = true; result.foo_ = value;
result.stringField_ = value; result.fooCase_ = FooOneofCase.StringField;
return this; return this;
} }
public Builder ClearStringField() { public Builder ClearStringField() {
PrepareBuilder(); PrepareBuilder();
result.hasStringField = false; if (result.fooCase_ == FooOneofCase.StringField) {
result.stringField_ = ""; result.fooCase_ = FooOneofCase.None;
}
return this;
}
public FooOneofCase FooCase {
get { return result.fooCase_; }
}
public Builder ClearFoo() {
PrepareBuilder();
result.foo_ = null;
result.fooCase_ = FooOneofCase.None;
return this; return this;
} }
} }

View File

@ -16,7 +16,7 @@ Q80CAICbQ9ECAAAAAACAakDRAgAAAAAAgHNA2AIB2AIA4gIDMjE14gIDMzE16gIDMjE26gIDMzE2
8wL4AtkB9ALzAvgCvQL0AoIDAwjaAYIDAwi+AooDAwjbAYoDAwi/ApIDAwjcAZIDAwjAApgDApgD 8wL4AtkB9ALzAvgCvQL0AoIDAwjaAYIDAwi+AooDAwjbAYoDAwi/ApIDAwjcAZIDAwjAApgDApgD
A6ADBaADBqgDCKgDCbIDAzIyNLIDAzMyNLoDAzIyNboDAzMyNegDkQPwA5ID+AOTA4AElAOIBKoG A6ADBaADBqgDCKgDCbIDAzIyNLIDAzMyNLoDAzIyNboDAzMyNegDkQPwA5ID+AOTA4AElAOIBKoG
kASsBp0ElwEAAKEEmAEAAAAAAACtBJkBAACxBJoBAAAAAAAAvQQAgM1DwQQAAAAAAMB5QMgEANIE kASsBp0ElwEAAKEEmAEAAAAAAACtBJkBAACxBJoBAAAAAAAAvQQAgM1DwQQAAAAAAMB5QMgEANIE
AzQxNdoEAzQxNogFAZAFBJgFB6IFAzQyNKoFAzQyNQ== AzQxNdoEAzQxNogFAZAFBJgFB6IFAzQyNKoFAzQyNZIHAzYwNA==
"); ");
} }
} }
@ -158,6 +158,7 @@ default_foreign_enum: FOREIGN_FOO
default_import_enum: IMPORT_FOO default_import_enum: IMPORT_FOO
default_string_piece: ""424"" default_string_piece: ""424""
default_cord: ""425"" default_cord: ""425""
oneof_bytes: ""604""
"; ";
} }
@ -286,6 +287,12 @@ default_cord: ""425""
[protobuf_unittest.default_import_enum_extension]: IMPORT_FOO [protobuf_unittest.default_import_enum_extension]: IMPORT_FOO
[protobuf_unittest.default_string_piece_extension]: ""424"" [protobuf_unittest.default_string_piece_extension]: ""424""
[protobuf_unittest.default_cord_extension]: ""425"" [protobuf_unittest.default_cord_extension]: ""425""
[protobuf_unittest.oneof_uint32_extension]: 601
[protobuf_unittest.oneof_nested_message_extension] {
bb: 602
}
[protobuf_unittest.oneof_string_extension]: ""603""
[protobuf_unittest.oneof_bytes_extension]: ""604""
"; ";
} }
} }

View File

@ -182,6 +182,11 @@ namespace Google.ProtocolBuffers
registry.Add(Unittest.PackedDoubleExtension); registry.Add(Unittest.PackedDoubleExtension);
registry.Add(Unittest.PackedBoolExtension); registry.Add(Unittest.PackedBoolExtension);
registry.Add(Unittest.PackedEnumExtension); registry.Add(Unittest.PackedEnumExtension);
registry.Add(Unittest.OneofUint32Extension);
registry.Add(Unittest.OneofNestedMessageExtension);
registry.Add(Unittest.OneofStringExtension);
registry.Add(Unittest.OneofBytesExtension);
} }
/// <summary> /// <summary>
@ -316,6 +321,13 @@ namespace Google.ProtocolBuffers
message.SetDefaultStringPiece("424"); message.SetDefaultStringPiece("424");
message.SetDefaultCord("425"); message.SetDefaultCord("425");
message.SetOneofUint32(601);
message.SetOneofNestedMessage(
TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(602).Build());
message.SetOneofString("603");
message.SetOneofBytes(ToBytes("604"));
} }
/// <summary> /// <summary>
@ -517,6 +529,13 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("424", message.DefaultStringPiece); Assert.AreEqual("424", message.DefaultStringPiece);
Assert.AreEqual("425", message.DefaultCord); Assert.AreEqual("425", message.DefaultCord);
Assert.IsFalse(message.HasOneofUint32);
Assert.IsFalse(message.HasOneofNestedMessage);
Assert.IsFalse(message.HasOneofString);
Assert.IsTrue(message.HasOneofBytes);
Assert.AreEqual(ToBytes("604"), message.OneofBytes);
} }
internal static void AssertClear(TestAllTypes message) internal static void AssertClear(TestAllTypes message)
@ -661,6 +680,11 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("abc", message.DefaultStringPiece); Assert.AreEqual("abc", message.DefaultStringPiece);
Assert.AreEqual("123", message.DefaultCord); Assert.AreEqual("123", message.DefaultCord);
Assert.IsFalse(message.HasOneofUint32);
Assert.IsFalse(message.HasOneofNestedMessage);
Assert.IsFalse(message.HasOneofString);
Assert.IsFalse(message.HasOneofBytes);
} }
/// <summary> /// <summary>
@ -817,6 +841,12 @@ namespace Google.ProtocolBuffers
message.SetExtension(Unittest.DefaultStringPieceExtension, "424"); message.SetExtension(Unittest.DefaultStringPieceExtension, "424");
message.SetExtension(Unittest.DefaultCordExtension, "425"); message.SetExtension(Unittest.DefaultCordExtension, "425");
message.SetExtension(Unittest.OneofUint32Extension, 601U);
message.SetExtension(Unittest.OneofNestedMessageExtension,
TestAllTypes.Types.NestedMessage.CreateBuilder().SetBb(602).Build());
message.SetExtension(Unittest.OneofStringExtension, "603");
message.SetExtension(Unittest.OneofBytesExtension, ToBytes("604"));
} }
internal static void ModifyRepeatedFields(TestAllTypes.Builder message) internal static void ModifyRepeatedFields(TestAllTypes.Builder message)
@ -1165,6 +1195,9 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("424", message.GetExtension(Unittest.DefaultStringPieceExtension)); Assert.AreEqual("424", message.GetExtension(Unittest.DefaultStringPieceExtension));
Assert.AreEqual("425", message.GetExtension(Unittest.DefaultCordExtension)); Assert.AreEqual("425", message.GetExtension(Unittest.DefaultCordExtension));
Assert.IsTrue(message.HasExtension(Unittest.OneofBytesExtension));
Assert.AreEqual(ToBytes("604"), message.GetExtension(Unittest.OneofBytesExtension));
} }
/// <summary> /// <summary>
@ -1451,6 +1484,11 @@ namespace Google.ProtocolBuffers
Assert.AreEqual("abc", message.GetExtension(Unittest.DefaultStringPieceExtension)); Assert.AreEqual("abc", message.GetExtension(Unittest.DefaultStringPieceExtension));
Assert.AreEqual("123", message.GetExtension(Unittest.DefaultCordExtension)); Assert.AreEqual("123", message.GetExtension(Unittest.DefaultCordExtension));
Assert.IsFalse(message.HasExtension(Unittest.OneofUint32Extension));
Assert.IsFalse(message.HasExtension(Unittest.OneofNestedMessageExtension));
Assert.IsFalse(message.HasExtension(Unittest.OneofStringExtension));
Assert.IsFalse(message.HasExtension(Unittest.OneofBytesExtension));
} }
/// <summary> /// <summary>
@ -1714,6 +1752,75 @@ namespace Google.ProtocolBuffers
Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(Unittest.UnpackedEnumExtension, 1)); Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, message.GetExtension(Unittest.UnpackedEnumExtension, 1));
} }
public static void AssertAtMostOneFieldSetOneof(TestOneof2 message)
{
int count = 0;
if (message.HasFooInt) { ++count; }
if (message.HasFooString) { ++count; }
if (message.HasFooCord) { ++count; }
if (message.HasFooStringPiece) { ++count; }
if (message.HasFooBytes) { ++count; }
if (message.HasFooEnum) { ++count; }
if (message.HasFooMessage) { ++count; }
if (message.HasFooGroup) { ++count; }
if (message.HasFooLazyMessage) { ++count; }
Assert.True(count <= 1);
count = 0;
if (message.HasBarInt) { ++count; }
if (message.HasBarString) { ++count; }
if (message.HasBarCord) { ++count; }
if (message.HasBarStringPiece) { ++count; }
if (message.HasBarBytes) { ++count; }
if (message.HasBarEnum) { ++count; }
Assert.True(count <= 1);
switch (message.FooCase)
{
case TestOneof2.FooOneofCase.FooInt:
{
Assert.True(message.HasFooInt);
break;
}
case TestOneof2.FooOneofCase.FooString:
{
Assert.True(message.HasFooString);
break;
}
case TestOneof2.FooOneofCase.FooCord:
{
Assert.True(message.HasFooCord);
break;
}
case TestOneof2.FooOneofCase.FooBytes:
{
Assert.True(message.HasFooBytes);
break;
}
case TestOneof2.FooOneofCase.FooEnum:
{
Assert.True(message.HasFooEnum);
break;
}
case TestOneof2.FooOneofCase.FooMessage:
{
Assert.True(message.HasFooMessage);
break;
}
case TestOneof2.FooOneofCase.FooGroup:
{
Assert.True(message.HasFooGroup);
break;
}
case TestOneof2.FooOneofCase.FooLazyMessage:
{
Assert.True(message.HasFooLazyMessage);
break;
}
case TestOneof2.FooOneofCase.None: { break; }
}
}
private static readonly string[] TestCultures = {"en-US", "en-GB", "fr-FR", "de-DE"}; private static readonly string[] TestCultures = {"en-US", "en-GB", "fr-FR", "de-DE"};
public delegate void CultureAction(); public delegate void CultureAction();

View File

@ -164,7 +164,7 @@ namespace Google.ProtocolBuffers
[Test] [Test]
public void ExtensionsSerializedSize() public void ExtensionsSerializedSize()
{ {
Assert.AreEqual(TestUtil.GetAllSet().SerializedSize, TestUtil.GetAllExtensionsSet().SerializedSize); Assert.IsTrue(TestUtil.GetAllSet().SerializedSize < TestUtil.GetAllExtensionsSet().SerializedSize);
} }
private static void AssertFieldsInOrder(ByteString data) private static void AssertFieldsInOrder(ByteString data)

View File

@ -58,8 +58,11 @@ namespace Google.ProtocolBuffers
public abstract int GetRepeatedFieldCount(FieldDescriptor field); public abstract int GetRepeatedFieldCount(FieldDescriptor field);
public abstract object this[FieldDescriptor field, int index] { get; set; } public abstract object this[FieldDescriptor field, int index] { get; set; }
public abstract bool HasField(FieldDescriptor field); public abstract bool HasField(FieldDescriptor field);
public abstract bool HasOneof(OneofDescriptor oneof);
public abstract FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof);
public abstract IBuilder CreateBuilderForField(FieldDescriptor field); public abstract IBuilder CreateBuilderForField(FieldDescriptor field);
public abstract TBuilder ClearField(FieldDescriptor field); public abstract TBuilder ClearField(FieldDescriptor field);
public abstract TBuilder ClearOneof(OneofDescriptor oneof);
public abstract TBuilder AddRepeatedField(FieldDescriptor field, object value); public abstract TBuilder AddRepeatedField(FieldDescriptor field, object value);
#endregion #endregion
@ -248,6 +251,11 @@ namespace Google.ProtocolBuffers
return ClearField(field); return ClearField(field);
} }
IBuilder IBuilder.WeakClearOneof(OneofDescriptor oneof)
{
return ClearOneof(oneof);
}
#endregion #endregion
/// <summary> /// <summary>

View File

@ -62,6 +62,8 @@ namespace Google.ProtocolBuffers
public abstract MessageDescriptor DescriptorForType { get; } public abstract MessageDescriptor DescriptorForType { get; }
public abstract IDictionary<FieldDescriptor, object> AllFields { get; } public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
public abstract bool HasField(FieldDescriptor field); public abstract bool HasField(FieldDescriptor field);
public abstract bool HasOneof(OneofDescriptor oneof);
public abstract FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof);
public abstract object this[FieldDescriptor field] { get; } public abstract object this[FieldDescriptor field] { get; }
public abstract int GetRepeatedFieldCount(FieldDescriptor field); public abstract int GetRepeatedFieldCount(FieldDescriptor field);
public abstract object this[FieldDescriptor field, int index] { get; } public abstract object this[FieldDescriptor field, int index] { get; }

View File

@ -47,6 +47,7 @@ namespace Google.ProtocolBuffers.Descriptors
private EnumDescriptor enumType; private EnumDescriptor enumType;
private MessageDescriptor messageType; private MessageDescriptor messageType;
private MessageDescriptor containingType; private MessageDescriptor containingType;
private OneofDescriptor containingOneof;
private object defaultValue; private object defaultValue;
private FieldType fieldType; private FieldType fieldType;
private MappedType mappedType; private MappedType mappedType;
@ -94,6 +95,16 @@ namespace Google.ProtocolBuffers.Descriptors
"FieldDescriptorProto.Extendee set for non-extension field."); "FieldDescriptorProto.Extendee set for non-extension field.");
} }
containingType = parent; containingType = parent;
if (proto.HasOneofIndex)
{
if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDeclCount)
{
throw new DescriptorValidationException(this,
"FieldDescriptorProto.oneof_index is out of range for type " + parent.Name);
}
containingOneof = parent.Oneofs[proto.OneofIndex];
containingOneof.fieldCount ++;
}
extensionScope = null; extensionScope = null;
} }
@ -254,6 +265,11 @@ namespace Google.ProtocolBuffers.Descriptors
get { return containingType; } get { return containingType; }
} }
public OneofDescriptor ContainingOneof
{
get { return containingOneof; }
}
/// <summary> /// <summary>
/// For extensions defined nested within message types, gets /// For extensions defined nested within message types, gets
/// the outer type. Not valid for non-extension fields. /// the outer type. Not valid for non-extension fields.

View File

@ -45,6 +45,7 @@ namespace Google.ProtocolBuffers.Descriptors
private readonly IList<EnumDescriptor> enumTypes; private readonly IList<EnumDescriptor> enumTypes;
private readonly IList<FieldDescriptor> fields; private readonly IList<FieldDescriptor> fields;
private readonly IList<FieldDescriptor> extensions; private readonly IList<FieldDescriptor> extensions;
private readonly IList<OneofDescriptor> oneofs;
private bool hasRequiredFields; private bool hasRequiredFields;
internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex) internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex)
@ -52,6 +53,10 @@ namespace Google.ProtocolBuffers.Descriptors
{ {
containingType = parent; containingType = parent;
oneofs = DescriptorUtil.ConvertAndMakeReadOnly(proto.OneofDeclList,
(oneof, index) =>
new OneofDescriptor(oneof, file, this, index));
nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedTypeList, nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedTypeList,
(type, index) => (type, index) =>
new MessageDescriptor(type, file, this, index)); new MessageDescriptor(type, file, this, index));
@ -69,6 +74,19 @@ namespace Google.ProtocolBuffers.Descriptors
(field, index) => (field, index) =>
new FieldDescriptor(field, file, this, index, true)); new FieldDescriptor(field, file, this, index, true));
for (int i = 0; i < proto.OneofDeclCount; i++)
{
oneofs[i].fields = new FieldDescriptor[oneofs[i].FieldCount];
oneofs[i].fieldCount = 0;
}
for (int i = 0; i< proto.FieldCount; i++)
{
OneofDescriptor oneofDescriptor = fields[i].ContainingOneof;
if (oneofDescriptor != null)
{
oneofDescriptor.fields[oneofDescriptor.fieldCount++] = fields[i];
}
}
file.DescriptorPool.AddSymbol(this); file.DescriptorPool.AddSymbol(this);
} }
@ -112,6 +130,11 @@ namespace Google.ProtocolBuffers.Descriptors
get { return enumTypes; } get { return enumTypes; }
} }
public IList<OneofDescriptor> Oneofs
{
get { return oneofs; }
}
/// <summary> /// <summary>
/// Returns a pre-computed result as to whether this message /// Returns a pre-computed result as to whether this message
/// has required fields. This includes optional fields which are /// has required fields. This includes optional fields which are
@ -189,6 +212,11 @@ namespace Google.ProtocolBuffers.Descriptors
{ {
extension.CrossLink(); extension.CrossLink();
} }
foreach (OneofDescriptor oneof in oneofs)
{
// oneof.C
}
} }
internal void CheckRequiredFields() internal void CheckRequiredFields()

View File

@ -0,0 +1,78 @@
// 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
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.ProtocolBuffers.DescriptorProtos;
namespace Google.ProtocolBuffers.Descriptors
{
public sealed class OneofDescriptor
{
private int index;
private OneofDescriptorProto proto;
private FileDescriptor file;
private MessageDescriptor containingType;
internal int fieldCount;
internal IList<FieldDescriptor> fields;
internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file,
MessageDescriptor parent, int index)
{
this.proto = proto;
this.file = file;
this.index = index;
containingType = parent;
fieldCount = 0;
}
public int Index
{
get { return index; }
}
public MessageDescriptor ContainingType
{
get { return containingType; }
}
public int FieldCount
{
get { return fieldCount; }
}
public FieldDescriptor Field(int index)
{
return fields[index];
}
}
}

View File

@ -48,6 +48,7 @@ namespace Google.ProtocolBuffers
{ {
private readonly MessageDescriptor type; private readonly MessageDescriptor type;
private readonly FieldSet fields; private readonly FieldSet fields;
private readonly FieldDescriptor[] oneofCase;
private readonly UnknownFieldSet unknownFields; private readonly UnknownFieldSet unknownFields;
private int memoizedSize = -1; private int memoizedSize = -1;
@ -57,10 +58,12 @@ namespace Google.ProtocolBuffers
/// <param name="type"></param> /// <param name="type"></param>
/// <param name="fields"></param> /// <param name="fields"></param>
/// <param name="unknownFields"></param> /// <param name="unknownFields"></param>
private DynamicMessage(MessageDescriptor type, FieldSet fields, UnknownFieldSet unknownFields) private DynamicMessage(MessageDescriptor type, FieldSet fields,
FieldDescriptor[] oneofCase, UnknownFieldSet unknownFields)
{ {
this.type = type; this.type = type;
this.fields = fields; this.fields = fields;
this.oneofCase = oneofCase;
this.unknownFields = unknownFields; this.unknownFields = unknownFields;
} }
@ -71,7 +74,9 @@ namespace Google.ProtocolBuffers
/// <returns></returns> /// <returns></returns>
public static DynamicMessage GetDefaultInstance(MessageDescriptor type) public static DynamicMessage GetDefaultInstance(MessageDescriptor type)
{ {
return new DynamicMessage(type, FieldSet.DefaultInstance, UnknownFieldSet.DefaultInstance); int oneofDescriptorCount = type.Proto.OneofDeclCount;
FieldDescriptor[] oneofCase = new FieldDescriptor[oneofDescriptorCount];
return new DynamicMessage(type, FieldSet.DefaultInstance, oneofCase, UnknownFieldSet.DefaultInstance);
} }
/// <summary> /// <summary>
@ -201,6 +206,23 @@ namespace Google.ProtocolBuffers
get { return fields.AllFieldDescriptors; } get { return fields.AllFieldDescriptors; }
} }
public override bool HasOneof(OneofDescriptor oneof)
{
VerifyContainingOneofType(oneof);
FieldDescriptor field = oneofCase[oneof.Index];
if (field == null)
{
return false;
}
return true;
}
public override FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof)
{
VerifyContainingOneofType(oneof);
return oneofCase[oneof.Index];
}
public override bool HasField(FieldDescriptor field) public override bool HasField(FieldDescriptor field)
{ {
VerifyContainingType(field); VerifyContainingType(field);
@ -305,6 +327,17 @@ namespace Google.ProtocolBuffers
} }
} }
/// <summary>
/// Verifies that the oneof is an oneof of this message.
/// </summary>
private void VerifyContainingOneofType(OneofDescriptor oneof)
{
if (oneof.ContainingType != type)
{
throw new ArgumentException("OneofDescritpor does not match message type");
}
}
/// <summary> /// <summary>
/// Builder for dynamic messages. Instances are created with DynamicMessage.CreateBuilder. /// Builder for dynamic messages. Instances are created with DynamicMessage.CreateBuilder.
/// </summary> /// </summary>
@ -312,6 +345,7 @@ namespace Google.ProtocolBuffers
{ {
private readonly MessageDescriptor type; private readonly MessageDescriptor type;
private FieldSet fields; private FieldSet fields;
private FieldDescriptor[] oneofCase;
private UnknownFieldSet unknownFields; private UnknownFieldSet unknownFields;
internal Builder(MessageDescriptor type) internal Builder(MessageDescriptor type)
@ -319,6 +353,7 @@ namespace Google.ProtocolBuffers
this.type = type; this.type = type;
this.fields = FieldSet.CreateInstance(); this.fields = FieldSet.CreateInstance();
this.unknownFields = UnknownFieldSet.DefaultInstance; this.unknownFields = UnknownFieldSet.DefaultInstance;
this.oneofCase = new FieldDescriptor[type.Proto.OneofDeclCount];
} }
protected override Builder ThisBuilder protected override Builder ThisBuilder
@ -340,6 +375,23 @@ namespace Google.ProtocolBuffers
} }
fields.MergeFrom(other); fields.MergeFrom(other);
MergeUnknownFields(other.UnknownFields); MergeUnknownFields(other.UnknownFields);
for (int i = 0; i < oneofCase.Length; i++)
{
if (other.HasOneof(type.Oneofs[i]))
{
if (oneofCase[i] == null)
{
oneofCase[i] = other.OneofFieldDescriptor(type.Oneofs[i]);
} else
{
if (oneofCase[i] != other.OneofFieldDescriptor(type.Oneofs[i]))
{
fields.ClearField(oneofCase[i]);
oneofCase[i] = other.OneofFieldDescriptor(type.Oneofs[i]);
}
}
}
}
return this; return this;
} }
@ -353,7 +405,7 @@ namespace Google.ProtocolBuffers
{ {
if (fields != null && !IsInitialized) if (fields != null && !IsInitialized)
{ {
throw new UninitializedMessageException(new DynamicMessage(type, fields, unknownFields)); throw new UninitializedMessageException(new DynamicMessage(type, fields, oneofCase, unknownFields));
} }
return BuildPartial(); return BuildPartial();
} }
@ -367,7 +419,7 @@ namespace Google.ProtocolBuffers
{ {
if (!IsInitialized) if (!IsInitialized)
{ {
throw new UninitializedMessageException(new DynamicMessage(type, fields, unknownFields)). throw new UninitializedMessageException(new DynamicMessage(type, fields, oneofCase, unknownFields)).
AsInvalidProtocolBufferException(); AsInvalidProtocolBufferException();
} }
return BuildPartial(); return BuildPartial();
@ -380,7 +432,7 @@ namespace Google.ProtocolBuffers
throw new InvalidOperationException("Build() has already been called on this Builder."); throw new InvalidOperationException("Build() has already been called on this Builder.");
} }
fields.MakeImmutable(); fields.MakeImmutable();
DynamicMessage result = new DynamicMessage(type, fields, unknownFields); DynamicMessage result = new DynamicMessage(type, fields, oneofCase, unknownFields);
fields = null; fields = null;
unknownFields = null; unknownFields = null;
return result; return result;
@ -390,6 +442,7 @@ namespace Google.ProtocolBuffers
{ {
Builder result = new Builder(type); Builder result = new Builder(type);
result.fields.MergeFrom(fields); result.fields.MergeFrom(fields);
result.oneofCase = oneofCase;
return result; return result;
} }
@ -431,6 +484,23 @@ namespace Google.ProtocolBuffers
return new Builder(field.MessageType); return new Builder(field.MessageType);
} }
public override bool HasOneof(OneofDescriptor oneof)
{
VerifyContainingOneofType(oneof);
FieldDescriptor field = oneofCase[oneof.Index];
if (field == null)
{
return false;
}
return true;
}
public override FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof)
{
VerifyContainingOneofType(oneof);
return oneofCase[oneof.Index];
}
public override bool HasField(FieldDescriptor field) public override bool HasField(FieldDescriptor field)
{ {
VerifyContainingType(field); VerifyContainingType(field);
@ -466,6 +536,17 @@ namespace Google.ProtocolBuffers
set set
{ {
VerifyContainingType(field); VerifyContainingType(field);
OneofDescriptor oneof = field.ContainingOneof;
if (oneof != null)
{
int index = oneof.Index;
FieldDescriptor oldField = oneofCase[index];
if ((oldField != null) && (oldField != field))
{
fields.ClearField(oldField);
}
oneofCase[index] = field;
}
fields[field] = value; fields[field] = value;
} }
} }
@ -473,10 +554,30 @@ namespace Google.ProtocolBuffers
public override Builder ClearField(FieldDescriptor field) public override Builder ClearField(FieldDescriptor field)
{ {
VerifyContainingType(field); VerifyContainingType(field);
OneofDescriptor oneof = field.ContainingOneof;
if (oneof != null)
{
int index = oneof.Index;
if (oneofCase[index] == field)
{
oneofCase[index] = null;
}
}
fields.ClearField(field); fields.ClearField(field);
return this; return this;
} }
public override Builder ClearOneof(OneofDescriptor oneof)
{
VerifyContainingOneofType(oneof);
FieldDescriptor field = oneofCase[oneof.Index];
if (field != null)
{
ClearField(field);
}
return this;
}
public override int GetRepeatedFieldCount(FieldDescriptor field) public override int GetRepeatedFieldCount(FieldDescriptor field)
{ {
VerifyContainingType(field); VerifyContainingType(field);
@ -507,6 +608,17 @@ namespace Google.ProtocolBuffers
throw new ArgumentException("FieldDescriptor does not match message type."); throw new ArgumentException("FieldDescriptor does not match message type.");
} }
} }
/// <summary>
/// Verifies that the oneof is an oneof of this message.
/// </summary>
private void VerifyContainingOneofType(OneofDescriptor oneof)
{
if (oneof.ContainingType != type)
{
throw new ArgumentException("OneofDescriptor does not match message type");
}
}
} }
} }
} }

View File

@ -45,6 +45,7 @@ namespace Google.ProtocolBuffers.FieldAccess
where TBuilder : IBuilder<TMessage, TBuilder> where TBuilder : IBuilder<TMessage, TBuilder>
{ {
private readonly IFieldAccessor<TMessage, TBuilder>[] accessors; private readonly IFieldAccessor<TMessage, TBuilder>[] accessors;
private readonly OneofAccessor<TMessage, TBuilder>[] oneofs;
private readonly MessageDescriptor descriptor; private readonly MessageDescriptor descriptor;
@ -68,17 +69,28 @@ 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];
oneofs = new OneofAccessor<TMessage, TBuilder>[descriptor.Oneofs.Count];
bool supportFieldPresence = descriptor.File.Syntax == FileDescriptor.ProtoSyntax.Proto2; bool supportFieldPresence = descriptor.File.Syntax == FileDescriptor.ProtoSyntax.Proto2;
for (int i = 0; i < accessors.Length; i++) int fieldSize = accessors.Length;
for (int i = 0; i < fieldSize; i++)
{ {
accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence); FieldDescriptor field = descriptor.Fields[i];
string containingOneofName = (field.ContainingOneof != null) ?
propertyNames[fieldSize +field.ContainingOneof.Index] : null;
accessors[i] = CreateAccessor(
field, propertyNames[i], containingOneofName, supportFieldPresence);
}
for (int i = 0; i < oneofs.Length; i++)
{
oneofs[i] = new OneofAccessor<TMessage, TBuilder>(descriptor, propertyNames[i + accessors.Length]);
} }
} }
/// <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, bool supportFieldPresence) private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(
FieldDescriptor field, string name, string containingOneofName, bool supportFieldPresence)
{ {
if (field.IsRepeated) if (field.IsRepeated)
{ {
@ -97,11 +109,24 @@ namespace Google.ProtocolBuffers.FieldAccess
switch (field.MappedType) switch (field.MappedType)
{ {
case MappedType.Message: case MappedType.Message:
return new SingleMessageAccessor<TMessage, TBuilder>(name); {
if (field.ContainingOneof != null)
{
return new SingleMessageAccessor<TMessage, TBuilder>(
field, name, containingOneofName, supportFieldPresence);
}
else
{
return new SingleMessageAccessor<TMessage, TBuilder>(
field, name, containingOneofName, true);
}
}
case MappedType.Enum: case MappedType.Enum:
return new SingleEnumAccessor<TMessage, TBuilder>(field, name, supportFieldPresence); return new SingleEnumAccessor<TMessage, TBuilder>(
field, name, containingOneofName, supportFieldPresence);
default: default:
return new SinglePrimitiveAccessor<TMessage, TBuilder>(field, name, supportFieldPresence); return new SinglePrimitiveAccessor<TMessage, TBuilder>(
field, name, containingOneofName, supportFieldPresence);
} }
} }
} }
@ -123,5 +148,14 @@ namespace Google.ProtocolBuffers.FieldAccess
return accessors[field.Index]; return accessors[field.Index];
} }
} }
internal OneofAccessor<TMessage, TBuilder> Oneof(OneofDescriptor oneof)
{
if (oneof.ContainingType != descriptor)
{
throw new ArgumentException("OneofDescriptor does not match message type");
}
return oneofs[oneof.Index];
}
} }
} }

View File

@ -0,0 +1,91 @@
// 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
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// 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.
using System;
using System.Reflection;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.FieldAccess
{
/// <summary>
/// Access for an oneof
/// </summary>
internal class OneofAccessor<TMessage, TBuilder>
where TMessage : IMessage<TMessage, TBuilder>
where TBuilder : IBuilder<TMessage, TBuilder>
{
private readonly Func<TMessage, object> caseDelegate;
private readonly Func<TBuilder, IBuilder> clearDelegate;
private MessageDescriptor descriptor;
internal OneofAccessor(MessageDescriptor descriptor, string name)
{
this.descriptor = descriptor;
MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
PropertyInfo caseProperty = typeof(TMessage).GetProperty(name + "Case");
if (clearMethod == null || caseProperty == null)
{
throw new ArgumentException("Not all required properties/methods available for oneof");
}
clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod);
caseDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(caseProperty.GetGetMethod());
}
/// <summary>
/// Indicates whether the specified message has set any field in the oneof.
/// </summary>
public bool Has(TMessage message)
{
return ((int) caseDelegate(message) != 0);
}
/// <summary>
/// Clears the oneof in the specified builder.
/// </summary>
public void Clear(TBuilder builder)
{
clearDelegate(builder);
}
/// <summary>
/// Indicates which field in the oneof is set for specified message
/// </summary>
public virtual FieldDescriptor GetOneofFieldDescriptor(TMessage message)
{
int fieldNumber = (int) caseDelegate(message);
if (fieldNumber > 0)
{
return descriptor.FindFieldByNumber(fieldNumber);
}
return null;
}
}
}

View File

@ -42,7 +42,8 @@ namespace Google.ProtocolBuffers.FieldAccess
{ {
private readonly EnumDescriptor enumDescriptor; private readonly EnumDescriptor enumDescriptor;
internal SingleEnumAccessor(FieldDescriptor field, string name, bool supportFieldPresence) : base(field, name, supportFieldPresence) internal SingleEnumAccessor(FieldDescriptor field, string name, string containingOneofName, bool supportFieldPresence)
: base(field, name, containingOneofName, supportFieldPresence)
{ {
enumDescriptor = field.EnumType; enumDescriptor = field.EnumType;
} }

View File

@ -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
{ {
@ -48,7 +49,8 @@ namespace Google.ProtocolBuffers.FieldAccess
/// </summary> /// </summary>
private readonly Func<IBuilder> createBuilderDelegate; private readonly Func<IBuilder> createBuilderDelegate;
internal SingleMessageAccessor(string name) : base(null, name, true) internal SingleMessageAccessor(FieldDescriptor field, string name, string containingOneofName, bool supportFieldPresence)
: base(field, name, containingOneofName, supportFieldPresence)
{ {
MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", ReflectionUtil.EmptyTypes); MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", ReflectionUtil.EmptyTypes);
if (createBuilderMethod == null) if (createBuilderMethod == null)

View File

@ -47,6 +47,7 @@ namespace Google.ProtocolBuffers.FieldAccess
private readonly Action<TBuilder, object> setValueDelegate; private readonly Action<TBuilder, object> setValueDelegate;
private readonly Func<TMessage, bool> hasDelegate; private readonly Func<TMessage, bool> hasDelegate;
private readonly Func<TBuilder, IBuilder> clearDelegate; private readonly Func<TBuilder, IBuilder> clearDelegate;
private readonly Func<TMessage, object> caseDelegate;
/// <summary> /// <summary>
/// The CLR type of the field (int, the enum type, ByteString, the message etc). /// The CLR type of the field (int, the enum type, ByteString, the message etc).
@ -57,7 +58,8 @@ namespace Google.ProtocolBuffers.FieldAccess
get { return clrType; } get { return clrType; }
} }
internal SinglePrimitiveAccessor(FieldDescriptor fieldDescriptor, string name, bool supportFieldPresence) internal SinglePrimitiveAccessor(
FieldDescriptor fieldDescriptor, string name, string containingOneofName, 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);
@ -76,9 +78,18 @@ namespace Google.ProtocolBuffers.FieldAccess
} }
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod()); hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
} else } else
{
if (fieldDescriptor.ContainingOneof != null)
{
PropertyInfo caseProperty = typeof(TMessage).GetProperty(containingOneofName + "Case");
caseDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(caseProperty.GetGetMethod());
hasDelegate = message => OneofFieldNumber(message).Equals(fieldDescriptor.FieldNumber);
}
else
{ {
hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue); hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue);
} }
}
clrType = messageProperty.PropertyType; clrType = messageProperty.PropertyType;
clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod); clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod);
@ -86,6 +97,11 @@ namespace Google.ProtocolBuffers.FieldAccess
setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod()); setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
} }
private int OneofFieldNumber(TMessage message)
{
return (int) caseDelegate(message);
}
public bool Has(TMessage message) public bool Has(TMessage message)
{ {
return hasDelegate(message); return hasDelegate(message);

View File

@ -105,6 +105,16 @@ namespace Google.ProtocolBuffers
set { InternalFieldAccessors[field].SetRepeated(ThisBuilder, index, value); } set { InternalFieldAccessors[field].SetRepeated(ThisBuilder, index, value); }
} }
public override bool HasOneof(OneofDescriptor oneof)
{
return MessageBeingBuilt.HasOneof(oneof);
}
public override FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof)
{
return MessageBeingBuilt.OneofFieldDescriptor(oneof);
}
public override bool HasField(FieldDescriptor field) public override bool HasField(FieldDescriptor field)
{ {
return MessageBeingBuilt.HasField(field); return MessageBeingBuilt.HasField(field);
@ -121,6 +131,12 @@ namespace Google.ProtocolBuffers
return ThisBuilder; return ThisBuilder;
} }
public override TBuilder ClearOneof(OneofDescriptor oneof)
{
InternalFieldAccessors.Oneof(oneof).Clear(ThisBuilder);
return ThisBuilder;
}
public override TBuilder MergeFrom(TMessage other) public override TBuilder MergeFrom(TMessage other)
{ {
if (other.DescriptorForType != InternalFieldAccessors.Descriptor) if (other.DescriptorForType != InternalFieldAccessors.Descriptor)

View File

@ -142,6 +142,16 @@ namespace Google.ProtocolBuffers
get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); } get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
} }
public override bool HasOneof(OneofDescriptor oneof)
{
return InternalFieldAccessors.Oneof(oneof).Has(ThisMessage);
}
public override FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof)
{
return InternalFieldAccessors.Oneof(oneof).GetOneofFieldDescriptor(ThisMessage);
}
public override bool HasField(FieldDescriptor field) public override bool HasField(FieldDescriptor field)
{ {
return InternalFieldAccessors[field].Has(ThisMessage); return InternalFieldAccessors[field].Has(ThisMessage);

View File

@ -103,6 +103,11 @@ namespace Google.ProtocolBuffers
/// </summary> /// </summary>
object this[FieldDescriptor field, int index] { get; set; } object this[FieldDescriptor field, int index] { get; set; }
bool HasOneof(OneofDescriptor oneof);
FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof);
/// <summary> /// <summary>
/// <see cref="IMessage{TMessage, TBuilder}.HasField"/> /// <see cref="IMessage{TMessage, TBuilder}.HasField"/>
/// </summary> /// </summary>
@ -125,6 +130,7 @@ namespace Google.ProtocolBuffers
IBuilder WeakAddRepeatedField(FieldDescriptor field, object value); IBuilder WeakAddRepeatedField(FieldDescriptor field, object value);
new IBuilder WeakClear(); new IBuilder WeakClear();
IBuilder WeakClearField(FieldDescriptor field); IBuilder WeakClearField(FieldDescriptor field);
IBuilder WeakClearOneof(OneofDescriptor oneof);
IBuilder WeakMergeFrom(IMessage message); IBuilder WeakMergeFrom(IMessage message);
new IBuilder WeakMergeFrom(ByteString data); new IBuilder WeakMergeFrom(ByteString data);
new IBuilder WeakMergeFrom(ByteString data, ExtensionRegistry registry); new IBuilder WeakMergeFrom(ByteString data, ExtensionRegistry registry);
@ -227,6 +233,14 @@ namespace Google.ProtocolBuffers
/// <returns></returns> /// <returns></returns>
TBuilder ClearField(FieldDescriptor field); TBuilder ClearField(FieldDescriptor field);
/// <summary>
/// Clears the oneof. This is exactly equivalent to calling the generated
/// Clear method corresponding to the oneof.
/// </summary>
/// <param name="oneof"></param>
/// <returns></returns>
TBuilder ClearOneof(OneofDescriptor oneof);
/// <summary> /// <summary>
/// Appends the given value as a new element for the specified repeated field. /// Appends the given value as a new element for the specified repeated field.
/// </summary> /// </summary>

View File

@ -67,6 +67,10 @@ namespace Google.ProtocolBuffers
/// </summary> /// </summary>
IDictionary<FieldDescriptor, object> AllFields { get; } IDictionary<FieldDescriptor, object> AllFields { get; }
bool HasOneof(OneofDescriptor oneof);
FieldDescriptor OneofFieldDescriptor(OneofDescriptor oneof);
/// <summary> /// <summary>
/// Returns true if the given field is set. This is exactly equivalent /// Returns true if the given field is set. This is exactly equivalent
/// to calling the generated "Has" property corresponding to the field. /// to calling the generated "Has" property corresponding to the field.

View File

@ -81,6 +81,7 @@
<Compile Include="Descriptors\FieldMappingAttribute.cs" /> <Compile Include="Descriptors\FieldMappingAttribute.cs" />
<Compile Include="Descriptors\FieldType.cs" /> <Compile Include="Descriptors\FieldType.cs" />
<Compile Include="Descriptors\FileDescriptor.cs" /> <Compile Include="Descriptors\FileDescriptor.cs" />
<Compile Include="Descriptors\OneofDescriptor.cs" />
<Compile Include="Descriptors\IDescriptor.cs" /> <Compile Include="Descriptors\IDescriptor.cs" />
<Compile Include="Descriptors\IndexedDescriptorBase.cs" /> <Compile Include="Descriptors\IndexedDescriptorBase.cs" />
<Compile Include="Descriptors\MappedType.cs" /> <Compile Include="Descriptors\MappedType.cs" />
@ -106,6 +107,7 @@
<Compile Include="FieldAccess\IFieldAccessor.cs" /> <Compile Include="FieldAccess\IFieldAccessor.cs" />
<Compile Include="FieldAccess\FieldAccessorTable.cs" /> <Compile Include="FieldAccess\FieldAccessorTable.cs" />
<Compile Include="FieldAccess\RepeatedMessageAccessor.cs" /> <Compile Include="FieldAccess\RepeatedMessageAccessor.cs" />
<Compile Include="FieldAccess\OneofAccessor.cs" />
<Compile Include="FieldSet.cs" /> <Compile Include="FieldSet.cs" />
<Compile Include="FrameworkPortability.cs" /> <Compile Include="FrameworkPortability.cs" />
<Compile Include="GeneratedBuilder.cs" /> <Compile Include="GeneratedBuilder.cs" />

File diff suppressed because it is too large Load Diff

View File

@ -1971,6 +1971,19 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
#endregion #endregion
private object oneofField_;
public enum OneofFieldOneofCase {
OneofUint32 = 111,
OneofNestedMessage = 112,
OneofString = 113,
OneofBytes = 114,
None = 0,
}
private OneofFieldOneofCase oneofFieldCase_ = OneofFieldOneofCase.None;
public OneofFieldOneofCase OneofFieldCase {
get { return oneofFieldCase_; }
}
public const int OptionalInt32FieldNumber = 1; public const int OptionalInt32FieldNumber = 1;
private bool hasOptionalInt32; private bool hasOptionalInt32;
private int optionalInt32_; private int optionalInt32_;
@ -2732,43 +2745,35 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public const int OneofUint32FieldNumber = 111; public const int OneofUint32FieldNumber = 111;
private bool hasOneofUint32;
private uint oneofUint32_;
public bool HasOneofUint32 { public bool HasOneofUint32 {
get { return hasOneofUint32; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofUint32; }
} }
public uint OneofUint32 { public uint OneofUint32 {
get { return oneofUint32_; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofUint32 ? (uint) oneofField_ : 0; }
} }
public const int OneofNestedMessageFieldNumber = 112; public const int OneofNestedMessageFieldNumber = 112;
private bool hasOneofNestedMessage;
private global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage oneofNestedMessage_;
public bool HasOneofNestedMessage { public bool HasOneofNestedMessage {
get { return hasOneofNestedMessage; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage OneofNestedMessage {
get { return oneofNestedMessage_ ?? global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage) oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance; }
} }
public const int OneofStringFieldNumber = 113; public const int OneofStringFieldNumber = 113;
private bool hasOneofString;
private string oneofString_ = "";
public bool HasOneofString { public bool HasOneofString {
get { return hasOneofString; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofString; }
} }
public string OneofString { public string OneofString {
get { return oneofString_; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofString ? (string) oneofField_ : ""; }
} }
public const int OneofBytesFieldNumber = 114; public const int OneofBytesFieldNumber = 114;
private bool hasOneofBytes;
private pb::ByteString oneofBytes_ = pb::ByteString.Empty;
public bool HasOneofBytes { public bool HasOneofBytes {
get { return hasOneofBytes; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofBytes; }
} }
public pb::ByteString OneofBytes { public pb::ByteString OneofBytes {
get { return oneofBytes_; } get { return oneofFieldCase_ == OneofFieldOneofCase.OneofBytes ? (pb::ByteString) oneofField_ : pb::ByteString.Empty; }
} }
public override bool IsInitialized { public override bool IsInitialized {
@ -2993,16 +2998,16 @@ namespace Google.ProtocolBuffers.TestProtos {
if (hasDefaultCord) { if (hasDefaultCord) {
output.WriteString(85, field_names[2], DefaultCord); output.WriteString(85, field_names[2], DefaultCord);
} }
if (hasOneofUint32) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofUint32) {
output.WriteUInt32(111, field_names[23], OneofUint32); output.WriteUInt32(111, field_names[23], OneofUint32);
} }
if (hasOneofNestedMessage) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
output.WriteMessage(112, field_names[21], OneofNestedMessage); output.WriteMessage(112, field_names[21], OneofNestedMessage);
} }
if (hasOneofString) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofString) {
output.WriteString(113, field_names[22], OneofString); output.WriteString(113, field_names[22], OneofString);
} }
if (hasOneofBytes) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofBytes) {
output.WriteBytes(114, field_names[20], OneofBytes); output.WriteBytes(114, field_names[20], OneofBytes);
} }
} }
@ -3326,16 +3331,16 @@ namespace Google.ProtocolBuffers.TestProtos {
if (hasDefaultCord) { if (hasDefaultCord) {
size += pb::CodedOutputStream.ComputeStringSize(85, DefaultCord); size += pb::CodedOutputStream.ComputeStringSize(85, DefaultCord);
} }
if (hasOneofUint32) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofUint32) {
size += pb::CodedOutputStream.ComputeUInt32Size(111, OneofUint32); size += pb::CodedOutputStream.ComputeUInt32Size(111, OneofUint32);
} }
if (hasOneofNestedMessage) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
size += pb::CodedOutputStream.ComputeMessageSize(112, OneofNestedMessage); size += pb::CodedOutputStream.ComputeMessageSize(112, OneofNestedMessage);
} }
if (hasOneofString) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofString) {
size += pb::CodedOutputStream.ComputeStringSize(113, OneofString); size += pb::CodedOutputStream.ComputeStringSize(113, OneofString);
} }
if (hasOneofBytes) { if (oneofFieldCase_ == OneofFieldOneofCase.OneofBytes) {
size += pb::CodedOutputStream.ComputeBytesSize(114, OneofBytes); size += pb::CodedOutputStream.ComputeBytesSize(114, OneofBytes);
} }
memoizedSerializedSize = size; memoizedSerializedSize = size;
@ -3520,15 +3525,8 @@ namespace Google.ProtocolBuffers.TestProtos {
if (hasDefaultCord) { if (hasDefaultCord) {
hash ^= defaultCord_.GetHashCode(); hash ^= defaultCord_.GetHashCode();
} }
if (hasOneofUint32) { if (oneofFieldCase_ != OneofFieldOneofCase.None) {
hash ^= oneofUint32_.GetHashCode(); hash ^= oneofField_.GetHashCode();
}
if (hasOneofNestedMessage) hash ^= oneofNestedMessage_.GetHashCode();
if (hasOneofString) {
hash ^= oneofString_.GetHashCode();
}
if (hasOneofBytes) {
hash ^= oneofBytes_.GetHashCode();
} }
return hash; return hash;
} }
@ -3657,10 +3655,10 @@ namespace Google.ProtocolBuffers.TestProtos {
if (hasDefaultImportEnum != other.hasDefaultImportEnum || (hasDefaultImportEnum && !defaultImportEnum_.Equals(other.defaultImportEnum_))) return false; if (hasDefaultImportEnum != other.hasDefaultImportEnum || (hasDefaultImportEnum && !defaultImportEnum_.Equals(other.defaultImportEnum_))) return false;
if (hasDefaultStringPiece != other.hasDefaultStringPiece || (hasDefaultStringPiece && !defaultStringPiece_.Equals(other.defaultStringPiece_))) return false; if (hasDefaultStringPiece != other.hasDefaultStringPiece || (hasDefaultStringPiece && !defaultStringPiece_.Equals(other.defaultStringPiece_))) return false;
if (hasDefaultCord != other.hasDefaultCord || (hasDefaultCord && !defaultCord_.Equals(other.defaultCord_))) return false; if (hasDefaultCord != other.hasDefaultCord || (hasDefaultCord && !defaultCord_.Equals(other.defaultCord_))) return false;
if (hasOneofUint32 != other.hasOneofUint32 || (hasOneofUint32 && !oneofUint32_.Equals(other.oneofUint32_))) return false; if (!OneofUint32.Equals(other.OneofUint32)) return false;
if (hasOneofNestedMessage != other.hasOneofNestedMessage || (hasOneofNestedMessage && !oneofNestedMessage_.Equals(other.oneofNestedMessage_))) return false; if (!OneofNestedMessage.Equals(other.OneofNestedMessage)) return false;
if (hasOneofString != other.hasOneofString || (hasOneofString && !oneofString_.Equals(other.oneofString_))) return false; if (!OneofString.Equals(other.OneofString)) return false;
if (hasOneofBytes != other.hasOneofBytes || (hasOneofBytes && !oneofBytes_.Equals(other.oneofBytes_))) return false; if (!OneofBytes.Equals(other.OneofBytes)) return false;
return true; return true;
} }
@ -3736,10 +3734,10 @@ namespace Google.ProtocolBuffers.TestProtos {
PrintField("default_import_enum", hasDefaultImportEnum, defaultImportEnum_, writer); PrintField("default_import_enum", hasDefaultImportEnum, defaultImportEnum_, writer);
PrintField("default_string_piece", hasDefaultStringPiece, defaultStringPiece_, writer); PrintField("default_string_piece", hasDefaultStringPiece, defaultStringPiece_, writer);
PrintField("default_cord", hasDefaultCord, defaultCord_, writer); PrintField("default_cord", hasDefaultCord, defaultCord_, writer);
PrintField("oneof_uint32", hasOneofUint32, oneofUint32_, writer); PrintField("oneof_uint32", oneofFieldCase_ == OneofFieldOneofCase.OneofUint32, oneofField_, writer);
PrintField("oneof_nested_message", hasOneofNestedMessage, oneofNestedMessage_, writer); PrintField("oneof_nested_message", oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage, oneofField_, writer);
PrintField("oneof_string", hasOneofString, oneofString_, writer); PrintField("oneof_string", oneofFieldCase_ == OneofFieldOneofCase.OneofString, oneofField_, writer);
PrintField("oneof_bytes", hasOneofBytes, oneofBytes_, writer); PrintField("oneof_bytes", oneofFieldCase_ == OneofFieldOneofCase.OneofBytes, oneofField_, writer);
} }
#endregion #endregion
@ -4095,17 +4093,24 @@ namespace Google.ProtocolBuffers.TestProtos {
if (other.HasDefaultCord) { if (other.HasDefaultCord) {
DefaultCord = other.DefaultCord; DefaultCord = other.DefaultCord;
} }
if (other.HasOneofUint32) { switch (other.OneofFieldCase) {
OneofUint32 = other.OneofUint32; case OneofFieldOneofCase.OneofUint32: {
SetOneofUint32(other.OneofUint32);
break;
} }
if (other.HasOneofNestedMessage) { case OneofFieldOneofCase.OneofNestedMessage: {
MergeOneofNestedMessage(other.OneofNestedMessage); MergeOneofNestedMessage(other.OneofNestedMessage);
break;
} }
if (other.HasOneofString) { case OneofFieldOneofCase.OneofString: {
OneofString = other.OneofString; SetOneofString(other.OneofString);
break;
} }
if (other.HasOneofBytes) { case OneofFieldOneofCase.OneofBytes: {
OneofBytes = other.OneofBytes; SetOneofBytes(other.OneofBytes);
break;
}
case OneofFieldOneofCase.None: { break; }
} }
return this; return this;
} }
@ -4497,24 +4502,37 @@ namespace Google.ProtocolBuffers.TestProtos {
break; break;
} }
case 888: { case 888: {
result.hasOneofUint32 = input.ReadUInt32(ref result.oneofUint32_); uint value = 0;
if (input.ReadUInt32(ref value)) {
result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofUint32;
}
break; break;
} }
case 898: { case 898: {
global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.Builder subBuilder = global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.CreateBuilder(); global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.Builder subBuilder = global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.CreateBuilder();
if (result.hasOneofNestedMessage) { if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
subBuilder.MergeFrom(OneofNestedMessage); subBuilder.MergeFrom(OneofNestedMessage);
} }
input.ReadMessage(subBuilder, extensionRegistry); input.ReadMessage(subBuilder, extensionRegistry);
OneofNestedMessage = subBuilder.BuildPartial(); result.oneofField_ = subBuilder.BuildPartial();
result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
break; break;
} }
case 906: { case 906: {
result.hasOneofString = input.ReadString(ref result.oneofString_); string value = "";
if (input.ReadString(ref value)) {
result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofString;
}
break; break;
} }
case 914: { case 914: {
result.hasOneofBytes = input.ReadBytes(ref result.oneofBytes_); pb::ByteString value = pb::ByteString.Empty;
if (input.ReadBytes(ref value)) {
result.oneofField_ = value;
result.oneofFieldCase_ = OneofFieldOneofCase.OneofBytes;
}
break; break;
} }
} }
@ -6401,104 +6419,119 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasOneofUint32 { public bool HasOneofUint32 {
get { return result.hasOneofUint32; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofUint32; }
} }
public uint OneofUint32 { public uint OneofUint32 {
get { return result.OneofUint32; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofUint32 ? (uint) result.oneofField_ : 0; }
set { SetOneofUint32(value); } set { SetOneofUint32(value); }
} }
public Builder SetOneofUint32(uint value) { public Builder SetOneofUint32(uint value) {
PrepareBuilder(); PrepareBuilder();
result.hasOneofUint32 = true; result.oneofField_ = value;
result.oneofUint32_ = value; result.oneofFieldCase_ = OneofFieldOneofCase.OneofUint32;
return this; return this;
} }
public Builder ClearOneofUint32() { public Builder ClearOneofUint32() {
PrepareBuilder(); PrepareBuilder();
result.hasOneofUint32 = false; if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofUint32) {
result.oneofUint32_ = 0; result.oneofFieldCase_ = OneofFieldOneofCase.None;
}
return this; return this;
} }
public bool HasOneofNestedMessage { public bool HasOneofNestedMessage {
get { return result.hasOneofNestedMessage; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage OneofNestedMessage {
get { return result.OneofNestedMessage; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance; }
set { SetOneofNestedMessage(value); } set { SetOneofNestedMessage(value); }
} }
public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage value) { public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
result.hasOneofNestedMessage = true; result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
result.oneofNestedMessage_ = value; result.oneofField_ = value;
return this; return this;
} }
public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.Builder builderForValue) { public Builder SetOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.Builder builderForValue) {
pb::ThrowHelper.ThrowIfNull(builderForValue, "builderForValue"); pb::ThrowHelper.ThrowIfNull(builderForValue, "builderForValue");
PrepareBuilder(); PrepareBuilder();
result.hasOneofNestedMessage = true; result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
result.oneofNestedMessage_ = builderForValue.Build(); result.oneofField_ = builderForValue.Build();
return this; return this;
} }
public Builder MergeOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage value) { public Builder MergeOneofNestedMessage(global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
if (result.hasOneofNestedMessage && if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage &&
result.oneofNestedMessage_ != global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance) { result.OneofNestedMessage != global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance) {
result.oneofNestedMessage_ = global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.CreateBuilder(result.oneofNestedMessage_).MergeFrom(value).BuildPartial(); result.oneofField_ = global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.CreateBuilder(result.OneofNestedMessage).MergeFrom(value).BuildPartial();
} else { } else {
result.oneofNestedMessage_ = value; result.oneofField_ = value;
} }
result.hasOneofNestedMessage = true; result.oneofFieldCase_ = OneofFieldOneofCase.OneofNestedMessage;
return this; return this;
} }
public Builder ClearOneofNestedMessage() { public Builder ClearOneofNestedMessage() {
if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage) {
PrepareBuilder(); PrepareBuilder();
result.hasOneofNestedMessage = false; result.oneofFieldCase_ = OneofFieldOneofCase.None;
result.oneofNestedMessage_ = null; result.oneofField_ = null;
}
return this; return this;
} }
public bool HasOneofString { public bool HasOneofString {
get { return result.hasOneofString; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofString; }
} }
public string OneofString { public string OneofString {
get { return result.OneofString; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofString ? (string) result.oneofField_ : ""; }
set { SetOneofString(value); } set { SetOneofString(value); }
} }
public Builder SetOneofString(string value) { public Builder SetOneofString(string value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
result.hasOneofString = true; result.oneofField_ = value;
result.oneofString_ = value; result.oneofFieldCase_ = OneofFieldOneofCase.OneofString;
return this; return this;
} }
public Builder ClearOneofString() { public Builder ClearOneofString() {
PrepareBuilder(); PrepareBuilder();
result.hasOneofString = false; if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofString) {
result.oneofString_ = ""; result.oneofFieldCase_ = OneofFieldOneofCase.None;
}
return this; return this;
} }
public bool HasOneofBytes { public bool HasOneofBytes {
get { return result.hasOneofBytes; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofBytes; }
} }
public pb::ByteString OneofBytes { public pb::ByteString OneofBytes {
get { return result.OneofBytes; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofBytes ? (pb::ByteString) result.oneofField_ : pb::ByteString.Empty; }
set { SetOneofBytes(value); } set { SetOneofBytes(value); }
} }
public Builder SetOneofBytes(pb::ByteString value) { public Builder SetOneofBytes(pb::ByteString value) {
pb::ThrowHelper.ThrowIfNull(value, "value"); pb::ThrowHelper.ThrowIfNull(value, "value");
PrepareBuilder(); PrepareBuilder();
result.hasOneofBytes = true; result.oneofField_ = value;
result.oneofBytes_ = value; result.oneofFieldCase_ = OneofFieldOneofCase.OneofBytes;
return this; return this;
} }
public Builder ClearOneofBytes() { public Builder ClearOneofBytes() {
PrepareBuilder(); PrepareBuilder();
result.hasOneofBytes = false; if (result.oneofFieldCase_ == OneofFieldOneofCase.OneofBytes) {
result.oneofBytes_ = pb::ByteString.Empty; result.oneofFieldCase_ = OneofFieldOneofCase.None;
}
return this;
}
public OneofFieldOneofCase OneofFieldCase {
get { return result.oneofFieldCase_; }
}
public Builder ClearOneofField() {
PrepareBuilder();
result.oneofField_ = null;
result.oneofFieldCase_ = OneofFieldOneofCase.None;
return this; return this;
} }
} }

View File

@ -49,6 +49,11 @@ namespace csharp {
EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor, EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor,
int fieldOrdinal) int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
if (SupportFieldPresence(descriptor_->file())) {
has_property_check = "has" + property_name();
} else {
has_property_check = property_name() + " != " + default_value();
}
} }
EnumFieldGenerator::~EnumFieldGenerator() { EnumFieldGenerator::~EnumFieldGenerator() {
@ -122,8 +127,7 @@ void EnumFieldGenerator::GenerateBuildingCode(Writer* writer) {
void EnumFieldGenerator::GenerateParsingCode(Writer* writer) { 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())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name()); writer->WriteLine(" result.has$0$ = true;", property_name());
} }
@ -141,11 +145,7 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
} }
void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) { void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("if ($0$) {", has_property_check);
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());
@ -153,11 +153,7 @@ void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
} }
void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("if ($0$) {", has_property_check);
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());
@ -165,11 +161,7 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
} }
void EnumFieldGenerator::WriteHash(Writer* writer) { void EnumFieldGenerator::WriteHash(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("if ($0$) {", has_property_check);
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(" hash ^= $0$_.GetHashCode();", name());
writer->WriteLine("}"); writer->WriteLine("}");
} }
@ -184,13 +176,94 @@ void EnumFieldGenerator::WriteEquals(Writer* writer) {
} }
} }
void EnumFieldGenerator::WriteToString(Writer* writer) { void EnumFieldGenerator::WriteToString(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);",
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);", descriptor_->name(), has_property_check, name());
descriptor_->name(), property_name(), name());
} else {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);",
descriptor_->name(), name());
} }
EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descriptor,
int fieldOrdinal)
: EnumFieldGenerator(descriptor, fieldOrdinal) {
has_property_check = oneof_name() + "Case_ == " + oneof_property_name() +
"OneofCase." + property_name();
}
EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {
}
void EnumOneofFieldGenerator::GenerateMembers(Writer* writer) {
AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return $0$; }", has_property_check);
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$ ? ($1$) $2$_ : $3$; }",
has_property_check, type_name(), oneof_name(), default_value());
writer->WriteLine("}");
}
void EnumOneofFieldGenerator::GenerateBuilderMembers(Writer* writer) {
AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.$0$; }", has_property_check);
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$ ? ($1$) result.$2$_ : $3$; }",
has_property_check, type_name(), oneof_name(), default_value());
writer->WriteLine(" set { Set$0$(value); }", property_name());
writer->WriteLine("}");
AddPublicMemberAttributes(writer);
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(),
type_name());
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.$0$_ = value;", oneof_name());
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" if (result.$0$) {", has_property_check);
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;",
oneof_name(), oneof_property_name());
writer->WriteLine(" }");
writer->WriteLine(" return this;");
writer->WriteLine("}");
}
void EnumOneofFieldGenerator::WriteEquals(Writer* writer) {
writer->WriteLine("if (!$0$.Equals(other.$0$)) return false;", property_name());
}
void EnumOneofFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);",
descriptor_->name(), has_property_check, oneof_name());
}
void EnumOneofFieldGenerator::GenerateParsingCode(Writer* writer) {
writer->WriteLine("object unknown;");
writer->WriteLine("$0$ enumValue = $1$;", type_name(), default_value());
writer->WriteLine("if(input.ReadEnum(ref enumValue, out unknown)) {",
name());
writer->WriteLine(" result.$0$_ = enumValue;", oneof_name());
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine("} else if(unknown is int) {");
if (!use_lite_runtime()) {
writer->WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now
writer->WriteLine(
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
writer->WriteLine(" }");
writer->WriteLine(
" unknownFields.MergeVarintField($0$, (ulong)(int)unknown);",
number());
}
writer->WriteLine("}");
} }
} // namespace csharp } // namespace csharp

View File

@ -60,10 +60,28 @@ class EnumFieldGenerator : public FieldGeneratorBase {
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(Writer* writer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(Writer* writer);
protected:
string has_property_check;
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator);
}; };
class EnumOneofFieldGenerator : public EnumFieldGenerator {
public:
EnumOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~EnumOneofFieldGenerator();
virtual void GenerateMembers(Writer* writer);
virtual void GenerateBuilderMembers(Writer* writer);
virtual void WriteEquals(Writer* writer);
virtual void WriteToString(Writer* writer);
virtual void GenerateParsingCode(Writer* writer);
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumOneofFieldGenerator);
};
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf

View File

@ -81,6 +81,14 @@ void FieldGeneratorBase::AddPublicMemberAttributes(Writer* writer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(writer);
} }
std::string FieldGeneratorBase::oneof_property_name() {
return UnderscoresToCamelCase(descriptor_->containing_oneof()->name(), true);
}
std::string FieldGeneratorBase::oneof_name() {
return UnderscoresToCamelCase(descriptor_->containing_oneof()->name(), false);
}
std::string FieldGeneratorBase::property_name() { std::string FieldGeneratorBase::property_name() {
return GetPropertyName(descriptor_); return GetPropertyName(descriptor_);
} }

View File

@ -71,6 +71,8 @@ class FieldGeneratorBase : public SourceGeneratorBase {
void AddPublicMemberAttributes(Writer* writer); void AddPublicMemberAttributes(Writer* writer);
std::string oneof_property_name();
std::string oneof_name();
std::string property_name(); std::string property_name();
std::string name(); std::string name();
std::string type_name(); std::string type_name();

View File

@ -345,23 +345,35 @@ FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
case FieldDescriptor::TYPE_MESSAGE: case FieldDescriptor::TYPE_MESSAGE:
if (descriptor->is_repeated()) { if (descriptor->is_repeated()) {
return new RepeatedMessageFieldGenerator(descriptor, fieldOrdinal); return new RepeatedMessageFieldGenerator(descriptor, fieldOrdinal);
} else {
if (descriptor->containing_oneof()) {
return new MessageOneofFieldGenerator(descriptor, fieldOrdinal);
} else { } else {
return new MessageFieldGenerator(descriptor, fieldOrdinal); return new MessageFieldGenerator(descriptor, fieldOrdinal);
} }
}
case FieldDescriptor::TYPE_ENUM: case FieldDescriptor::TYPE_ENUM:
if (descriptor->is_repeated()) { if (descriptor->is_repeated()) {
return new RepeatedEnumFieldGenerator(descriptor, fieldOrdinal); return new RepeatedEnumFieldGenerator(descriptor, fieldOrdinal);
} else {
if (descriptor->containing_oneof()) {
return new EnumOneofFieldGenerator(descriptor, fieldOrdinal);
} else { } else {
return new EnumFieldGenerator(descriptor, fieldOrdinal); return new EnumFieldGenerator(descriptor, fieldOrdinal);
} }
}
default: default:
if (descriptor->is_repeated()) { if (descriptor->is_repeated()) {
return new RepeatedPrimitiveFieldGenerator(descriptor, fieldOrdinal); return new RepeatedPrimitiveFieldGenerator(descriptor, fieldOrdinal);
} else {
if (descriptor->containing_oneof()) {
return new PrimitiveOneofFieldGenerator(descriptor, fieldOrdinal);
} else { } else {
return new PrimitiveFieldGenerator(descriptor, fieldOrdinal); return new PrimitiveFieldGenerator(descriptor, fieldOrdinal);
} }
} }
} }
}
bool HasRequiredFields(const Descriptor* descriptor, std::set<const Descriptor*>* already_seen) { bool HasRequiredFields(const Descriptor* descriptor, std::set<const Descriptor*>* already_seen) {
if (already_seen->find(descriptor) != already_seen->end()) { if (already_seen->find(descriptor) != already_seen->end()) {

View File

@ -156,6 +156,10 @@ void MessageGenerator::GenerateStaticVariableInitializers(Writer* writer) {
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
writer->Write("\"$0$\", ", GetPropertyName(descriptor_->field(i))); writer->Write("\"$0$\", ", GetPropertyName(descriptor_->field(i)));
} }
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
writer->Write("\"$0$\", ",
UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true));
}
writer->WriteLine("});"); writer->WriteLine("});");
} }
@ -260,6 +264,31 @@ void MessageGenerator::Generate(Writer* writer) {
writer->WriteLine(); writer->WriteLine();
} }
// oneof
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
string name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false);
string property_name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true);
writer->WriteLine("private object $0$_;", name);
writer->WriteLine("public enum $0$OneofCase {", property_name);
writer->Indent();
for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
writer->WriteLine("$0$ = $1$,",
GetPropertyName(field),
SimpleItoa(field->number()));
}
writer->WriteLine("None = 0,");
writer->Outdent();
writer->WriteLine("}");
writer->WriteLine("private $0$OneofCase $1$Case_ = $0$OneofCase.None;",
property_name, name);
writer->WriteLine("public $0$OneofCase $0$Case {", property_name);
writer->WriteLine(" get { return $0$Case_; }", name);
writer->WriteLine("}");
writer->WriteLine();
}
// Fields
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* fieldDescriptor = descriptor_->field(i); const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
@ -310,10 +339,20 @@ void MessageGenerator::GenerateLiteRuntimeMethods(Writer* writer) {
writer->Indent(); writer->Indent();
writer->WriteLine("int hash = GetType().GetHashCode();"); writer->WriteLine("int hash = GetType().GetHashCode();");
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
if (field->containing_oneof() == NULL) {
scoped_ptr<FieldGeneratorBase> generator( scoped_ptr<FieldGeneratorBase> generator(
CreateFieldGeneratorInternal(descriptor_->field(i))); CreateFieldGeneratorInternal(field));
generator->WriteHash(writer); generator->WriteHash(writer);
} }
}
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
string name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false);
string property_name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true);
writer->WriteLine("if ($0$Case_ != $1$OneofCase.None) {", name, property_name);
writer->WriteLine(" hash ^= $0$_.GetHashCode();", name);
writer->WriteLine("}");
}
if (callbase) { if (callbase) {
writer->WriteLine("hash ^= base.GetHashCode();"); writer->WriteLine("hash ^= base.GetHashCode();");
} }
@ -577,6 +616,23 @@ void MessageGenerator::GenerateBuilder(Writer* writer) {
// No field comment :( // No field comment :(
generator->GenerateBuilderMembers(writer); generator->GenerateBuilderMembers(writer);
} }
// oneof
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
writer->WriteLine();
string name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false);
string property_name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true);
writer->WriteLine("public $0$OneofCase $0$Case {", property_name);
writer->WriteLine(" get { return result.$0$Case_; }", name);
writer->WriteLine("}");
writer->WriteLine("public Builder Clear$0$() {", property_name);
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.$0$_ = null;", name);
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;", name, property_name);
writer->WriteLine(" return this;");
writer->WriteLine("}");
}
writer->Outdent(); writer->Outdent();
writer->WriteLine("}"); writer->WriteLine("}");
} }
@ -675,10 +731,37 @@ void MessageGenerator::GenerateCommonBuilderMethods(Writer* writer) {
full_class_name()); full_class_name());
writer->WriteLine("PrepareBuilder();"); writer->WriteLine("PrepareBuilder();");
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
if (!descriptor_->field(i)->containing_oneof()) {
scoped_ptr<FieldGeneratorBase> generator( scoped_ptr<FieldGeneratorBase> generator(
CreateFieldGeneratorInternal(descriptor_->field(i))); CreateFieldGeneratorInternal(descriptor_->field(i)));
generator->GenerateMergingCode(writer); generator->GenerateMergingCode(writer);
} }
}
// Merge oneof fields
for (int i = 0; i < descriptor_->oneof_decl_count(); ++i) {
string name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false);
string property_name = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true);
writer->WriteLine("switch (other.$0$Case) {", property_name);
writer->Indent();
for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
writer->WriteLine("case $0$OneofCase.$1$: {",
property_name, GetPropertyName(field));
if (field->type() == FieldDescriptor::TYPE_GROUP ||
field->type() == FieldDescriptor::TYPE_MESSAGE) {
writer->WriteLine(" Merge$0$(other.$0$);", GetPropertyName(field));
} else {
writer->WriteLine(" Set$0$(other.$0$);", GetPropertyName(field));
}
writer->WriteLine(" break;");
writer->WriteLine("}");
}
writer->WriteLine("case $0$OneofCase.None: { break; }", property_name);
writer->Outdent();
writer->WriteLine("}");
}
// if message type has extensions // if message type has extensions
if (descriptor_->extension_range_count() > 0) { if (descriptor_->extension_range_count() > 0) {
writer->WriteLine(" this.MergeExtensionFields(other);"); writer->WriteLine(" this.MergeExtensionFields(other);");

View File

@ -50,6 +50,7 @@ namespace csharp {
MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor,
int fieldOrdinal) int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
has_property_check = "has" + property_name();
} }
MessageFieldGenerator::~MessageFieldGenerator() { MessageFieldGenerator::~MessageFieldGenerator() {
@ -149,7 +150,7 @@ void MessageFieldGenerator::GenerateParsingCode(Writer* writer) {
} }
void MessageFieldGenerator::GenerateSerializationCode(Writer* writer) { void MessageFieldGenerator::GenerateSerializationCode(Writer* writer) {
writer->WriteLine("if (has$0$) {", property_name()); writer->WriteLine("if ($0$) {", has_property_check);
writer->WriteLine(" output.Write$0$($1$, field_names[$3$], $2$);", writer->WriteLine(" output.Write$0$($1$, field_names[$3$], $2$);",
message_or_group(), number(), property_name(), message_or_group(), number(), property_name(),
field_ordinal()); field_ordinal());
@ -157,7 +158,7 @@ void MessageFieldGenerator::GenerateSerializationCode(Writer* writer) {
} }
void MessageFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void MessageFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
writer->WriteLine("if (has$0$) {", property_name()); writer->WriteLine("if ($0$) {", has_property_check);
writer->WriteLine(" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);", writer->WriteLine(" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);",
message_or_group(), number(), property_name()); message_or_group(), number(), property_name());
writer->WriteLine("}"); writer->WriteLine("}");
@ -177,6 +178,119 @@ void MessageFieldGenerator::WriteToString(Writer* writer) {
property_name(), name(), GetFieldName(descriptor_)); property_name(), name(), GetFieldName(descriptor_));
} }
MessageOneofFieldGenerator::MessageOneofFieldGenerator(const FieldDescriptor* descriptor,
int fieldOrdinal)
: MessageFieldGenerator(descriptor, fieldOrdinal) {
has_property_check = oneof_name() + "Case_ == " + oneof_property_name() +
"OneofCase." + property_name();
}
MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {
}
void MessageOneofFieldGenerator::GenerateMembers(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return $0$; }", has_property_check);
writer->WriteLine("}");
}
AddDeprecatedFlag(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$ ? ($1$) $2$_ : $3$; }",
has_property_check, type_name(), oneof_name(), default_value());
writer->WriteLine("}");
}
void MessageOneofFieldGenerator::GenerateBuilderMembers(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) {
AddDeprecatedFlag(writer);
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.$0$; }", has_property_check);
writer->WriteLine("}");
}
AddDeprecatedFlag(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$ ? ($1$) result.$2$_ : $3$; }",
has_property_check, type_name(), oneof_name(), default_value());
writer->WriteLine(" set { Set$0$(value); }", property_name());
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(),
type_name());
AddNullCheck(writer);
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine(" result.$0$_ = value;", oneof_name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Set$0$($1$.Builder builderForValue) {",
property_name(), type_name());
AddNullCheck(writer, "builderForValue");
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine(" result.$0$_ = builderForValue.Build();", oneof_name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Merge$0$($1$ value) {", property_name(),
type_name());
AddNullCheck(writer);
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" if (result.$0$ &&", has_property_check);
writer->WriteLine(" result.$0$ != $1$) {", property_name(), default_value());
writer->WriteLine(
" result.$0$_ = $1$.CreateBuilder(result.$2$).MergeFrom(value).BuildPartial();",
oneof_name(), type_name(), property_name());
writer->WriteLine(" } else {");
writer->WriteLine(" result.$0$_ = value;", oneof_name());
writer->WriteLine(" }");
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" if (result.$0$) {", has_property_check);
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;",
oneof_name(), oneof_property_name());
writer->WriteLine(" result.$0$_ = null;", oneof_name());
writer->WriteLine(" }");
writer->WriteLine(" return this;");
writer->WriteLine("}");
}
void MessageOneofFieldGenerator::GenerateParsingCode(Writer* writer) {
writer->WriteLine("$0$.Builder subBuilder = $0$.CreateBuilder();",
type_name());
writer->WriteLine("if (result.$0$) {", has_property_check);
writer->WriteLine(" subBuilder.MergeFrom($0$);", property_name());
writer->WriteLine("}");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
writer->WriteLine("input.ReadGroup($0$, subBuilder, extensionRegistry);",
number());
} else {
writer->WriteLine("input.ReadMessage(subBuilder, extensionRegistry);");
}
writer->WriteLine("result.$0$_ = subBuilder.BuildPartial();", oneof_name());
writer->WriteLine("result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
}
void MessageOneofFieldGenerator::WriteEquals(Writer* writer) {
writer->WriteLine("if (!$0$.Equals(other.$0$)) return false;", property_name());
}
void MessageOneofFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);",
descriptor_->name(), has_property_check, oneof_name());
}
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf

View File

@ -60,10 +60,28 @@ class MessageFieldGenerator : public FieldGeneratorBase {
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(Writer* writer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(Writer* writer);
protected:
string has_property_check;
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator);
}; };
class MessageOneofFieldGenerator : public MessageFieldGenerator {
public:
MessageOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~MessageOneofFieldGenerator();
virtual void GenerateMembers(Writer* writer);
virtual void GenerateBuilderMembers(Writer* writer);
virtual void WriteEquals(Writer* writer);
virtual void WriteToString(Writer* writer);
virtual void GenerateParsingCode(Writer* writer);
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageOneofFieldGenerator);
};
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf

View File

@ -50,6 +50,11 @@ namespace csharp {
PrimitiveFieldGenerator::PrimitiveFieldGenerator( PrimitiveFieldGenerator::PrimitiveFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal) const FieldDescriptor* descriptor, int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
if (SupportFieldPresence(descriptor_->file())) {
has_property_check = "has" + property_name();
} else {
has_property_check = property_name() + " != " + default_value();
}
} }
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() { PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
@ -133,11 +138,7 @@ void PrimitiveFieldGenerator::GenerateParsingCode(Writer* writer) {
} }
void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("if ($0$) {", has_property_check);
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());
@ -145,22 +146,14 @@ void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
} }
void PrimitiveFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("if ($0$) {", has_property_check);
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) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("if ($0$) {", has_property_check);
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(" hash ^= $0$_.GetHashCode();", name());
writer->WriteLine("}"); writer->WriteLine("}");
} }
@ -175,13 +168,84 @@ void PrimitiveFieldGenerator::WriteEquals(Writer* writer) {
} }
} }
void PrimitiveFieldGenerator::WriteToString(Writer* writer) { void PrimitiveFieldGenerator::WriteToString(Writer* writer) {
if (SupportFieldPresence(descriptor_->file())) { writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);",
writer->WriteLine("PrintField(\"$0$\", has$1$, $2$_, writer);", descriptor_->name(), has_property_check, name());
descriptor_->name(), property_name(), name());
} else {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);",
descriptor_->name(), name());
} }
PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal)
: PrimitiveFieldGenerator(descriptor, fieldOrdinal) {
has_property_check = oneof_name() + "Case_ == " + oneof_property_name() +
"OneofCase." + property_name();
}
PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
}
void PrimitiveOneofFieldGenerator::GenerateMembers(Writer* writer) {
AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return $0$; }", has_property_check);
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return $0$ ? ($1$) $2$_ : $3$; }",
has_property_check, type_name(), oneof_name(), default_value());
writer->WriteLine("}");
}
void PrimitiveOneofFieldGenerator::GenerateBuilderMembers(Writer* writer) {
AddDeprecatedFlag(writer);
if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name());
writer->WriteLine(" get { return result.$0$; }", has_property_check);
writer->WriteLine("}");
}
AddPublicMemberAttributes(writer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name());
writer->WriteLine(" get { return result.$0$ ? ($1$) result.$2$_ : $3$; }",
has_property_check, type_name(), oneof_name(), default_value());
writer->WriteLine(" set { Set$0$(value); }", property_name());
writer->WriteLine("}");
AddPublicMemberAttributes(writer);
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(),
type_name());
AddNullCheck(writer);
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" result.$0$_ = value;", oneof_name());
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine(" return this;");
writer->WriteLine("}");
AddDeprecatedFlag(writer);
writer->WriteLine("public Builder Clear$0$() {", property_name());
writer->WriteLine(" PrepareBuilder();");
writer->WriteLine(" if (result.$0$) {", has_property_check);
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;",
oneof_name(), oneof_property_name());
writer->WriteLine(" }");
writer->WriteLine(" return this;");
writer->WriteLine("}");
}
void PrimitiveOneofFieldGenerator::WriteEquals(Writer* writer) {
writer->WriteLine("if (!$0$.Equals(other.$0$)) return false;", property_name());
}
void PrimitiveOneofFieldGenerator::WriteToString(Writer* writer) {
writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);",
descriptor_->name(), has_property_check, oneof_name());
}
void PrimitiveOneofFieldGenerator::GenerateParsingCode(Writer* writer) {
writer->WriteLine("$0$ value = $1$;", type_name(), default_value());
writer->WriteLine("if (input.Read$0$(ref value)) {",
capitalized_type_name());
writer->WriteLine(" result.$0$_ = value;", oneof_name());
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;",
oneof_name(), oneof_property_name(), property_name());
writer->WriteLine("}");
} }
} // namespace csharp } // namespace csharp

View File

@ -60,10 +60,28 @@ class PrimitiveFieldGenerator : public FieldGeneratorBase {
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(Writer* writer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(Writer* writer);
protected:
string has_property_check;
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator);
}; };
class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator {
public:
PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~PrimitiveOneofFieldGenerator();
virtual void GenerateMembers(Writer* writer);
virtual void GenerateBuilderMembers(Writer* writer);
virtual void WriteEquals(Writer* writer);
virtual void WriteToString(Writer* writer);
virtual void GenerateParsingCode(Writer* writer);
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveOneofFieldGenerator);
};
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf