| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace Azure.Data.Tables |
| | | 9 | | { |
| | | 10 | | internal static class TableEntityExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Returns a new Dictionary with the appropriate Odata type annotation for a given propertyName value pair. |
| | | 14 | | /// The default case is intentionally unhandled as this means that no type annotation for the specified type is |
| | | 15 | | /// This is because the type is naturally serialized in a way that the table service can interpret without hints |
| | | 16 | | /// </summary> |
| | | 17 | | internal static Dictionary<string, object> ToOdataAnnotatedDictionary<T>(this T entity) where T : class, ITableE |
| | | 18 | | { |
| | 1908 | 19 | | if (entity is IDictionary<string, object> dictEntity) |
| | | 20 | | { |
| | 840 | 21 | | return dictEntity.ToOdataAnnotatedDictionary(); |
| | | 22 | | } |
| | | 23 | | |
| | 1068 | 24 | | var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public); |
| | 1068 | 25 | | var annotatedDictionary = new Dictionary<string, object>(properties.Length * 2); |
| | | 26 | | |
| | 45120 | 27 | | foreach (var prop in properties) |
| | | 28 | | { |
| | 21492 | 29 | | annotatedDictionary[prop.Name] = prop.GetValue(entity); |
| | | 30 | | |
| | 21492 | 31 | | switch (annotatedDictionary[prop.Name]) |
| | | 32 | | { |
| | | 33 | | case byte[] _: |
| | 1320 | 34 | | annotatedDictionary[prop.Name.ToOdataTypeString()] = TableConstants.Odata.EdmBinary; |
| | 1320 | 35 | | break; |
| | | 36 | | case long _: |
| | 1896 | 37 | | annotatedDictionary[prop.Name.ToOdataTypeString()] = TableConstants.Odata.EdmInt64; |
| | | 38 | | // Int64 / long should be serialized as string. |
| | 1896 | 39 | | annotatedDictionary[prop.Name] = annotatedDictionary[prop.Name].ToString(); |
| | 1896 | 40 | | break; |
| | | 41 | | case double _: |
| | 2184 | 42 | | annotatedDictionary[prop.Name.ToOdataTypeString()] = TableConstants.Odata.EdmDouble; |
| | 2184 | 43 | | break; |
| | | 44 | | case Guid _: |
| | 1320 | 45 | | annotatedDictionary[prop.Name.ToOdataTypeString()] = TableConstants.Odata.EdmGuid; |
| | 1320 | 46 | | break; |
| | | 47 | | case DateTimeOffset _: |
| | 1320 | 48 | | annotatedDictionary[prop.Name.ToOdataTypeString()] = TableConstants.Odata.EdmDateTime; |
| | 1320 | 49 | | break; |
| | | 50 | | case DateTime _: |
| | 1320 | 51 | | annotatedDictionary[prop.Name.ToOdataTypeString()] = TableConstants.Odata.EdmDateTime; |
| | | 52 | | break; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 1068 | 56 | | return annotatedDictionary; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | } |