Commit 487da48a authored by csharptest's avatar csharptest

Fix for repeated extensions.

parent 80824a51
......@@ -73,7 +73,8 @@ namespace Google.ProtocolBuffers.ProtoGen {
if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance) {
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) {
if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance) {
writer.WriteLine("[global::System.CLSCompliant(false)]");
......@@ -91,10 +92,11 @@ namespace Google.ProtocolBuffers.ProtoGen {
if (UseLiteRuntime) {
writer.WriteLine("{0}.{1} = ", scope, name);
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.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.Enum) ? "new EnumLiteMap<" + type + ">()" : "null");
writer.WriteLine("{0},", Descriptor.Index);
......
......@@ -66,7 +66,7 @@ namespace Google.ProtocolBuffers {
}
public class EnumLiteMap<TEnum> : IEnumLiteMap<IEnumLite>
where TEnum : struct, IComparable, IFormattable, IConvertible {
where TEnum : struct, IComparable, IFormattable {
struct EnumValue : IEnumLite, IComparable<IEnumLite> {
int _value;
......@@ -86,7 +86,7 @@ namespace Google.ProtocolBuffers {
public EnumLiteMap() {
items = new SortedList<int, IEnumLite>();
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) {
......
......@@ -126,7 +126,32 @@ namespace Google.ProtocolBuffers {
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
where TContainingType : IMessageLite {
......@@ -166,8 +191,8 @@ namespace Google.ProtocolBuffers {
false /* isRepeated */, false /* isPacked */)) {
}
/** For use by generated code only. */
public GeneratedExtensionLite(
/** Repeating fields: For use by generated code only. */
protected GeneratedExtensionLite(
TContainingType containingTypeDefaultInstance,
TExtensionType defaultValue,
IMessageLite messageDefaultInstance,
......@@ -231,48 +256,21 @@ namespace Google.ProtocolBuffers {
/// 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 {
public virtual object ToReflectionType(object value) {
return SingularToReflectionType(value);
}
}
/// <summary>
/// Like ToReflectionType(object) but for a single element.
/// </summary>
internal Object SingularToReflectionType(object value) {
public object SingularToReflectionType(object value) {
return descriptor.MappedType == MappedType.Enum
? descriptor.EnumType.FindValueByNumber((int)value)
: value;
}
public 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 {
public virtual object FromReflectionType(object value) {
return SingularFromReflectionType(value);
}
}
public object SingularFromReflectionType(object value) {
......
......@@ -32,6 +32,8 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos;
......@@ -81,8 +83,7 @@ namespace Google.ProtocolBuffers {
TestAllExtensionsLite message =
TestAllExtensionsLite.CreateBuilder()
.SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123)
#warning broken
// .AddExtension(UnitTestLiteProtoFile.RepeatedStringExtensionLite, "hello")
.AddExtension(UnitTestLiteProtoFile.RepeatedStringExtensionLite, "hello")
.SetExtension(UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite,
TestAllTypesLite.Types.NestedEnum.BAZ)
.SetExtension(UnitTestLiteProtoFile.OptionalNestedMessageExtensionLite,
......@@ -96,13 +97,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(123, (int)message2.GetExtension(
UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
#warning broken type infrence
//Assert.AreEqual(1, message2.GetExtensionCount(
// UnitTestLiteProtoFile.RepeatedStringExtensionLite));
//Assert.AreEqual(1, message2.GetExtension(
// UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0));
//Assert.AreEqual("hello", message2.GetExtension(
// UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0));
Assert.AreEqual(1, message2.GetExtensionCount(
UnitTestLiteProtoFile.RepeatedStringExtensionLite));
Assert.AreEqual(1, message2.GetExtension(
UnitTestLiteProtoFile.RepeatedStringExtensionLite).Count);
Assert.AreEqual("hello", message2.GetExtension(
UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0));
Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.BAZ, message2.GetExtension(
UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite));
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