csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. All rights reserved. |
| 4 | // http://github.com/jskeet/dotnet-protobufs/ |
| 5 | // Original C++/Java/Python code: |
| 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
| 11 | // |
| 12 | // * Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // * Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following disclaimer |
| 16 | // in the documentation and/or other materials provided with the |
| 17 | // distribution. |
| 18 | // * Neither the name of Google Inc. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived from |
| 20 | // this software without specific prior written permission. |
| 21 | // |
| 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | #endregion |
| 34 | |
| 35 | using System; |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 36 | using System.Collections; |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 37 | using System.Collections.Generic; |
| 38 | using Google.ProtocolBuffers.Collections; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 39 | using Google.ProtocolBuffers.Descriptors; |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 40 | |
| 41 | namespace Google.ProtocolBuffers { |
| 42 | |
| 43 | public interface IGeneratedExtensionLite { |
| 44 | int Number { get; } |
| 45 | object ContainingType { get; } |
| 46 | IMessageLite MessageDefaultInstance { get; } |
| 47 | } |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 48 | |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 49 | public class ExtensionDescriptorLite : IFieldDescriptorLite { |
| 50 | /// <summary> |
| 51 | /// Immutable mapping from field type to mapped type. Built using the attributes on |
| 52 | /// FieldType values. |
| 53 | /// </summary> |
| 54 | public static readonly IDictionary<FieldType, MappedType> FieldTypeToMappedTypeMap = MapFieldTypes(); |
| 55 | |
| 56 | private static IDictionary<FieldType, MappedType> MapFieldTypes() { |
| 57 | var map = new Dictionary<FieldType, MappedType>(); |
| 58 | foreach (System.Reflection.FieldInfo field in typeof(FieldType).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)) { |
| 59 | FieldType fieldType = (FieldType)field.GetValue(null); |
| 60 | FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0]; |
| 61 | map[fieldType] = mapping.MappedType; |
| 62 | } |
| 63 | return Dictionaries.AsReadOnly(map); |
| 64 | } |
| 65 | |
| 66 | private readonly IEnumLiteMap enumTypeMap; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 67 | private readonly int number; |
| 68 | private readonly FieldType type; |
| 69 | private readonly bool isRepeated; |
| 70 | private readonly bool isPacked; |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 71 | private readonly MappedType mapType; |
| 72 | private readonly object defaultValue; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 73 | |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 74 | public ExtensionDescriptorLite(IEnumLiteMap enumTypeMap, int number, FieldType type, object defaultValue, bool isRepeated, bool isPacked) { |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 75 | this.enumTypeMap = enumTypeMap; |
| 76 | this.number = number; |
| 77 | this.type = type; |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 78 | this.mapType = FieldTypeToMappedTypeMap[type]; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 79 | this.isRepeated = isRepeated; |
| 80 | this.isPacked = isPacked; |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 81 | this.defaultValue = defaultValue; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 82 | } |
| 83 | |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 84 | public bool IsRepeated { |
| 85 | get { return isRepeated; } |
| 86 | } |
| 87 | |
| 88 | public bool IsRequired { |
| 89 | get { return false; } |
| 90 | } |
| 91 | |
| 92 | public bool IsPacked { |
| 93 | get { return isPacked; } |
| 94 | } |
| 95 | |
| 96 | public bool IsExtension { |
| 97 | get { return true; } |
| 98 | } |
| 99 | |
| 100 | #warning ToDo - Discover the meaning and purpose of this durring serialization and return the correct value |
| 101 | public bool MessageSetWireFormat { |
| 102 | get { return true; } |
| 103 | } |
| 104 | |
| 105 | public int FieldNumber { |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 106 | get { return number; } |
| 107 | } |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 108 | |
| 109 | public IEnumLiteMap EnumType { |
| 110 | get { return enumTypeMap; } |
| 111 | } |
| 112 | |
| 113 | public FieldType FieldType { |
| 114 | get { return type; } |
| 115 | } |
| 116 | |
| 117 | public MappedType MappedType { |
| 118 | get { return mapType; } |
| 119 | } |
| 120 | |
| 121 | public object DefaultValue { |
| 122 | get { return defaultValue; } |
| 123 | } |
| 124 | |
| 125 | public int CompareTo(IFieldDescriptorLite other) { |
| 126 | return FieldNumber.CompareTo(other.FieldNumber); |
| 127 | } |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 128 | } |
csharptest | 487da48 | 2010-11-07 18:49:42 -0600 | [diff] [blame^] | 129 | |
| 130 | public class GeneratedRepeatExtensionLite<TContainingType, TExtensionType> : GeneratedExtensionLite<TContainingType, IList<TExtensionType>> |
| 131 | where TContainingType : IMessageLite { |
| 132 | public GeneratedRepeatExtensionLite(TContainingType containingTypeDefaultInstance, |
| 133 | IMessageLite messageDefaultInstance, IEnumLiteMap enumTypeMap, int number, FieldType type, bool isPacked) : |
| 134 | base(containingTypeDefaultInstance, new List<TExtensionType>(), messageDefaultInstance, enumTypeMap, number, type, isPacked) { |
| 135 | } |
| 136 | |
| 137 | public override object ToReflectionType(object value) { |
| 138 | IList<TExtensionType> result = new List<TExtensionType>(); |
| 139 | foreach (object element in (IEnumerable)value) { |
| 140 | result.Add((TExtensionType)SingularToReflectionType(element)); |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | public override object FromReflectionType(object value) { |
| 146 | // Must convert the whole list. |
| 147 | List<TExtensionType> result = new List<TExtensionType>(); |
| 148 | foreach (object element in (IEnumerable)value) { |
| 149 | result.Add((TExtensionType)SingularFromReflectionType(element)); |
| 150 | } |
| 151 | return result; |
| 152 | } |
| 153 | } |
| 154 | |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 155 | public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite |
| 156 | where TContainingType : IMessageLite { |
| 157 | |
| 158 | private readonly TContainingType containingTypeDefaultInstance; |
| 159 | private readonly TExtensionType defaultValue; |
| 160 | private readonly IMessageLite messageDefaultInstance; |
| 161 | private readonly ExtensionDescriptorLite descriptor; |
| 162 | |
| 163 | // We can't always initialize a GeneratedExtension when we first construct |
| 164 | // it due to initialization order difficulties (namely, the default |
| 165 | // instances may not have been constructed yet). So, we construct an |
| 166 | // uninitialized GeneratedExtension once, then call internalInit() on it |
| 167 | // later. Generated code will always call internalInit() on all extensions |
| 168 | // as part of the static initialization code, and internalInit() throws an |
| 169 | // exception if called more than once, so this method is useless to users. |
| 170 | protected GeneratedExtensionLite( |
| 171 | TContainingType containingTypeDefaultInstance, |
| 172 | TExtensionType defaultValue, |
| 173 | IMessageLite messageDefaultInstance, |
| 174 | ExtensionDescriptorLite descriptor) { |
| 175 | this.containingTypeDefaultInstance = containingTypeDefaultInstance; |
| 176 | this.messageDefaultInstance = messageDefaultInstance; |
| 177 | this.defaultValue = defaultValue; |
| 178 | this.descriptor = descriptor; |
| 179 | } |
| 180 | |
| 181 | /** For use by generated code only. */ |
| 182 | public GeneratedExtensionLite( |
| 183 | TContainingType containingTypeDefaultInstance, |
| 184 | TExtensionType defaultValue, |
| 185 | IMessageLite messageDefaultInstance, |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 186 | IEnumLiteMap enumTypeMap, |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 187 | int number, |
| 188 | FieldType type) |
| 189 | : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance, |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 190 | new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue, |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 191 | false /* isRepeated */, false /* isPacked */)) { |
| 192 | } |
| 193 | |
csharptest | 487da48 | 2010-11-07 18:49:42 -0600 | [diff] [blame^] | 194 | /** Repeating fields: For use by generated code only. */ |
| 195 | protected GeneratedExtensionLite( |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 196 | TContainingType containingTypeDefaultInstance, |
| 197 | TExtensionType defaultValue, |
| 198 | IMessageLite messageDefaultInstance, |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 199 | IEnumLiteMap enumTypeMap, |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 200 | int number, |
| 201 | FieldType type, |
| 202 | bool isPacked) |
| 203 | : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance, |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 204 | new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue, |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 205 | true /* isRepeated */, isPacked)) { |
| 206 | } |
| 207 | |
| 208 | /// <summary> |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 209 | /// Returns information about this extension |
| 210 | /// </summary> |
| 211 | public IFieldDescriptorLite Descriptor { |
| 212 | get { return descriptor; } |
| 213 | } |
| 214 | |
| 215 | /// <summary> |
| 216 | /// Returns the default value for this extension |
| 217 | /// </summary> |
| 218 | public TExtensionType DefaultValue { |
| 219 | get { return defaultValue; } |
| 220 | } |
| 221 | |
| 222 | /// <summary> |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 223 | /// used for the extension registry |
| 224 | /// </summary> |
| 225 | object IGeneratedExtensionLite.ContainingType { |
| 226 | get { return ContainingTypeDefaultInstance; } |
| 227 | } |
| 228 | /** |
| 229 | * Default instance of the type being extended, used to identify that type. |
| 230 | */ |
| 231 | public TContainingType ContainingTypeDefaultInstance { |
| 232 | get { |
| 233 | return containingTypeDefaultInstance; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** Get the field number. */ |
| 238 | public int Number { |
| 239 | get { |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 240 | return descriptor.FieldNumber; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | /** |
| 244 | * If the extension is an embedded message, this is the default instance of |
| 245 | * that type. |
| 246 | */ |
| 247 | public IMessageLite MessageDefaultInstance { |
| 248 | get { |
| 249 | return messageDefaultInstance; |
| 250 | } |
| 251 | } |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 252 | |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 253 | /// <summary> |
| 254 | /// Converts from the type used by the native accessors to the type |
| 255 | /// used by reflection accessors. For example, the reflection accessors |
| 256 | /// for enums use EnumValueDescriptors but the native accessors use |
| 257 | /// the generated enum type. |
| 258 | /// </summary> |
csharptest | 487da48 | 2010-11-07 18:49:42 -0600 | [diff] [blame^] | 259 | public virtual object ToReflectionType(object value) { |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 260 | return SingularToReflectionType(value); |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /// <summary> |
| 264 | /// Like ToReflectionType(object) but for a single element. |
| 265 | /// </summary> |
csharptest | 487da48 | 2010-11-07 18:49:42 -0600 | [diff] [blame^] | 266 | public object SingularToReflectionType(object value) { |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 267 | return descriptor.MappedType == MappedType.Enum |
| 268 | ? descriptor.EnumType.FindValueByNumber((int)value) |
| 269 | : value; |
| 270 | } |
| 271 | |
csharptest | 487da48 | 2010-11-07 18:49:42 -0600 | [diff] [blame^] | 272 | public virtual object FromReflectionType(object value) { |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 273 | return SingularFromReflectionType(value); |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 274 | } |
| 275 | |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 276 | public object SingularFromReflectionType(object value) { |
csharptest | 80824a5 | 2010-11-07 17:25:47 -0600 | [diff] [blame] | 277 | switch (Descriptor.MappedType) { |
| 278 | case MappedType.Message: |
| 279 | if (value is TExtensionType) { |
| 280 | return value; |
| 281 | } else { |
| 282 | // It seems the copy of the embedded message stored inside the |
| 283 | // extended message is not of the exact type the user was |
| 284 | // expecting. This can happen if a user defines a |
| 285 | // GeneratedExtension manually and gives it a different type. |
| 286 | // This should not happen in normal use. But, to be nice, we'll |
| 287 | // copy the message to whatever type the caller was expecting. |
| 288 | return MessageDefaultInstance.WeakCreateBuilderForType() |
| 289 | .WeakMergeFrom((IMessageLite)value).WeakBuild(); |
| 290 | } |
| 291 | case MappedType.Enum: |
| 292 | // Just return a boxed int - that can be unboxed to the enum |
| 293 | IEnumLite enumValue = (IEnumLite)value; |
| 294 | return enumValue.Number; |
| 295 | default: |
| 296 | return value; |
| 297 | } |
csharptest | 980ba8d | 2010-11-07 16:30:39 -0600 | [diff] [blame] | 298 | } |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame] | 299 | } |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 300 | } |