| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace Azure.Data.Tables.Queryable |
| | 7 | | { |
| | 8 | | internal static class ClientConvert |
| | 9 | | { |
| 2 | 10 | | private static readonly Type[] s_knownTypes = CreateKnownPrimitives(); |
| | 11 | |
|
| | 12 | |
|
| | 13 | | internal enum StorageType |
| | 14 | | { |
| | 15 | | Boolean, |
| | 16 | | Byte, |
| | 17 | | ByteArray, |
| | 18 | | Char, |
| | 19 | | CharArray, |
| | 20 | | DateTime, |
| | 21 | | DateTimeOffset, |
| | 22 | | Decimal, |
| | 23 | | Double, |
| | 24 | | Guid, |
| | 25 | | Int16, |
| | 26 | | Int32, |
| | 27 | | Int64, |
| | 28 | | Single, |
| | 29 | | String, |
| | 30 | | SByte, |
| | 31 | | TimeSpan, |
| | 32 | | Type, |
| | 33 | | UInt16, |
| | 34 | | UInt32, |
| | 35 | | UInt64, |
| | 36 | | Uri, |
| | 37 | | XDocument, |
| | 38 | | XElement, |
| | 39 | | Binary, |
| | 40 | | } |
| | 41 | |
|
| | 42 | | internal static bool IsBinaryValue(object value) |
| | 43 | | { |
| 808 | 44 | | return StorageType.Binary == (StorageType)IndexOfStorage(value.GetType()); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | internal static bool TryKeyBinaryToString(object binaryValue, out string result) |
| | 48 | | { |
| | 49 | | const System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.Public | System.Reflection.Bindi |
| 0 | 50 | | byte[] bytes = (byte[])binaryValue.GetType().InvokeMember("ToArray", Flags, null, binaryValue, null, null /* |
| 0 | 51 | | return WebConvert.TryKeyPrimitiveToString(bytes, out result); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | internal static bool TryKeyPrimitiveToString(object value, out string result) |
| | 55 | | { |
| 808 | 56 | | if (IsBinaryValue(value)) |
| | 57 | | { |
| 0 | 58 | | return TryKeyBinaryToString(value, out result); |
| | 59 | | } |
| | 60 | | // Support DateTimeOffset |
| 808 | 61 | | if (value is DateTimeOffset) |
| | 62 | | { |
| 52 | 63 | | value = ((DateTimeOffset)value).UtcDateTime; |
| | 64 | | } |
| 756 | 65 | | else if (value is DateTimeOffset?) |
| | 66 | | { |
| 0 | 67 | | value = ((DateTimeOffset?)value).Value.UtcDateTime; |
| | 68 | | } |
| | 69 | |
|
| 808 | 70 | | return WebConvert.TryKeyPrimitiveToString(value, out result); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private static Type[] CreateKnownPrimitives() |
| | 74 | | { |
| 2 | 75 | | Type[] types = new Type[1 + (int)StorageType.Binary]; |
| 2 | 76 | | types[(int)StorageType.Boolean] = typeof(bool); |
| 2 | 77 | | types[(int)StorageType.Byte] = typeof(byte); |
| 2 | 78 | | types[(int)StorageType.ByteArray] = typeof(byte[]); |
| 2 | 79 | | types[(int)StorageType.Char] = typeof(char); |
| 2 | 80 | | types[(int)StorageType.CharArray] = typeof(char[]); |
| 2 | 81 | | types[(int)StorageType.DateTime] = typeof(DateTime); |
| 2 | 82 | | types[(int)StorageType.DateTimeOffset] = typeof(DateTimeOffset); |
| 2 | 83 | | types[(int)StorageType.Decimal] = typeof(decimal); |
| 2 | 84 | | types[(int)StorageType.Double] = typeof(double); |
| 2 | 85 | | types[(int)StorageType.Guid] = typeof(Guid); |
| 2 | 86 | | types[(int)StorageType.Int16] = typeof(short); |
| 2 | 87 | | types[(int)StorageType.Int32] = typeof(int); |
| 2 | 88 | | types[(int)StorageType.Int64] = typeof(long); |
| 2 | 89 | | types[(int)StorageType.Single] = typeof(float); |
| 2 | 90 | | types[(int)StorageType.String] = typeof(string); |
| 2 | 91 | | types[(int)StorageType.SByte] = typeof(sbyte); |
| 2 | 92 | | types[(int)StorageType.TimeSpan] = typeof(TimeSpan); |
| 2 | 93 | | types[(int)StorageType.Type] = typeof(Type); |
| 2 | 94 | | types[(int)StorageType.UInt16] = typeof(ushort); |
| 2 | 95 | | types[(int)StorageType.UInt32] = typeof(uint); |
| 2 | 96 | | types[(int)StorageType.UInt64] = typeof(ulong); |
| 2 | 97 | | types[(int)StorageType.Uri] = typeof(Uri); |
| 2 | 98 | | types[(int)StorageType.XDocument] = typeof(System.Xml.Linq.XDocument); |
| 2 | 99 | | types[(int)StorageType.XElement] = typeof(System.Xml.Linq.XElement); |
| 2 | 100 | | types[(int)StorageType.Binary] = null; |
| 2 | 101 | | return types; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | private static int IndexOfStorage(Type type) |
| | 105 | | { |
| 808 | 106 | | int index = ClientConvert.IndexOfReference(ClientConvert.s_knownTypes, type); |
| 808 | 107 | | return index; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | internal static int IndexOfReference<T>(T[] array, T value) where T : class |
| | 111 | | { |
| 18344 | 112 | | for (int i = 0; i < array.Length; ++i) |
| | 113 | | { |
| 9172 | 114 | | if (object.ReferenceEquals(array[i], value)) |
| | 115 | | { |
| 808 | 116 | | return i; |
| | 117 | | } |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | return -1; |
| | 121 | | } |
| | 122 | | } |
| | 123 | | } |