Commit bef2caf5 authored by Jon Skeet's avatar Jon Skeet

Added DynamicMessage and ExtendableBuilder, along with the first supporting tests.

parent 5d7adf66
This diff is collapsed.
......@@ -275,64 +275,64 @@ namespace Google.ProtocolBuffers {
}
}
/* TODO(jonskeet): Reinstate this when protoc is ready
private TestRecursiveMessage makeRecursiveMessage(int depth) {
private static TestRecursiveMessage MakeRecursiveMessage(int depth) {
if (depth == 0) {
return TestRecursiveMessage.newBuilder().setI(5).build();
return TestRecursiveMessage.CreateBuilder().SetI(5).Build();
} else {
return TestRecursiveMessage.newBuilder()
.setA(makeRecursiveMessage(depth - 1)).build();
return TestRecursiveMessage.CreateBuilder()
.SetA(MakeRecursiveMessage(depth - 1)).Build();
}
}
private void assertMessageDepth(TestRecursiveMessage message, int depth) {
private static void AssertMessageDepth(TestRecursiveMessage message, int depth) {
if (depth == 0) {
assertFalse(message.hasA());
assertEquals(5, message.getI());
Assert.IsFalse(message.HasA);
Assert.AreEqual(5, message.I);
} else {
assertTrue(message.hasA());
assertMessageDepth(message.getA(), depth - 1);
Assert.IsTrue(message.HasA);
AssertMessageDepth(message.A, depth - 1);
}
}
public void testMaliciousRecursion() {
ByteString data64 = makeRecursiveMessage(64).toByteString();
ByteString data65 = makeRecursiveMessage(65).toByteString();
[Test]
public void MaliciousRecursion() {
ByteString data64 = MakeRecursiveMessage(64).ToByteString();
ByteString data65 = MakeRecursiveMessage(65).ToByteString();
assertMessageDepth(TestRecursiveMessage.parseFrom(data64), 64);
AssertMessageDepth(TestRecursiveMessage.ParseFrom(data64), 64);
try {
TestRecursiveMessage.parseFrom(data65);
fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException e) {
TestRecursiveMessage.ParseFrom(data65);
Assert.Fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException) {
// success.
}
CodedInputStream input = data64.newCodedInput();
input.setRecursionLimit(8);
CodedInputStream input = data64.CreateCodedInput();
input.SetRecursionLimit(8);
try {
TestRecursiveMessage.parseFrom(input);
fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException e) {
TestRecursiveMessage.ParseFrom(input);
Assert.Fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException) {
// success.
}
}
*/
/* TODO(jonskeet): Reinstate this when protoc is ready
public void testSizeLimit() throws Exception {
CodedInputStream input = CodedInputStream.newInstance(
TestUtil.getAllSet().toByteString().newInput());
input.setSizeLimit(16);
[Test]
public void SizeLimit() {
// Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
// apply to the latter case.
MemoryStream ms = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
CodedInputStream input = CodedInputStream.CreateInstance(ms);
input.SetSizeLimit(16);
try {
TestAllTypes.parseFrom(input);
fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException e) {
TestAllTypes.ParseFrom(input);
Assert.Fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException) {
// success.
}
}*/
}
/// <summary>
/// Tests that if we read an string that contains invalid UTF-8, no exception
......
......@@ -14,6 +14,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
namespace Google.ProtocolBuffers {
......@@ -33,6 +34,10 @@ namespace Google.ProtocolBuffers {
return bytes;
}
private static void AssertEqualBytes(byte[] a, byte[] b) {
Assert.AreEqual(ByteString.CopyFrom(a), ByteString.CopyFrom(b));
}
/// <summary>
/// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
/// checks that the result matches the given bytes
......@@ -174,24 +179,23 @@ namespace Google.ProtocolBuffers {
0x9abcdef012345678UL);
}
/* TODO(jonskeet): Put this back when we've got the rest working!
[Test]
public void testWriteWholeMessage() throws Exception {
TestAllTypes message = TestUtil.getAllSet();
public void WriteWholeMessage() {
TestAllTypes message = TestUtil.GetAllSet();
byte[] rawBytes = message.toByteArray();
assertEqualBytes(TestUtil.getGoldenMessage().toByteArray(), rawBytes);
byte[] rawBytes = message.ToByteArray();
AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);
// Try different block sizes.
for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output =
CodedOutputStream.newInstance(rawOutput, blockSize);
message.writeTo(output);
output.flush();
assertEqualBytes(rawBytes, rawOutput.toByteArray());
CodedOutputStream.CreateInstance(rawOutput, blockSize);
message.WriteTo(output);
output.Flush();
AssertEqualBytes(rawBytes, rawOutput.ToArray());
}
}
} */
[Test]
......
......@@ -44,10 +44,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractMessageTest.cs" />
<Compile Include="ByteStringTest.cs" />
<Compile Include="CodedInputStreamTest.cs" />
<Compile Include="CodedOutputStreamTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionTester.cs" />
<Compile Include="TestProtos\UnitTestEmbedOptimizeForProtoFile.cs" />
<Compile Include="TestProtos\UnitTestImportProtoFile.cs" />
<Compile Include="TestProtos\UnitTestMessageSetProtoFile.cs" />
......
This diff is collapsed.
......@@ -40,7 +40,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#endregion
#region Extensions
/**/
#endregion
#region Static variables
......@@ -219,6 +218,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::TestEmbedOptimizedForSize) {
return MergeFrom((self::TestEmbedOptimizedForSize) other);
......
......@@ -34,7 +34,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#endregion
#region Extensions
/**/
#endregion
#region Static variables
......@@ -192,6 +191,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::ImportMessage) {
return MergeFrom((self::ImportMessage) other);
......
......@@ -55,7 +55,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#endregion
#region Extensions
/**/
#endregion
#region Static variables
......@@ -197,7 +196,7 @@ namespace Google.ProtocolBuffers.TestProtos {
return (Builder) new Builder().MergeFrom(prototype);
}
public sealed partial class Builder : pb::GeneratedBuilder<self::TestMessageSet, self::TestMessageSet.Builder>.ExtendableBuilder {
public sealed partial class Builder : pb::ExtendableBuilder<self::TestMessageSet, self::TestMessageSet.Builder> {
// Construct using self::TestMessageSet.CreateBuilder()
internal Builder() {}
......@@ -230,6 +229,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::TestMessageSet) {
return MergeFrom((self::TestMessageSet) other);
......@@ -409,6 +412,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::TestMessageSetContainer) {
return MergeFrom((self::TestMessageSetContainer) other);
......@@ -639,6 +646,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::TestMessageSetExtension1) {
return MergeFrom((self::TestMessageSetExtension1) other);
......@@ -848,6 +859,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::TestMessageSetExtension2) {
return MergeFrom((self::TestMessageSetExtension2) other);
......@@ -1091,6 +1106,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::RawMessageSet.Types.Item) {
return MergeFrom((self::RawMessageSet.Types.Item) other);
......@@ -1309,6 +1328,10 @@ namespace Google.ProtocolBuffers.TestProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::RawMessageSet) {
return MergeFrom((self::RawMessageSet) other);
......
......@@ -39,7 +39,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#endregion
#region Extensions
/**/
#endregion
#region Static variables
......@@ -146,7 +145,7 @@ namespace Google.ProtocolBuffers.TestProtos {
return (Builder) new Builder().MergeFrom(prototype);
}
public sealed partial class Builder : pb::GeneratedBuilder<self::TestOptimizedForSize, self::TestOptimizedForSize.Builder>.ExtendableBuilder {
public sealed partial class Builder : pb::ExtendableBuilder<self::TestOptimizedForSize, self::TestOptimizedForSize.Builder> {
// Construct using self::TestOptimizedForSize.CreateBuilder()
internal Builder() {}
......
This diff is collapsed.
......@@ -8,6 +8,7 @@ using System.IO;
namespace Google.ProtocolBuffers {
/// <summary>
/// Implementation of the non-generic IMessage interface as far as possible.
/// TODO(jonskeet): Make this generic, to avoid so much casting in DynamicMessage.
/// </summary>
public abstract class AbstractBuilder : IBuilder {
#region Unimplemented members of IBuilder
......@@ -57,7 +58,7 @@ namespace Google.ProtocolBuffers {
}
#endregion
public IBuilder Clear() {
public virtual IBuilder Clear() {
foreach(FieldDescriptor field in AllFields.Keys) {
ClearFieldImpl(field);
}
......@@ -168,5 +169,15 @@ namespace Google.ProtocolBuffers {
}
public abstract UnknownFieldSet UnknownFields { get; set; }
public IBuilder SetField(FieldDescriptor field, object value) {
this[field] = value;
return this;
}
public IBuilder SetRepeatedField(FieldDescriptor field, int index, object value) {
this[field, index] = value;
return this;
}
}
}
......@@ -16,6 +16,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Google.ProtocolBuffers.Collections;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers {
......@@ -171,15 +172,13 @@ namespace Google.ProtocolBuffers {
if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
return false;
}
// TODO(jonskeet): Check that dictionaries support equality appropriately
// (I suspect they don't!)
return AllFields.Equals(otherMessage.AllFields);
return Dictionaries.Equals(AllFields, otherMessage.AllFields);
}
public override int GetHashCode() {
int hash = 41;
hash = (19 * hash) + DescriptorForType.GetHashCode();
hash = (53 * hash) + AllFields.GetHashCode();
hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
return hash;
}
}
......
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.Collections {
......@@ -9,8 +11,85 @@ namespace Google.ProtocolBuffers.Collections {
/// in the generic class.
/// </summary>
public static class Dictionaries {
/// <summary>
/// Compares two dictionaries for equality. Each value is compared with equality using Equals
/// for non-IEnumerable implementations, and using EnumerableEquals otherwise.
/// TODO(jonskeet): This is clearly pretty slow, and involves lots of boxing/unboxing...
/// </summary>
public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right) {
if (left.Count != right.Count) {
return false;
}
foreach (KeyValuePair<TKey,TValue> leftEntry in left)
{
TValue rightValue;
if (!right.TryGetValue(leftEntry.Key, out rightValue)) {
return false;
}
IEnumerable leftEnumerable = leftEntry.Value as IEnumerable;
IEnumerable rightEnumerable = rightValue as IEnumerable;
if (leftEnumerable == null || rightEnumerable == null) {
if (!object.Equals(leftEntry.Value, rightValue)) {
return false;
}
} else {
IEnumerator leftEnumerator = leftEnumerable.GetEnumerator();
try {
foreach (object rightObject in rightEnumerable) {
if (!leftEnumerator.MoveNext()) {
return false;
}
if (!object.Equals(leftEnumerator.Current, rightObject)) {
return false;
}
}
if (leftEnumerator.MoveNext()) {
return false;
}
} finally {
if (leftEnumerator is IDisposable) {
((IDisposable)leftEnumerator).Dispose();
}
}
}
}
return true;
}
public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue> (IDictionary<TKey, TValue> dictionary) {
return dictionary.IsReadOnly ? dictionary : new ReadOnlyDictionary<TKey, TValue>(dictionary);
}
/// <summary>
/// Creates a hashcode for a dictionary by XORing the hashcodes of all the fields
/// and values. (By XORing, we avoid ordering issues.)
/// TODO(jonskeet): Currently XORs other stuff too, and assumes non-null values.
/// </summary>
public static int GetHashCode<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
int ret = 31;
foreach (KeyValuePair<TKey, TValue> entry in dictionary) {
int hash = entry.Key.GetHashCode() ^ GetDeepHashCode(entry.Value);
ret ^= hash;
}
return ret;
}
/// <summary>
/// Determines the hash of a value by either taking it directly or hashing all the elements
/// for IEnumerable implementations.
/// </summary>
private static int GetDeepHashCode(object value) {
IEnumerable iterable = value as IEnumerable;
if (iterable == null) {
return value.GetHashCode();
}
int hash = 29;
foreach (object element in iterable) {
hash = hash * 37 + element.GetHashCode();
}
return hash;
}
}
}
......@@ -161,7 +161,6 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
#endregion
#region Extensions
/**/
#endregion
#region Static variables
......@@ -547,6 +546,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::FileDescriptorProto) {
return MergeFrom((self::FileDescriptorProto) other);
......@@ -1128,6 +1131,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::DescriptorProto.Types.ExtensionRange) {
return MergeFrom((self::DescriptorProto.Types.ExtensionRange) other);
......@@ -1459,6 +1466,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::DescriptorProto) {
return MergeFrom((self::DescriptorProto) other);
......@@ -2130,6 +2141,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::FieldDescriptorProto) {
return MergeFrom((self::FieldDescriptorProto) other);
......@@ -2582,6 +2597,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::EnumDescriptorProto) {
return MergeFrom((self::EnumDescriptorProto) other);
......@@ -2919,6 +2938,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::EnumValueDescriptorProto) {
return MergeFrom((self::EnumValueDescriptorProto) other);
......@@ -3231,6 +3254,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::ServiceDescriptorProto) {
return MergeFrom((self::ServiceDescriptorProto) other);
......@@ -3584,6 +3611,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::MethodDescriptorProto) {
return MergeFrom((self::MethodDescriptorProto) other);
......@@ -4022,6 +4053,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::FileOptions) {
return MergeFrom((self::FileOptions) other);
......@@ -4437,6 +4472,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::MessageOptions) {
return MergeFrom((self::MessageOptions) other);
......@@ -4664,6 +4703,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::FieldOptions) {
return MergeFrom((self::FieldOptions) other);
......@@ -4881,6 +4924,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::EnumOptions) {
return MergeFrom((self::EnumOptions) other);
......@@ -5041,6 +5088,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::EnumValueOptions) {
return MergeFrom((self::EnumValueOptions) other);
......@@ -5201,6 +5252,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::ServiceOptions) {
return MergeFrom((self::ServiceOptions) other);
......@@ -5361,6 +5416,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
return returnMe;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
public override IBuilder MergeFrom(pb::IMessage other) {
if (other is self::MethodOptions) {
return MergeFrom((self::MethodOptions) other);
......
......@@ -43,7 +43,8 @@ namespace Google.ProtocolBuffers.Descriptors {
/// Finds an enum value by number. If multiple enum values have the
/// same number, this returns the first defined value with that number.
/// </summary>
internal EnumValueDescriptor FindValueByNumber(int number) {
// TODO(jonskeet): Make internal and use InternalsVisibleTo?
public EnumValueDescriptor FindValueByNumber(int number) {
return File.DescriptorPool.FindEnumValueByNumber(this, number);
}
......@@ -52,7 +53,8 @@ namespace Google.ProtocolBuffers.Descriptors {
/// </summary>
/// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
/// <returns>The value's descriptor, or null if not found.</returns>
internal EnumValueDescriptor FindValueByName(string name) {
// TODO(jonskeet): Make internal and use InternalsVisibleTo?
public EnumValueDescriptor FindValueByName(string name) {
return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
}
}
......
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Text;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers {
public abstract class ExtendableBuilder<TMessage, TBuilder> : GeneratedBuilder<TMessage, TBuilder>
where TMessage : ExtendableMessage<TMessage, TBuilder>
where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
protected ExtendableBuilder() {}
/// <summary>
/// Checks if a singular extension is present
/// </summary>
public bool HasExtension<TExtension>(GeneratedExtensionBase<TMessage, TExtension> extension) {
return MessageBeingBuilt.HasExtension(extension);
}
/// <summary>
/// Returns the number of elements in a repeated extension.
/// </summary>
public int GetExtensionCount<TExtension>(GeneratedExtensionBase<TMessage, IList<TExtension>> extension) {
return MessageBeingBuilt.GetExtensionCount(extension);
}
/// <summary>
/// Returns the value of an extension.
/// </summary>
public TExtension GetExtension<TExtension>(GeneratedExtensionBase<TMessage, TExtension> extension) {
return MessageBeingBuilt.GetExtension(extension);
}
/// <summary>
/// Returns one element of a repeated extension.
/// </summary>
public TExtension GetExtension<TExtension>(GeneratedExtensionBase<TMessage, IList<TExtension>> extension, int index) {
return MessageBeingBuilt.GetExtension(extension, index);
}
/// <summary>
/// Sets the value of an extension.
/// </summary>
public ExtendableBuilder<TMessage, TBuilder> SetExtension<TExtension>(GeneratedExtensionBase<TMessage, TExtension> extension, TExtension value) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions[extension.Descriptor] = extension.ToReflectionType(value);
return this;
}
/// <summary>
/// Sets the value of one element of a repeated extension.
/// </summary>
public ExtendableBuilder<TMessage, TBuilder> SetExtension<TExtension>(
GeneratedExtensionBase<TMessage, IList<TExtension>> extension, int index, Type value) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions[extension.Descriptor, index] = extension.SingularToReflectionType(value);
return this;
}
/// <summary>
/// Appends a value to a repeated extension.
/// </summary>
public ExtendableBuilder<TMessage, TBuilder> AddExtension<TExtension>(
GeneratedExtensionBase<TMessage, IList<TExtension>> extension, TExtension value) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
return this;
}
/// <summary>
/// Clears an extension.
/// </summary>
public ExtendableBuilder<TMessage, TBuilder> ClearExtension<TExtension>(
GeneratedExtensionBase<TMessage, TExtension> extension) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions.ClearField(extension.Descriptor);
return this;
}
/// <summary>
/// Called by subclasses to parse an unknown field or an extension.
/// </summary>
/// <returns>true unless the tag is an end-group tag</returns>
protected override bool ParseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
ExtensionRegistry extensionRegistry, uint tag) {
return FieldSet.MergeFieldFrom(input, unknownFields, extensionRegistry, this, tag);
}
// ---------------------------------------------------------------
// Reflection
public override object this[FieldDescriptor field, int index] {
set {
if (field.IsExtension) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
// TODO(jonskeet): Figure out how to call this!
// message.VerifyExtensionContainingType(field);
message.Extensions[field, index] = value;
} else {
base[field, index] = value;
}
}
}
public override object this[FieldDescriptor field] {
set {
if (field.IsExtension) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
// TODO(jonskeet): Figure out how to call this!
// message.VerifyExtensionContainingType(field);
message.Extensions[field] = value;
} else {
base[field] = value;
}
InternalFieldAccessors[field].SetValue(this, value);
}
}
public override IBuilder<TMessage> ClearField(FieldDescriptor field) {
if (field.IsExtension) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyContainingType(field);
message.Extensions.ClearField(field);
return this;
} else {
return base.ClearField(field);
}
}
public override IBuilder<TMessage> AddRepeatedField(FieldDescriptor field, object value) {
if (field.IsExtension) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyContainingType(field);
message.Extensions.AddRepeatedField(field, value);
return this;
} else {
return base.AddRepeatedField(field, value);
}
}
}
}
......@@ -12,10 +12,17 @@ namespace Google.ProtocolBuffers {
protected ExtendableMessage() {}
private readonly FieldSet extensions = FieldSet.CreateFieldSet();
/// <summary>
/// Access for the builder.
/// </summary>
internal FieldSet Extensions {
get { return extensions; }
}
/// <summary>
/// Checks if a singular extension is present.
/// </summary>
public bool HasExtension(GeneratedExtensionBase<TMessage, TBuilder> extension) {
public bool HasExtension<TExtension>(GeneratedExtensionBase<TMessage, TExtension> extension) {
return extensions.HasField(extension.Descriptor);
}
......@@ -111,7 +118,7 @@ namespace Google.ProtocolBuffers {
}
}
private void VerifyContainingType(FieldDescriptor field) {
internal void VerifyContainingType(FieldDescriptor field) {
if (field.ContainingType != DescriptorForType) {
throw new ArgumentException("FieldDescriptor does not match message type.");
}
......@@ -161,5 +168,13 @@ namespace Google.ProtocolBuffers {
protected int ExtensionsSerializedSize {
get { return extensions.SerializedSize; }
}
internal void VerifyExtensionContainingType<TExtension>(GeneratedExtensionBase<TMessage, TExtension> extension) {
if (extension.Descriptor.ContainingType != DescriptorForType) {
// This can only happen if someone uses unchecked operations.
throw new ArgumentException("Extension is for type \"" + extension.Descriptor.ContainingType.FullName
+ "\" which does not match message type \"" + DescriptorForType.FullName + "\".");
}
}
}
}
......@@ -73,7 +73,7 @@ namespace Google.ProtocolBuffers.FieldAccess {
}
public int GetRepeatedCount(IMessage message) {
return (int) countProperty.GetValue(null, null);
return (int) countProperty.GetValue(message, null);
}
public virtual object GetRepeatedValue(IMessage message, int index) {
......
......@@ -65,7 +65,7 @@ namespace Google.ProtocolBuffers {
/// Called by derived classes to parse an unknown field.
/// </summary>
/// <returns>true unless the tag is an end-group tag</returns>
protected bool ParseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
protected virtual bool ParseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
ExtensionRegistry extensionRegistry, uint tag) {
return unknownFields.MergeFieldFrom(tag, input);
}
......@@ -115,7 +115,7 @@ namespace Google.ProtocolBuffers {
return AddRepeatedField(field, value);
}
public IBuilder<TMessage> ClearField(FieldDescriptor field) {
public virtual IBuilder<TMessage> ClearField(FieldDescriptor field) {
InternalFieldAccessors[field].Clear(this);
return this;
}
......@@ -155,7 +155,7 @@ namespace Google.ProtocolBuffers {
return this;
}
public IBuilder<TMessage> AddRepeatedField(FieldDescriptor field, object value) {
public virtual IBuilder<TMessage> AddRepeatedField(FieldDescriptor field, object value) {
InternalFieldAccessors[field].AddRepeated(this, value);
return this;
}
......@@ -190,20 +190,22 @@ namespace Google.ProtocolBuffers {
return this;
}
/// <summary>
/// Overridden when optimized for speed.
/// </summary>
public virtual IBuilder<TMessage> MergeFrom(CodedInputStream input) {
((IBuilder)this).MergeFrom(input);
return this;
}
/// <summary>
/// Overridden when optimized for speed.
/// </summary>
public virtual IBuilder<TMessage> MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) {
((IBuilder)this).MergeFrom(input, extensionRegistry);
return this;
}
protected override IBuilder MergeFromImpl(CodedInputStream data, ExtensionRegistry extensionRegistry) {
return MergeFrom(data, extensionRegistry);
}
/// <summary>
/// Like Build(), but will wrap UninitializedMessageException in
/// InvalidProtocolBufferException.
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
......@@ -33,14 +34,14 @@ namespace Google.ProtocolBuffers {
private readonly FieldDescriptor descriptor;
private readonly IMessage messageDefaultInstance;
protected GeneratedExtensionBase(FieldDescriptor descriptor) {
protected GeneratedExtensionBase(FieldDescriptor descriptor, Type singularExtensionType) {
if (!descriptor.IsExtension) {
throw new ArgumentException("GeneratedExtension given a regular (non-extension) field.");
}
this.descriptor = descriptor;
if (descriptor.MappedType == MappedType.Message) {
PropertyInfo defaultInstanceProperty = typeof(TExtension)
PropertyInfo defaultInstanceProperty = singularExtensionType
.GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public);
if (defaultInstanceProperty == null) {
throw new ArgumentException("No public static DefaultInstance property for type " + typeof(TExtension).Name);
......@@ -83,6 +84,38 @@ namespace Google.ProtocolBuffers {
}
}
/// <summary>
/// Converts from the type used by the native accessors to the type
/// used by reflection accessors. For example, the reflection accessors
/// for enums use EnumValueDescriptors but the native accessors use
/// the generated enum type.
/// </summary>
public object ToReflectionType(object value) {
if (descriptor.IsRepeated) {
if (descriptor.MappedType == MappedType.Enum) {
// Must convert the whole list.
IList<object> result = new List<object>();
foreach (object element in (IEnumerable) value) {
result.Add(SingularToReflectionType(element));
}
return result;
} else {
return value;
}
} else {
return SingularToReflectionType(value);
}
}
/// <summary>
/// Like ToReflectionType(object) but for a single element.
/// </summary>
internal Object SingularToReflectionType(object value) {
return descriptor.MappedType == MappedType.Enum
? descriptor.EnumType.FindValueByNumber((int) value)
: value;
}
public abstract object FromReflectionType(object value);
}
}
\ No newline at end of file
......@@ -48,8 +48,11 @@ namespace Google.ProtocolBuffers {
MessageDescriptor descriptor = DescriptorForType;
foreach (FieldDescriptor field in descriptor.Fields) {
IFieldAccessor accessor = InternalFieldAccessors[field];
if ((field.IsRepeated && accessor.GetRepeatedCount(this) != 0)
|| accessor.Has(this)) {
if (field.IsRepeated) {
if (accessor.GetRepeatedCount(this) != 0) {
ret[field] = accessor.GetValue(this);
}
} else if (HasField(field)) {
ret[field] = accessor.GetValue(this);
}
}
......
......@@ -8,11 +8,11 @@ namespace Google.ProtocolBuffers {
/// Class used to represent repeat extensions in generated classes.
/// </summary>
public class GeneratedRepeatExtension<TContainer, TExtensionElement> : GeneratedExtensionBase<TContainer, IList<TExtensionElement>> {
private GeneratedRepeatExtension(FieldDescriptor field) : base(field) {
private GeneratedRepeatExtension(FieldDescriptor field) : base(field, typeof(TExtensionElement)) {
}
public static GeneratedExtensionBase<TContainer, IList<TExtensionElement>> CreateInstance(FieldDescriptor descriptor) {
if (descriptor.IsRepeated) {
if (!descriptor.IsRepeated) {
throw new ArgumentException("Must call GeneratedRepeatExtension.CreateInstance() for repeated types.");
}
return new GeneratedRepeatExtension<TContainer, TExtensionElement>(descriptor);
......
......@@ -9,7 +9,7 @@ namespace Google.ProtocolBuffers {
public class GeneratedSingleExtension<TContainer, TExtension> : GeneratedExtensionBase<TContainer, TExtension>
where TContainer : IMessage<TContainer> {
internal GeneratedSingleExtension(FieldDescriptor descriptor) : base(descriptor) {
internal GeneratedSingleExtension(FieldDescriptor descriptor) : base(descriptor, typeof(TExtension)) {
}
public static GeneratedSingleExtension<TContainer, TExtension> CreateInstance(FieldDescriptor descriptor) {
......
......@@ -50,6 +50,18 @@ namespace Google.ProtocolBuffers {
/// <returns></returns>
object this[FieldDescriptor field] { get; set; }
/// <summary>
/// Only present in the nongeneric interface - useful for tests, but
/// not as much in real life.
/// </summary>
IBuilder SetField(FieldDescriptor field, object value);
/// <summary>
/// Only present in the nongeneric interface - useful for tests, but
/// not as much in real life.
/// </summary>
IBuilder SetRepeatedField(FieldDescriptor field, int index, object value);
/// <summary>
/// Get the message's type's descriptor.
/// <see cref="IMessage{T}.DescriptorForType"/>
......
......@@ -68,6 +68,7 @@
<Compile Include="Descriptors\PackageDescriptor.cs" />
<Compile Include="Descriptors\ServiceDescriptor.cs" />
<Compile Include="DynamicMessage.cs" />
<Compile Include="ExtendableBuilder.cs" />
<Compile Include="ExtendableMessage.cs" />
<Compile Include="ExtensionInfo.cs" />
<Compile Include="ExtensionRegistry.cs" />
......
......@@ -70,6 +70,9 @@ namespace Google.ProtocolBuffers {
}
private void Write(string data) {
if (data.Length == 0) {
return;
}
if (atStartOfLine) {
atStartOfLine = false;
writer.Write(indent);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment