diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:35:22 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:35:22 +0100 |
commit | 5d7adf66ceb531be50cec1963ecdf271f5fc591e (patch) | |
tree | d34f52a7dcd26a60cafc28e6125b038552a1cbf7 /csharp/ProtocolBuffers | |
parent | 3f9a6f211664021db368c4b4549793935315382a (diff) | |
download | protobuf-5d7adf66ceb531be50cec1963ecdf271f5fc591e.tar.gz protobuf-5d7adf66ceb531be50cec1963ecdf271f5fc591e.tar.bz2 protobuf-5d7adf66ceb531be50cec1963ecdf271f5fc591e.zip |
First unit test reading a complete message\!
Diffstat (limited to 'csharp/ProtocolBuffers')
-rw-r--r-- | csharp/ProtocolBuffers/AbstractBuilder.cs | 14 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/ByteString.cs | 30 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/CodedInputStream.cs | 4 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/CodedOutputStream.cs | 5 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/Descriptors/FieldDescriptor.cs | 5 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/Descriptors/MessageDescriptor.cs | 8 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs | 2 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs | 7 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs | 2 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/FieldSet.cs | 18 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedBuilder.cs | 9 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/TextFormat.cs | 8 |
12 files changed, 71 insertions, 41 deletions
diff --git a/csharp/ProtocolBuffers/AbstractBuilder.cs b/csharp/ProtocolBuffers/AbstractBuilder.cs index 02626f91..256975e9 100644 --- a/csharp/ProtocolBuffers/AbstractBuilder.cs +++ b/csharp/ProtocolBuffers/AbstractBuilder.cs @@ -104,16 +104,20 @@ namespace Google.ProtocolBuffers { } IBuilder IBuilder.MergeFrom(CodedInputStream input) { - return ((IBuilder)this).MergeFrom(input, ExtensionRegistry.Empty); + return MergeFromImpl(input, ExtensionRegistry.Empty); } - IBuilder IBuilder.MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) { + protected virtual IBuilder MergeFromImpl(CodedInputStream input, ExtensionRegistry extensionRegistry) { UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder(UnknownFields); FieldSet.MergeFrom(input, unknownFields, extensionRegistry, this); UnknownFields = unknownFields.Build(); return this; } + IBuilder IBuilder.MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) { + return MergeFromImpl(input, extensionRegistry); + } + IBuilder IBuilder.MergeUnknownFields(UnknownFieldSet unknownFields) { UnknownFields = UnknownFieldSet.CreateBuilder(UnknownFields) .MergeFrom(unknownFields) @@ -121,8 +125,6 @@ namespace Google.ProtocolBuffers { return this; } - public UnknownFieldSet UnknownFields { get; set; } - IBuilder IBuilder.MergeFrom(ByteString data) { CodedInputStream input = data.CreateCodedInput(); ((IBuilder)this).MergeFrom(input); @@ -160,9 +162,11 @@ namespace Google.ProtocolBuffers { IBuilder IBuilder.MergeFrom(Stream input, ExtensionRegistry extensionRegistry) { CodedInputStream codedInput = CodedInputStream.CreateInstance(input); - ((IBuilder) this).MergeFrom(codedInput, extensionRegistry); + ((IBuilder)this).MergeFrom(codedInput, extensionRegistry); codedInput.CheckLastTagWas(0); return this; } + + public abstract UnknownFieldSet UnknownFields { get; set; } } } diff --git a/csharp/ProtocolBuffers/ByteString.cs b/csharp/ProtocolBuffers/ByteString.cs index 7c644f9a..8a7909e3 100644 --- a/csharp/ProtocolBuffers/ByteString.cs +++ b/csharp/ProtocolBuffers/ByteString.cs @@ -23,7 +23,7 @@ namespace Google.ProtocolBuffers { /// Immutable array of bytes. /// TODO(jonskeet): Implement the common collection interfaces? /// </summary> - public sealed class ByteString : IEnumerable<byte> { + public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString> { private static readonly ByteString empty = new ByteString(new byte[0]); @@ -126,6 +126,34 @@ namespace Google.ProtocolBuffers { // TODO(jonskeet): CopyTo, Equals, GetHashCode if they turn out to be required + public override bool Equals(object obj) { + ByteString other = obj as ByteString; + if (obj == null) { + return false; + } + return Equals(other); + } + + public override int GetHashCode() { + int ret = 23; + foreach (byte b in bytes) { + ret = (ret << 8) | b; + } + return ret; + } + + public bool Equals(ByteString other) { + if (other.bytes.Length != bytes.Length) { + return false; + } + for (int i = 0; i < bytes.Length; i++) { + if (other.bytes[i] != bytes[i]) { + return false; + } + } + return true; + } + /// <summary> /// Builder for ByteStrings which allows them to be created without extra /// copying being involved. This has to be a nested type in order to have access diff --git a/csharp/ProtocolBuffers/CodedInputStream.cs b/csharp/ProtocolBuffers/CodedInputStream.cs index 418f5809..7fab7d21 100644 --- a/csharp/ProtocolBuffers/CodedInputStream.cs +++ b/csharp/ProtocolBuffers/CodedInputStream.cs @@ -569,8 +569,8 @@ namespace Google.ProtocolBuffers { totalBytesRetired += bufferSize; bufferPos = 0; - bufferSize = (input == null) ? -1 : input.Read(buffer, 0, buffer.Length); - if (bufferSize == -1) { + bufferSize = (input == null) ? 0 : input.Read(buffer, 0, buffer.Length); + if (bufferSize == 0) { bufferSize = 0; if (mustSucceed) { throw InvalidProtocolBufferException.TruncatedMessage(); diff --git a/csharp/ProtocolBuffers/CodedOutputStream.cs b/csharp/ProtocolBuffers/CodedOutputStream.cs index fff30d3f..6314a702 100644 --- a/csharp/ProtocolBuffers/CodedOutputStream.cs +++ b/csharp/ProtocolBuffers/CodedOutputStream.cs @@ -206,6 +206,7 @@ namespace Google.ProtocolBuffers { public void WriteBytes(int fieldNumber, ByteString value) { // TODO(jonskeet): Optimise this! (No need to copy the bytes twice.) + WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited); byte[] bytes = value.ToByteArray(); WriteRawVarint32((uint)bytes.Length); WriteRawBytes(bytes); @@ -223,12 +224,12 @@ namespace Google.ProtocolBuffers { public void WriteSFixed32(int fieldNumber, int value) { WriteTag(fieldNumber, WireFormat.WireType.Fixed32); - WriteRawVarint32((uint)value); + WriteRawLittleEndian32((uint)value); } public void WriteSFixed64(int fieldNumber, long value) { WriteTag(fieldNumber, WireFormat.WireType.Fixed64); - WriteRawVarint64((ulong)value); + WriteRawLittleEndian64((ulong)value); } public void WriteSInt32(int fieldNumber, int value) { diff --git a/csharp/ProtocolBuffers/Descriptors/FieldDescriptor.cs b/csharp/ProtocolBuffers/Descriptors/FieldDescriptor.cs index 38e6adfa..f2f99a38 100644 --- a/csharp/ProtocolBuffers/Descriptors/FieldDescriptor.cs +++ b/csharp/ProtocolBuffers/Descriptors/FieldDescriptor.cs @@ -22,7 +22,7 @@ namespace Google.ProtocolBuffers.Descriptors { if (proto.HasType) { fieldType = GetFieldTypeFromProtoType(proto.Type); - //type = proto.Type; + mappedType = FieldTypeToMappedTypeMap[fieldType]; } if (FieldNumber <= 0) { @@ -244,8 +244,7 @@ namespace Google.ProtocolBuffers.Descriptors { /// Immutable mapping from field type to mapped type. Built using the attributes on /// FieldType values. /// </summary> - public static readonly IDictionary<FieldType, MappedType> FieldTypeToWireFormatMap = MapFieldTypes(); - + public static readonly IDictionary<FieldType, MappedType> FieldTypeToMappedTypeMap = MapFieldTypes(); private static IDictionary<FieldType, MappedType> MapFieldTypes() { var map = new Dictionary<FieldType, MappedType>(); diff --git a/csharp/ProtocolBuffers/Descriptors/MessageDescriptor.cs b/csharp/ProtocolBuffers/Descriptors/MessageDescriptor.cs index 548f226b..b733fb93 100644 --- a/csharp/ProtocolBuffers/Descriptors/MessageDescriptor.cs +++ b/csharp/ProtocolBuffers/Descriptors/MessageDescriptor.cs @@ -19,16 +19,16 @@ namespace Google.ProtocolBuffers.Descriptors { containingType = parent; nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedTypeList, - (type, index) => new MessageDescriptor(type, file, null, index)); + (type, index) => new MessageDescriptor(type, file, this, index)); enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumTypeList, - (type, index) => new EnumDescriptor(type, file, null, index)); + (type, index) => new EnumDescriptor(type, file, this, index)); fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.FieldList, - (field, index) => new FieldDescriptor(field, file, null, index, false)); + (field, index) => new FieldDescriptor(field, file, this, index, false)); extensions = DescriptorUtil.ConvertAndMakeReadOnly(proto.ExtensionList, - (field, index) => new FieldDescriptor(field, file, null, index, true)); + (field, index) => new FieldDescriptor(field, file, this, index, true)); file.DescriptorPool.AddSymbol(this); } diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs index 6f40938e..18da75c4 100644 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs @@ -21,7 +21,7 @@ namespace Google.ProtocolBuffers.FieldAccess { internal RepeatedMessageAccessor(string name, Type messageType, Type builderType) : base(name, messageType, builderType) { - createBuilderMethod = ClrType.GetMethod("CreateBuilder", BindingFlags.Public | BindingFlags.Static); + createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]); if (createBuilderMethod == null) { throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name); } diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs index 232a1e28..11611b64 100644 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs @@ -11,7 +11,6 @@ namespace Google.ProtocolBuffers.FieldAccess { private readonly PropertyInfo messageProperty; private readonly PropertyInfo builderProperty; - private readonly PropertyInfo hasProperty; private readonly PropertyInfo countProperty; private readonly MethodInfo clearMethod; private readonly MethodInfo addMethod; @@ -30,15 +29,13 @@ namespace Google.ProtocolBuffers.FieldAccess { internal RepeatedPrimitiveAccessor(string name, Type messageType, Type builderType) { messageProperty = messageType.GetProperty(name + "List"); builderProperty = builderType.GetProperty(name + "List"); - hasProperty = messageType.GetProperty("Has" + name); countProperty = messageType.GetProperty(name + "Count"); clearMethod = builderType.GetMethod("Clear" + name); - addMethod = builderType.GetMethod("Add" + name); getElementMethod = messageType.GetMethod("Get" + name, new Type[] { typeof(int) }); - setElementMethod = builderType.GetMethod("Set" + name, new Type[] { typeof(int) }); + addMethod = builderType.GetMethod("Add" + name, new Type[] { ClrType }); + setElementMethod = builderType.GetMethod("Set" + name, new Type[] { typeof(int), ClrType }); if (messageProperty == null || builderProperty == null - || hasProperty == null || countProperty == null || clearMethod == null || addMethod == null diff --git a/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs index c9647b07..20e1ba13 100644 --- a/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs @@ -19,7 +19,7 @@ namespace Google.ProtocolBuffers.FieldAccess { internal SingleMessageAccessor(string name, Type messageType, Type builderType) : base(name, messageType, builderType) { - createBuilderMethod = ClrType.GetMethod("CreateBuilder", BindingFlags.Public | BindingFlags.Static); + createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);//BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); if (createBuilderMethod == null) { throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name); } diff --git a/csharp/ProtocolBuffers/FieldSet.cs b/csharp/ProtocolBuffers/FieldSet.cs index e42b4a01..bca7e5b5 100644 --- a/csharp/ProtocolBuffers/FieldSet.cs +++ b/csharp/ProtocolBuffers/FieldSet.cs @@ -91,11 +91,8 @@ namespace Google.ProtocolBuffers { } // TODO(jonskeet): Should this be in UnknownFieldSet.Builder really? Or CodedInputStream? - internal static void MergeFrom(CodedInputStream input, - UnknownFieldSet.Builder unknownFields, - ExtensionRegistry extensionRegistry, - IBuilder builder) { - + internal static void MergeFrom(CodedInputStream input, UnknownFieldSet.Builder unknownFields, + ExtensionRegistry extensionRegistry, IBuilder builder) { while (true) { uint tag = input.ReadTag(); if (tag == 0) { @@ -120,15 +117,10 @@ namespace Google.ProtocolBuffers { /// <param name="builder">Builder to merge field into, if it's a known field</param> /// <param name="tag">The tag, which should already have been read from the input</param> /// <returns>true unless the tag is an end-group tag</returns> - internal static bool MergeFieldFrom(CodedInputStream input, - UnknownFieldSet.Builder unknownFields, - ExtensionRegistry extensionRegistry, - IBuilder builder, - uint tag) { + internal static bool MergeFieldFrom(CodedInputStream input, UnknownFieldSet.Builder unknownFields, + ExtensionRegistry extensionRegistry, IBuilder builder, uint tag) { MessageDescriptor type = builder.DescriptorForType; - - if (type.Options.MessageSetWireFormat - && tag == WireFormat.MessageSetTag.ItemStart) { + if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart) { MergeMessageSetExtensionFromCodedStream(input, unknownFields, extensionRegistry, builder); return true; } diff --git a/csharp/ProtocolBuffers/GeneratedBuilder.cs b/csharp/ProtocolBuffers/GeneratedBuilder.cs index acafe486..4b3567d0 100644 --- a/csharp/ProtocolBuffers/GeneratedBuilder.cs +++ b/csharp/ProtocolBuffers/GeneratedBuilder.cs @@ -199,6 +199,10 @@ namespace Google.ProtocolBuffers { ((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 @@ -222,6 +226,11 @@ namespace Google.ProtocolBuffers { } return BuildPartial(); } + + public override UnknownFieldSet UnknownFields { + get { return MessageBeingBuilt.UnknownFields; } + set { MessageBeingBuilt.SetUnknownFields(value); } + } public abstract TMessage BuildPartial(); public abstract IBuilder<TMessage> Clone(); diff --git a/csharp/ProtocolBuffers/TextFormat.cs b/csharp/ProtocolBuffers/TextFormat.cs index e395b5f9..a9a4e1b8 100644 --- a/csharp/ProtocolBuffers/TextFormat.cs +++ b/csharp/ProtocolBuffers/TextFormat.cs @@ -187,15 +187,15 @@ namespace Google.ProtocolBuffers { } internal static ulong ParseUInt64(string text) { - return (ulong) ParseInteger(text, true, false); + return (ulong) ParseInteger(text, false, true); } internal static long ParseInt64(string text) { - return ParseInteger(text, true, false); + return ParseInteger(text, true, true); } internal static uint ParseUInt32(string text) { - return (uint) ParseInteger(text, true, false); + return (uint) ParseInteger(text, false, false); } internal static int ParseInt32(string text) { @@ -229,7 +229,7 @@ namespace Google.ProtocolBuffers { ulong result = Convert.ToUInt64(text, radix); if (negative) { - ulong max = isLong ? 0x8000000UL : 0x8000L; + ulong max = isLong ? 0x8000000000000000UL : 0x80000000L; if (result > max) { throw new FormatException("Number of out range: " + original); } |