Commit 487da48a authored by csharptest's avatar csharptest

Fix for repeated extensions.

parent 80824a51
...@@ -73,7 +73,8 @@ namespace Google.ProtocolBuffers.ProtoGen { ...@@ -73,7 +73,8 @@ namespace Google.ProtocolBuffers.ProtoGen {
if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance) { if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance) {
writer.WriteLine("[global::System.CLSCompliant(false)]"); writer.WriteLine("[global::System.CLSCompliant(false)]");
} }
writer.WriteLine("{0} static pb::GeneratedExtensionLite<{1}, {2}> {3};", ClassAccessLevel, extends, type, name); writer.WriteLine("{0} static pb::{4}<{1}, {2}> {3};", ClassAccessLevel, extends, type, name,
Descriptor.IsRepeated ? "GeneratedRepeatExtensionLite" : "GeneratedExtensionLite");
} else if (Descriptor.IsRepeated) { } else if (Descriptor.IsRepeated) {
if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance) { if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance) {
writer.WriteLine("[global::System.CLSCompliant(false)]"); writer.WriteLine("[global::System.CLSCompliant(false)]");
...@@ -91,10 +92,11 @@ namespace Google.ProtocolBuffers.ProtoGen { ...@@ -91,10 +92,11 @@ namespace Google.ProtocolBuffers.ProtoGen {
if (UseLiteRuntime) { if (UseLiteRuntime) {
writer.WriteLine("{0}.{1} = ", scope, name); writer.WriteLine("{0}.{1} = ", scope, name);
writer.Indent(); writer.Indent();
writer.WriteLine("new pb::GeneratedExtensionLite<{0}, {1}>(", extends, type); writer.WriteLine("new pb::{0}<{1}, {2}>(", Descriptor.IsRepeated ? "GeneratedRepeatExtensionLite" : "GeneratedExtensionLite", extends, type);
writer.Indent(); writer.Indent();
writer.WriteLine("{0}.DefaultInstance,", extends); writer.WriteLine("{0}.DefaultInstance,", extends);
writer.WriteLine("{0},", Descriptor.HasDefaultValue ? DefaultValue : IsNullableType ? "null" : "default(" + type + ")"); if(!Descriptor.IsRepeated)
writer.WriteLine("{0},", Descriptor.HasDefaultValue ? DefaultValue : IsNullableType ? "null" : "default(" + type + ")");
writer.WriteLine("{0},", (Descriptor.MappedType == MappedType.Message) ? type + ".DefaultInstance" : "null"); writer.WriteLine("{0},", (Descriptor.MappedType == MappedType.Message) ? type + ".DefaultInstance" : "null");
writer.WriteLine("{0},", (Descriptor.MappedType == MappedType.Enum) ? "new EnumLiteMap<" + type + ">()" : "null"); writer.WriteLine("{0},", (Descriptor.MappedType == MappedType.Enum) ? "new EnumLiteMap<" + type + ">()" : "null");
writer.WriteLine("{0},", Descriptor.Index); writer.WriteLine("{0},", Descriptor.Index);
......
...@@ -66,7 +66,7 @@ namespace Google.ProtocolBuffers { ...@@ -66,7 +66,7 @@ namespace Google.ProtocolBuffers {
} }
public class EnumLiteMap<TEnum> : IEnumLiteMap<IEnumLite> public class EnumLiteMap<TEnum> : IEnumLiteMap<IEnumLite>
where TEnum : struct, IComparable, IFormattable, IConvertible { where TEnum : struct, IComparable, IFormattable {
struct EnumValue : IEnumLite, IComparable<IEnumLite> { struct EnumValue : IEnumLite, IComparable<IEnumLite> {
int _value; int _value;
...@@ -86,7 +86,7 @@ namespace Google.ProtocolBuffers { ...@@ -86,7 +86,7 @@ namespace Google.ProtocolBuffers {
public EnumLiteMap() { public EnumLiteMap() {
items = new SortedList<int, IEnumLite>(); items = new SortedList<int, IEnumLite>();
foreach (TEnum evalue in Enum.GetValues(typeof(TEnum))) foreach (TEnum evalue in Enum.GetValues(typeof(TEnum)))
items.Add(evalue.ToInt32(CultureInfo.InvariantCulture), new EnumValue(evalue.ToInt32(CultureInfo.InvariantCulture))); items.Add(Convert.ToInt32(evalue), new EnumValue(Convert.ToInt32(evalue)));
} }
IEnumLite IEnumLiteMap.FindValueByNumber(int number) { IEnumLite IEnumLiteMap.FindValueByNumber(int number) {
......
...@@ -126,7 +126,32 @@ namespace Google.ProtocolBuffers { ...@@ -126,7 +126,32 @@ namespace Google.ProtocolBuffers {
return FieldNumber.CompareTo(other.FieldNumber); return FieldNumber.CompareTo(other.FieldNumber);
} }
} }
public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> : GeneratedExtensionLite<TContainingType, IList<TExtensionType>>
where TContainingType : IMessageLite {
public GeneratedRepeatExtensionLite(TContainingType containingTypeDefaultInstance,
IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number, FieldType type, bool isPacked) :
base(containingTypeDefaultInstance, new List<TExtensionType>(), messageDefaultInstance, enumTypeMap, number, type, isPacked) {
}
public override object ToReflectionType(object value) {
IList<TExtensionType> result = new List<TExtensionType>();
foreach (object element in (IEnumerable)value) {
result.Add((TExtensionType)SingularToReflectionType(element));
}
return result;
}
public override object FromReflectionType(object value) {
// Must convert the whole list.
List<TExtensionType> result = new List<TExtensionType>();
foreach (object element in (IEnumerable)value) {
result.Add((TExtensionType)SingularFromReflectionType(element));
}
return result;
}
}
public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite
where TContainingType : IMessageLite { where TContainingType : IMessageLite {
...@@ -166,8 +191,8 @@ namespace Google.ProtocolBuffers { ...@@ -166,8 +191,8 @@ namespace Google.ProtocolBuffers {
false /* isRepeated */, false /* isPacked */)) { false /* isRepeated */, false /* isPacked */)) {
} }
/** For use by generated code only. */ /** Repeating fields: For use by generated code only. */
public GeneratedExtensionLite( protected GeneratedExtensionLite(
TContainingType containingTypeDefaultInstance, TContainingType containingTypeDefaultInstance,
TExtensionType defaultValue, TExtensionType defaultValue,
IMessageLite messageDefaultInstance, IMessageLite messageDefaultInstance,
...@@ -231,48 +256,21 @@ namespace Google.ProtocolBuffers { ...@@ -231,48 +256,21 @@ namespace Google.ProtocolBuffers {
/// for enums use EnumValueDescriptors but the native accessors use /// for enums use EnumValueDescriptors but the native accessors use
/// the generated enum type. /// the generated enum type.
/// </summary> /// </summary>
public object ToReflectionType(object value) { public virtual 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); return SingularToReflectionType(value);
}
} }
/// <summary> /// <summary>
/// Like ToReflectionType(object) but for a single element. /// Like ToReflectionType(object) but for a single element.
/// </summary> /// </summary>
internal Object SingularToReflectionType(object value) { public object SingularToReflectionType(object value) {
return descriptor.MappedType == MappedType.Enum return descriptor.MappedType == MappedType.Enum
? descriptor.EnumType.FindValueByNumber((int)value) ? descriptor.EnumType.FindValueByNumber((int)value)
: value; : value;
} }
public object FromReflectionType(object value) { public virtual object FromReflectionType(object value) {
if (descriptor.IsRepeated) {
if (Descriptor.MappedType == MappedType.Message ||
Descriptor.MappedType == MappedType.Enum) {
// Must convert the whole list.
List<TExtensionType> result = new List<TExtensionType>();
foreach (object element in (IEnumerable)value) {
result.Add((TExtensionType)SingularFromReflectionType(element));
}
return result;
} else {
return value;
}
} else {
return SingularFromReflectionType(value); return SingularFromReflectionType(value);
}
} }
public object SingularFromReflectionType(object value) { public object SingularFromReflectionType(object value) {
......
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion #endregion
using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using Google.ProtocolBuffers.Descriptors; using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos; using Google.ProtocolBuffers.TestProtos;
...@@ -81,8 +83,7 @@ namespace Google.ProtocolBuffers { ...@@ -81,8 +83,7 @@ namespace Google.ProtocolBuffers {
TestAllExtensionsLite message = TestAllExtensionsLite message =
TestAllExtensionsLite.CreateBuilder() TestAllExtensionsLite.CreateBuilder()
.SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123) .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123)
#warning broken .AddExtension(UnitTestLiteProtoFile.RepeatedStringExtensionLite, "hello")
// .AddExtension(UnitTestLiteProtoFile.RepeatedStringExtensionLite, "hello")
.SetExtension(UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite, .SetExtension(UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite,
TestAllTypesLite.Types.NestedEnum.BAZ) TestAllTypesLite.Types.NestedEnum.BAZ)
.SetExtension(UnitTestLiteProtoFile.OptionalNestedMessageExtensionLite, .SetExtension(UnitTestLiteProtoFile.OptionalNestedMessageExtensionLite,
...@@ -96,13 +97,12 @@ namespace Google.ProtocolBuffers { ...@@ -96,13 +97,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(123, (int)message2.GetExtension( Assert.AreEqual(123, (int)message2.GetExtension(
UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
#warning broken type infrence Assert.AreEqual(1, message2.GetExtensionCount(
//Assert.AreEqual(1, message2.GetExtensionCount( UnitTestLiteProtoFile.RepeatedStringExtensionLite));
// UnitTestLiteProtoFile.RepeatedStringExtensionLite)); Assert.AreEqual(1, message2.GetExtension(
//Assert.AreEqual(1, message2.GetExtension( UnitTestLiteProtoFile.RepeatedStringExtensionLite).Count);
// UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0)); Assert.AreEqual("hello", message2.GetExtension(
//Assert.AreEqual("hello", message2.GetExtension( UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0));
// UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0));
Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.BAZ, message2.GetExtension( Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.BAZ, message2.GetExtension(
UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite)); UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite));
Assert.AreEqual(7, message2.GetExtension( Assert.AreEqual(7, message2.GetExtension(
......
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