-
Notifications
You must be signed in to change notification settings - Fork 851
/
Utils.cs
383 lines (339 loc) · 14 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Amazon.Util.Internal;
using System.Globalization;
using System.Collections;
using Amazon.DynamoDBv2.DocumentModel;
namespace Amazon.DynamoDBv2.DataModel
{
#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Amazon.DynamoDBv2.Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)]
#endif
internal static class Utils
{
private static readonly Type[] EmptyTypes = new Type[0];
#region Type methods
private static readonly Type[] primitiveTypesArray = new Type[]
{
typeof(Boolean),
typeof(Byte),
typeof(Char),
typeof(DateTime),
typeof(Decimal),
typeof(Double),
typeof(int),
typeof(long),
typeof(SByte),
typeof(short),
typeof(Single),
typeof(String),
typeof(uint),
typeof(ulong),
typeof(ushort),
typeof(Guid),
typeof(byte[]),
typeof(MemoryStream),
typeof(Primitive)
};
public static readonly IEnumerable<Type> PrimitiveTypes = new HashSet<Type>(primitiveTypesArray);
public static bool IsPrimitive(Type type)
{
return PrimitiveTypes.Any(ti => type.IsAssignableFrom(ti));
}
public static bool IsPrimitive<T>()
{
return IsPrimitive(typeof(T));
}
public static void ValidatePrimitiveType(Type type)
{
if (!Utils.IsPrimitive(type))
throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture,
"{0} is not a supported Primitive type", type.FullName));
}
public static void ValidatePrimitiveType<T>()
{
ValidatePrimitiveType(typeof(T));
}
public static void ValidateVersionType(Type memberType)
{
if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
(memberType.IsAssignableFrom(typeof(Byte)) ||
memberType.IsAssignableFrom(typeof(SByte)) ||
memberType.IsAssignableFrom(typeof(int)) ||
memberType.IsAssignableFrom(typeof(uint)) ||
memberType.IsAssignableFrom(typeof(long)) ||
memberType.IsAssignableFrom(typeof(ulong)) ||
memberType.IsAssignableFrom(typeof(short)) ||
memberType.IsAssignableFrom(typeof(ushort))))
{
return;
}
throw new InvalidOperationException("Version property must be of primitive, numeric, integer, nullable type (e.g. int?, long?, byte?)");
}
public static Type GetPrimitiveElementType(Type collectionType)
{
var elementType = Utils.GetElementType(collectionType);
if (elementType != null)
{
Utils.ValidatePrimitiveType(elementType);
return elementType;
}
throw new InvalidOperationException("Unable to determine element type");
}
public static Type GetElementType(Type collectionType)
{
var elementType = collectionType.GetElementType();
if (elementType == null)
{
var genericArguments = collectionType.GetGenericArguments();
if (genericArguments != null && genericArguments.Length == 1)
elementType = genericArguments[0];
}
// elementType may be null at this point, meaning that the collectionType isn't a collectionType
return elementType;
}
public static bool ItemsToCollection(Type targetType, IEnumerable<object> items, out object result)
{
return targetType.IsArray ?
ItemsToArray(targetType, items, out result): //targetType is Array
ItemsToIList(targetType, items, out result); //targetType is IList or has Add method.
}
private static bool ItemsToIList(Type targetType, IEnumerable<object> items, out object result)
{
result = Utils.Instantiate(targetType);
var ilist = result as IList;
if (ilist != null)
{
foreach (var item in items)
ilist.Add(item);
return true;
}
var addMethod = targetType.GetMethod("Add");
if (addMethod != null)
{
foreach (var item in items)
addMethod.Invoke(result, new object[] { item });
return true;
}
result = null;
return false;
}
private static bool ItemsToArray(Type targetType, IEnumerable<object> items, out object result)
{
var itemlist = items.ToList();
var array = (Array)InstantiateArray(targetType, itemlist.Count);
for (int i = 0; i < itemlist.Count; i++)
{
var item = itemlist[i];
array.SetValue(item, i);
}
result = array;
return true;
}
#endregion
#region Attribute methods
public static DynamoDBTableAttribute GetTableAttribute(Type targetType)
{
DynamoDBTableAttribute tableAttribute = GetAttribute(targetType) as DynamoDBTableAttribute;
if (tableAttribute == null)
return null;
return tableAttribute;
}
public static DynamoDBAttribute GetAttribute(Type targetType)
{
if (targetType == null) throw new ArgumentNullException("targetType");
object[] attributes = targetType.GetCustomAttributes(typeof(DynamoDBAttribute), true);
return GetSingleDDBAttribute(attributes);
}
public static DynamoDBAttribute GetAttribute(MemberInfo targetMemberInfo)
{
object[] attributes = GetAttributeObjects(targetMemberInfo);
return GetSingleDDBAttribute(attributes);
}
public static List<DynamoDBAttribute> GetAttributes(MemberInfo targetMemberInfo)
{
object[] attObjects = GetAttributeObjects(targetMemberInfo) ?? new object[0];
var attributes = new List<DynamoDBAttribute>();
foreach (var attObj in attObjects)
{
var attribute = attObj as DynamoDBAttribute;
if (attribute != null)
attributes.Add(attribute);
}
return attributes;
}
private static DynamoDBAttribute GetSingleDDBAttribute(object[] attributes)
{
if (attributes.Length == 0)
return null;
if (attributes.Length == 1)
return (attributes[0] as DynamoDBAttribute);
throw new InvalidOperationException("Cannot have multiple DynamoDBAttributes on a single member");
}
private static object[] GetAttributeObjects(MemberInfo targetMemberInfo)
{
if (targetMemberInfo == null) throw new ArgumentNullException("targetMemberInfo");
#if NETSTANDARD
object[] attributes = CustomAttributeExtensions.GetCustomAttributes(targetMemberInfo, typeof(DynamoDBAttribute), true).ToArray<object>();
#else
object[] attributes = Attribute.GetCustomAttributes(targetMemberInfo, typeof(DynamoDBAttribute), true).ToArray<object>();
#endif
return attributes;
}
#endregion
#region Non-DynamoDB utilities
public static string ToLowerCamelCase(string value)
{
if (string.IsNullOrEmpty(value) || char.IsLower(value[0])) return value;
StringBuilder sb = new StringBuilder(value);
sb[0] = char.ToLowerInvariant(sb[0]);
return sb.ToString();
}
private static Type[][] validConstructorInputs = new Type[][]
{
EmptyTypes,
};
private static Type[][] validArrayConstructorInputs = new Type[][]
{
//supports one dimension Array only
new Type[] { typeof(int) }
};
private static Type[][] validConverterConstructorInputs = new Type[][]
{
EmptyTypes,
new Type[] { typeof(DynamoDBContext) }
};
public static object InstantiateConverter(Type objectType, IDynamoDBContext context)
{
return InstantiateHelper(objectType, validConverterConstructorInputs, new object[] { context });
}
public static object InstantiateArray(Type objectType,int length)
{
return InstantiateHelper(objectType, validArrayConstructorInputs, new object[] { length });
}
public static object Instantiate(Type objectType)
{
return InstantiateHelper(objectType, validConstructorInputs, null);
}
private static object InstantiateHelper(Type objectType, Type[][] validConstructorInputs, object[] optionalInput = null)
{
if (objectType == null)
throw new ArgumentNullException("objectType");
if (!CanInstantiateHelper(objectType, validConstructorInputs))
throw new InvalidOperationException("Cannot instantiate type " + objectType.FullName);
var constructors = GetConstructors(objectType, validConstructorInputs).ToList();
if (constructors != null && constructors.Count > 0)
{
foreach (var constructor in constructors)
{
var inputs = constructor.GetParameters();
object[] constructorParameters = inputs.Length == 0 ?
null : optionalInput;
object instance = constructor.Invoke(constructorParameters);
return instance;
}
}
throw new InvalidOperationException("Unable to find valid constructor for type " + objectType.FullName);
}
private static IEnumerable<ConstructorInfo> GetConstructors(Type typeInfo, Type[][] validConstructorInputs)
{
foreach(var inputTypes in validConstructorInputs)
{
var constructor = typeInfo.GetConstructor(inputTypes);
if (constructor != null)
yield return constructor;
}
}
public static bool CanInstantiate(Type objectType)
{
return CanInstantiateHelper(objectType, validConstructorInputs);
}
public static bool CanInstantiateArray(Type objectType)
{
return objectType.IsArray && CanInstantiateHelper(objectType, validArrayConstructorInputs);
}
public static bool CanInstantiateConverter(Type objectType)
{
return CanInstantiateHelper(objectType, validConverterConstructorInputs);
}
private static bool CanInstantiateHelper(Type objectType, Type[][] validConstructorInputs)
{
var objectTypeWrapper = objectType;
bool candidate =
//objectType.IsPublic &&
objectTypeWrapper.IsClass &&
!objectTypeWrapper.IsInterface &&
!objectTypeWrapper.IsAbstract &&
!objectTypeWrapper.IsGenericTypeDefinition &&
!objectTypeWrapper.ContainsGenericParameters;
if (!candidate)
return false;
// check valid constructor inputs
var constructors = GetConstructors(objectTypeWrapper, validConstructorInputs).ToList();
if (constructors.Count == 0)
return false;
return true;
}
public static Type GetType(MemberInfo member)
{
var pi = member as PropertyInfo;
var fi = member as FieldInfo;
if (pi == null && fi == null)
throw new ArgumentOutOfRangeException("member", "member must be of type PropertyInfo or FieldInfo");
return (pi != null ? pi.PropertyType : fi.FieldType);
}
public static bool IsReadWrite(MemberInfo member)
{
PropertyInfo property = member as PropertyInfo;
FieldInfo field = member as FieldInfo;
if (property != null)
{
return (property.CanRead && property.CanWrite);
}
else if (field != null)
{
return (field.IsPublic && !field.IsLiteral && !field.IsInitOnly);
}
else
{
throw new ArgumentOutOfRangeException("member", "Member must be FieldInfo or PropertyInfo");
}
}
public static bool ImplementsInterface(Type targetType, Type interfaceType)
{
if (!interfaceType.IsInterface)
throw new ArgumentOutOfRangeException("interfaceType", "Type is not an interface");
foreach (var inter in targetType.GetInterfaces())
{
if (InternalSDKUtils.AreTypesEqual(inter, interfaceType))
return true;
if (inter.IsGenericTypeDefinition && inter.IsGenericType)
{
var generic = inter.GetGenericTypeDefinition();
if (generic == interfaceType)
return true;
}
}
return false;
}
#endregion
}
}