diff options
author | csharptest <roger@csharptest.net> | 2010-11-07 17:25:47 -0600 |
---|---|---|
committer | csharptest <roger@csharptest.net> | 2010-11-07 17:25:47 -0600 |
commit | 80824a51c2730cad30395cf8c54b2a44546cd744 (patch) | |
tree | 1f523a817bd0059554fe1ee2690b4ea670de59b7 /src/ProtocolBuffers/GeneratedExtensionLite.cs | |
parent | 980ba8dcd470ddb964a84da1317028dd81c1d60a (diff) | |
download | protobuf-80824a51c2730cad30395cf8c54b2a44546cd744.tar.gz protobuf-80824a51c2730cad30395cf8c54b2a44546cd744.tar.bz2 protobuf-80824a51c2730cad30395cf8c54b2a44546cd744.zip |
First Lite tests are passing.
Diffstat (limited to 'src/ProtocolBuffers/GeneratedExtensionLite.cs')
-rw-r--r-- | src/ProtocolBuffers/GeneratedExtensionLite.cs | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/src/ProtocolBuffers/GeneratedExtensionLite.cs b/src/ProtocolBuffers/GeneratedExtensionLite.cs index e27dff72..c2ba5627 100644 --- a/src/ProtocolBuffers/GeneratedExtensionLite.cs +++ b/src/ProtocolBuffers/GeneratedExtensionLite.cs @@ -33,6 +33,7 @@ #endregion using System; +using System.Collections; using System.Collections.Generic; using Google.ProtocolBuffers.Collections; using Google.ProtocolBuffers.Descriptors; @@ -224,8 +225,78 @@ 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 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); + } + } + public object SingularFromReflectionType(object value) { - return value; + switch (Descriptor.MappedType) { + case MappedType.Message: + if (value is TExtensionType) { + return value; + } else { + // It seems the copy of the embedded message stored inside the + // extended message is not of the exact type the user was + // expecting. This can happen if a user defines a + // GeneratedExtension manually and gives it a different type. + // This should not happen in normal use. But, to be nice, we'll + // copy the message to whatever type the caller was expecting. + return MessageDefaultInstance.WeakCreateBuilderForType() + .WeakMergeFrom((IMessageLite)value).WeakBuild(); + } + case MappedType.Enum: + // Just return a boxed int - that can be unboxed to the enum + IEnumLite enumValue = (IEnumLite)value; + return enumValue.Number; + default: + return value; + } } } }
\ No newline at end of file |