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