diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:35:23 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:35:23 +0100 |
commit | bef2caf5e49dbcf6b926d01cfb0948dedee49c93 (patch) | |
tree | b4861e64633b81094c1ae96630136000de0b7505 /csharp/ProtocolBuffers | |
parent | 5d7adf66ceb531be50cec1963ecdf271f5fc591e (diff) | |
download | protobuf-bef2caf5e49dbcf6b926d01cfb0948dedee49c93.tar.gz protobuf-bef2caf5e49dbcf6b926d01cfb0948dedee49c93.tar.bz2 protobuf-bef2caf5e49dbcf6b926d01cfb0948dedee49c93.zip |
Added DynamicMessage and ExtendableBuilder, along with the first supporting tests.
Diffstat (limited to 'csharp/ProtocolBuffers')
-rw-r--r-- | csharp/ProtocolBuffers/AbstractBuilder.cs | 13 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/AbstractMessage.cs | 7 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/Collections/Dictionaries.cs | 79 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs | 61 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/Descriptors/EnumDescriptor.cs | 6 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/DynamicMessage.cs | 376 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/ExtendableBuilder.cs | 147 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/ExtendableMessage.cs | 19 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs | 2 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedBuilder.cs | 16 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedExtensionBase.cs | 37 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedMessage.cs | 7 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedRepeatException.cs | 4 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedSingleExtension.cs | 2 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/IBuilder.cs | 12 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/ProtocolBuffers.csproj | 1 | ||||
-rw-r--r-- | csharp/ProtocolBuffers/TextGenerator.cs | 3 |
17 files changed, 764 insertions, 28 deletions
diff --git a/csharp/ProtocolBuffers/AbstractBuilder.cs b/csharp/ProtocolBuffers/AbstractBuilder.cs index 256975e9..d65c0911 100644 --- a/csharp/ProtocolBuffers/AbstractBuilder.cs +++ b/csharp/ProtocolBuffers/AbstractBuilder.cs @@ -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; + } } } diff --git a/csharp/ProtocolBuffers/AbstractMessage.cs b/csharp/ProtocolBuffers/AbstractMessage.cs index 9f855a45..6dfcc46d 100644 --- a/csharp/ProtocolBuffers/AbstractMessage.cs +++ b/csharp/ProtocolBuffers/AbstractMessage.cs @@ -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; } } diff --git a/csharp/ProtocolBuffers/Collections/Dictionaries.cs b/csharp/ProtocolBuffers/Collections/Dictionaries.cs index fb0ebdd2..640b6597 100644 --- a/csharp/ProtocolBuffers/Collections/Dictionaries.cs +++ b/csharp/ProtocolBuffers/Collections/Dictionaries.cs @@ -1,6 +1,8 @@ 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; + } } } diff --git a/csharp/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs b/csharp/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs index ed159229..14e1f177 100644 --- a/csharp/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs +++ b/csharp/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs @@ -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); diff --git a/csharp/ProtocolBuffers/Descriptors/EnumDescriptor.cs b/csharp/ProtocolBuffers/Descriptors/EnumDescriptor.cs index 83807582..a28efc5f 100644 --- a/csharp/ProtocolBuffers/Descriptors/EnumDescriptor.cs +++ b/csharp/ProtocolBuffers/Descriptors/EnumDescriptor.cs @@ -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); } } diff --git a/csharp/ProtocolBuffers/DynamicMessage.cs b/csharp/ProtocolBuffers/DynamicMessage.cs index 8a18375c..5b22d38b 100644 --- a/csharp/ProtocolBuffers/DynamicMessage.cs +++ b/csharp/ProtocolBuffers/DynamicMessage.cs @@ -1,11 +1,381 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; +using Google.ProtocolBuffers.Descriptors; namespace Google.ProtocolBuffers { - public class DynamicMessage { - internal static object GetDefaultInstance(Google.ProtocolBuffers.Descriptors.MessageDescriptor messageDescriptor) { - throw new NotImplementedException(); + public class DynamicMessage : AbstractMessage { + + private readonly MessageDescriptor type; + private readonly FieldSet fields; + private readonly UnknownFieldSet unknownFields; + private int memoizedSize = -1; + + /// <summary> + /// Creates a DynamicMessage with the given FieldSet. + /// </summary> + /// <param name="type"></param> + /// <param name="fields"></param> + /// <param name="unknownFields"></param> + private DynamicMessage(MessageDescriptor type, FieldSet fields, UnknownFieldSet unknownFields) { + this.type = type; + this.fields = fields; + this.unknownFields = unknownFields; + } + + /// <summary> + /// Returns a DynamicMessage representing the default instance of the given type. + /// </summary> + /// <param name="type"></param> + /// <returns></returns> + public static DynamicMessage GetDefaultInstance(MessageDescriptor type) { + return new DynamicMessage(type, FieldSet.DefaultInstance, UnknownFieldSet.DefaultInstance); + } + + /// <summary> + /// Parses a message of the given type from the given stream. + /// </summary> + public static DynamicMessage ParseFrom(MessageDescriptor type, CodedInputStream input) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(input); + return dynamicBuilder.BuildParsed(); + + } + + /// <summary> + /// Parse a message of the given type from the given stream and extension registry. + /// </summary> + /// <param name="type"></param> + /// <param name="input"></param> + /// <param name="extensionRegistry"></param> + /// <returns></returns> + public static DynamicMessage ParseFrom(MessageDescriptor type, CodedInputStream input, ExtensionRegistry extensionRegistry) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder) builder.MergeFrom(input, extensionRegistry); + return dynamicBuilder.BuildParsed(); + } + + /// <summary> + /// Parses a message of the given type from the given stream. + /// </summary> + public static DynamicMessage ParseFrom(MessageDescriptor type, Stream input) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(input); + return dynamicBuilder.BuildParsed(); + } + + /// <summary> + /// Parse a message of the given type from the given stream and extension registry. + /// </summary> + /// <param name="type"></param> + /// <param name="input"></param> + /// <param name="extensionRegistry"></param> + /// <returns></returns> + public static DynamicMessage ParseFrom(MessageDescriptor type, Stream input, ExtensionRegistry extensionRegistry) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(input, extensionRegistry); + return dynamicBuilder.BuildParsed(); + } + + /// <summary> + /// Parse <paramref name="data"/> as a message of the given type and return it. + /// </summary> + public static DynamicMessage ParseFrom(MessageDescriptor type, ByteString data) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(data); + return dynamicBuilder.BuildParsed(); + } + + /// <summary> + /// Parse <paramref name="data"/> as a message of the given type and return it. + /// </summary> + public static DynamicMessage ParseFrom(MessageDescriptor type, ByteString data, ExtensionRegistry extensionRegistry) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(data, extensionRegistry); + return dynamicBuilder.BuildParsed(); + + } + + /// <summary> + /// Parse <paramref name="data"/> as a message of the given type and return it. + /// </summary> + public static DynamicMessage ParseFrom(MessageDescriptor type, byte[] data) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(data); + return dynamicBuilder.BuildParsed(); + } + + /// <summary> + /// Parse <paramref name="data"/> as a message of the given type and return it. + /// </summary> + public static DynamicMessage ParseFrom(MessageDescriptor type, byte[] data, ExtensionRegistry extensionRegistry) { + IBuilder builder = CreateBuilder(type); + Builder dynamicBuilder = (Builder)builder.MergeFrom(data, extensionRegistry); + return dynamicBuilder.BuildParsed(); + } + + /// <summary> + /// Constructs a builder for the given type. + /// </summary> + public static Builder CreateBuilder(MessageDescriptor type) { + return new Builder(type); + } + + /// <summary> + /// Constructs a builder for a message of the same type as <paramref name="prototype"/>, + /// and initializes it with the same contents. + /// </summary> + /// <param name="prototype"></param> + /// <returns></returns> + public static Builder CreateBuilder(IMessage prototype) { + return (Builder) new Builder(prototype.DescriptorForType).MergeFrom(prototype); + } + + // ----------------------------------------------------------------- + // Implementation of IMessage interface. + + public override MessageDescriptor DescriptorForType { + get { return type; } + } + + protected override IMessage DefaultInstanceForTypeImpl { + get { return GetDefaultInstance(type); } + } + + public override IDictionary<FieldDescriptor, object> AllFields { + get { return fields.AllFields; } + } + + public override bool HasField(FieldDescriptor field) { + VerifyContainingType(field); + return fields.HasField(field); + } + + public override object this[FieldDescriptor field] { + get { + VerifyContainingType(field); + object result = fields[field]; + if (result == null) { + result = GetDefaultInstance(field.MessageType); + } + return result; + } + } + + public override int GetRepeatedFieldCount(FieldDescriptor field) { + VerifyContainingType(field); + return fields.GetRepeatedFieldCount(field); + } + + public override object this[FieldDescriptor field, int index] { + get { + VerifyContainingType(field); + return fields[field, index]; + } + } + + public override UnknownFieldSet UnknownFields { + get { return unknownFields; } + } + + public bool Initialized { + get { return fields.IsInitializedWithRespectTo(type); } + } + + public override void WriteTo(CodedOutputStream output) { + fields.WriteTo(output); + if (type.Options.MessageSetWireFormat) { + unknownFields.WriteAsMessageSetTo(output); + } else { + unknownFields.WriteTo(output); + } + } + + public override int SerializedSize { + get { + int size = memoizedSize; + if (size != -1) return size; + + size = fields.SerializedSize; + if (type.Options.MessageSetWireFormat) { + size += unknownFields.SerializedSizeAsMessageSet; + } else { + size += unknownFields.SerializedSize; + } + + memoizedSize = size; + return size; + } + } + + protected override IBuilder CreateBuilderForTypeImpl() { + return new Builder(type); + } + + /// <summary> + /// Verifies that the field is a field of this message. + /// </summary> + private void VerifyContainingType(FieldDescriptor field) { + if (field.ContainingType != type) { + throw new ArgumentException("FieldDescriptor does not match message type."); + } + } + + public class Builder : AbstractBuilder { + private readonly MessageDescriptor type; + private FieldSet fields; + private UnknownFieldSet unknownFields; + + internal Builder(MessageDescriptor type) { + this.type = type; + this.fields = FieldSet.CreateFieldSet(); + this.unknownFields = UnknownFieldSet.DefaultInstance; + } + + public override IBuilder Clear() { + fields.Clear(); + return this; + } + + public override IBuilder MergeFrom(IMessage other) { + if (other.DescriptorForType != type) { + throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type."); + } + fields.MergeFrom(other); + return this; + } + + protected override IMessage BuildImpl() { + if (!Initialized) { + throw new UninitializedMessageException(new DynamicMessage(type, fields, unknownFields)); + } + return BuildPartialImpl(); + } + + /// <summary> + /// Helper for DynamicMessage.ParseFrom() methods to call. Throws + /// InvalidProtocolBufferException + /// </summary> + /// <returns></returns> + internal DynamicMessage BuildParsed() { + if (!Initialized) { + throw new UninitializedMessageException(new DynamicMessage(type, fields, unknownFields)).AsInvalidProtocolBufferException(); + } + return (DynamicMessage) BuildPartialImpl(); + } + + protected override IMessage BuildPartialImpl() { + fields.MakeImmutable(); + DynamicMessage result = new DynamicMessage(type, fields, unknownFields); + fields = null; + unknownFields = null; + return result; + } + + protected override IBuilder CloneImpl() { + Builder result = new Builder(type); + result.fields.MergeFrom(fields); + return result; + } + + public override bool Initialized { + get { return fields.IsInitializedWithRespectTo(type); } + } + + protected override IBuilder MergeFromImpl(CodedInputStream input, ExtensionRegistry extensionRegistry) { + UnknownFieldSet.Builder unknownFieldsBuilder = UnknownFieldSet.CreateBuilder(unknownFields); + FieldSet.MergeFrom(input, unknownFieldsBuilder, extensionRegistry, this); + unknownFields = unknownFieldsBuilder.Build(); + return this; + } + + public override MessageDescriptor DescriptorForType { + get { return type; } + } + + protected override IMessage DefaultInstanceForTypeImpl { + get { return GetDefaultInstance(type); } + } + + public override IDictionary<FieldDescriptor, object> AllFields { + get { return fields.AllFields; } + } + + public override IBuilder CreateBuilderForField(FieldDescriptor field) { + VerifyContainingType(field); + if (field.MappedType != MappedType.Message) { + throw new ArgumentException("CreateBuilderForField is only valid for fields with message type."); + } + return new Builder(field.MessageType); + } + + public override bool HasField(FieldDescriptor field) { + VerifyContainingType(field); + return fields.HasField(field); + } + + public override object this[FieldDescriptor field, int index] { + get { + VerifyContainingType(field); + return fields[field, index]; + } + set { + VerifyContainingType(field); + fields[field, index] = value; + } + } + + public override object this[FieldDescriptor field] { + get { + VerifyContainingType(field); + object result = fields[field]; + if (result == null) { + result = GetDefaultInstance(field.MessageType); + } + return result; + } + set { + VerifyContainingType(field); + fields[field] = value; + } + } + + protected override IBuilder ClearFieldImpl(FieldDescriptor field) { + VerifyContainingType(field); + fields.ClearField(field); + return this; + } + + public override int GetRepeatedFieldCount(FieldDescriptor field) { + VerifyContainingType(field); + return fields.GetRepeatedFieldCount(field); + } + + protected override IBuilder AddRepeatedFieldImpl(FieldDescriptor field, object value) { + VerifyContainingType(field); + fields.AddRepeatedField(field, value); + return this; + } + + public override UnknownFieldSet UnknownFields { + get { + return unknownFields; + } + set { + unknownFields = value; + } + } + + /// <summary> + /// Verifies that the field is a field of this message. + /// </summary> + /// <param name="field"></param> + private void VerifyContainingType(FieldDescriptor field) { + if (field.ContainingType != type) { + throw new ArgumentException("FieldDescriptor does not match message type."); + } + } } } } diff --git a/csharp/ProtocolBuffers/ExtendableBuilder.cs b/csharp/ProtocolBuffers/ExtendableBuilder.cs new file mode 100644 index 00000000..3f5a8162 --- /dev/null +++ b/csharp/ProtocolBuffers/ExtendableBuilder.cs @@ -0,0 +1,147 @@ +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); + } + } + } +} diff --git a/csharp/ProtocolBuffers/ExtendableMessage.cs b/csharp/ProtocolBuffers/ExtendableMessage.cs index 872b15b6..0a2d0317 100644 --- a/csharp/ProtocolBuffers/ExtendableMessage.cs +++ b/csharp/ProtocolBuffers/ExtendableMessage.cs @@ -13,9 +13,16 @@ namespace Google.ProtocolBuffers { 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 + "\"."); + } + } } } diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs index 11611b64..15ea6253 100644 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs @@ -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) { diff --git a/csharp/ProtocolBuffers/GeneratedBuilder.cs b/csharp/ProtocolBuffers/GeneratedBuilder.cs index 4b3567d0..154aa05f 100644 --- a/csharp/ProtocolBuffers/GeneratedBuilder.cs +++ b/csharp/ProtocolBuffers/GeneratedBuilder.cs @@ -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,19 +190,21 @@ 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 diff --git a/csharp/ProtocolBuffers/GeneratedExtensionBase.cs b/csharp/ProtocolBuffers/GeneratedExtensionBase.cs index 40118388..2ab6d7a6 100644 --- a/csharp/ProtocolBuffers/GeneratedExtensionBase.cs +++ b/csharp/ProtocolBuffers/GeneratedExtensionBase.cs @@ -1,4 +1,5 @@ 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 diff --git a/csharp/ProtocolBuffers/GeneratedMessage.cs b/csharp/ProtocolBuffers/GeneratedMessage.cs index 2aa8e842..37fd2e6b 100644 --- a/csharp/ProtocolBuffers/GeneratedMessage.cs +++ b/csharp/ProtocolBuffers/GeneratedMessage.cs @@ -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); } } diff --git a/csharp/ProtocolBuffers/GeneratedRepeatException.cs b/csharp/ProtocolBuffers/GeneratedRepeatException.cs index d766d7f6..a38f5c1b 100644 --- a/csharp/ProtocolBuffers/GeneratedRepeatException.cs +++ b/csharp/ProtocolBuffers/GeneratedRepeatException.cs @@ -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); diff --git a/csharp/ProtocolBuffers/GeneratedSingleExtension.cs b/csharp/ProtocolBuffers/GeneratedSingleExtension.cs index 11805293..0adcd7ef 100644 --- a/csharp/ProtocolBuffers/GeneratedSingleExtension.cs +++ b/csharp/ProtocolBuffers/GeneratedSingleExtension.cs @@ -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) { diff --git a/csharp/ProtocolBuffers/IBuilder.cs b/csharp/ProtocolBuffers/IBuilder.cs index f847f217..b46842cd 100644 --- a/csharp/ProtocolBuffers/IBuilder.cs +++ b/csharp/ProtocolBuffers/IBuilder.cs @@ -51,6 +51,18 @@ namespace Google.ProtocolBuffers { 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"/> /// </summary> diff --git a/csharp/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/ProtocolBuffers/ProtocolBuffers.csproj index 3735adb7..6c97e8d1 100644 --- a/csharp/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/ProtocolBuffers/ProtocolBuffers.csproj @@ -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" /> diff --git a/csharp/ProtocolBuffers/TextGenerator.cs b/csharp/ProtocolBuffers/TextGenerator.cs index 724c1a8f..b5837504 100644 --- a/csharp/ProtocolBuffers/TextGenerator.cs +++ b/csharp/ProtocolBuffers/TextGenerator.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); |