private bool TryFromMap(Type targetType, Document map, DynamoDBFlatConfig flatConfig, out object output) { output = null; if (!Utils.CanInstantiate(targetType)) { return(false); } Type valueType; if (!IsSupportedDictionaryType(targetType, out valueType)) { return(false); } var dictionary = Utils.Instantiate(targetType); var idictionary = dictionary as IDictionary; var propertyStorage = new SimplePropertyStorage(valueType); foreach (var kvp in map) { var key = kvp.Key; var entry = kvp.Value; var item = FromDynamoDBEntry(propertyStorage, entry, flatConfig); idictionary.Add(key, item); } output = dictionary; return(true); }
private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, object value, DynamoDBFlatConfig flatConfig, bool canReturnScalarInsteadOfList) { if (value == null) { return(null); } var conversion = flatConfig.Conversion; var converter = propertyStorage.Converter; if (converter != null) { var entry = converter.ToEntry(value); if (entry != null) { entry = entry.ToConvertedEntry(conversion); } return(entry); } var type = propertyStorage.MemberType; if (canReturnScalarInsteadOfList) { DynamoDBEntry entry = null; if (TryToScalar(value, type, flatConfig, ref entry)) { return(entry); } } if (conversion.HasConverter(type)) { return(conversion.ConvertToEntry(type, value)); } else { Document map; if (TryToMap(value, type, flatConfig, out map)) { return(map); } DynamoDBList list; if (TryToList(value, type, flatConfig, out list)) { return(list); } return(SerializeToDocument(value, type, flatConfig)); } }
private bool TryToMap(object value, Type type, DynamoDBFlatConfig flatConfig, out Document output) { output = null; ITypeInfo typeWrapper; Type keyType, valueType; if (!IsSupportedDictionaryType(type, out typeWrapper, out keyType, out valueType)) { return(false); } var idictionary = value as IDictionary; if (idictionary == null) { return(false); } output = new Document(); SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType); foreach (object keyValue in idictionary.Keys) { object item = idictionary[keyValue]; string key = keyValue as string; if (key == null) { continue; } DynamoDBEntry entry; if (item == null) { entry = DynamoDBNull.Null; } else { entry = ToDynamoDBEntry(propertyStorage, item, flatConfig); } output[key] = entry; } return(true); }
private bool TryFromList(Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, out object output) { var targetTypeWrapper = TypeFactory.GetTypeInfo(targetType); if ((!Utils.ImplementsInterface(targetType, typeof(ICollection <>)) && !Utils.ImplementsInterface(targetType, typeof(IList))) || !Utils.CanInstantiate(targetType)) { output = null; return(false); } var elementType = targetTypeWrapper.GetGenericArguments()[0]; var collection = Utils.Instantiate(targetType); IList ilist = collection as IList; bool useIListInterface = ilist != null; var propertyStorage = new SimplePropertyStorage(elementType); MethodInfo collectionAdd = null; if (!useIListInterface) { collectionAdd = targetTypeWrapper.GetMethod("Add"); } foreach (DynamoDBEntry entry in list.Entries) { var item = FromDynamoDBEntry(propertyStorage, entry, flatConfig); if (useIListInterface) { ilist.Add(item); } else { collectionAdd.Invoke(collection, new object[] { item }); } } output = collection; return(true); }
// DynamoDBEntry <--> Object private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDBEntry entry, DynamoDBFlatConfig flatConfig) { var converter = propertyStorage.Converter; if (converter != null) { return(converter.FromEntry(entry)); } var conversion = flatConfig.Conversion; var targetType = propertyStorage.MemberType; if (conversion.HasConverter(targetType)) { return(conversion.ConvertFromEntry(targetType, entry)); } else { object output; Document document = entry as Document; if (document != null) { if (TryFromMap(targetType, document, flatConfig, out output)) { return(output); } return(DeserializeFromDocument(document, targetType, flatConfig)); } DynamoDBList list = entry as DynamoDBList; if (list != null && TryFromList(targetType, list, flatConfig, out output)) { return(output); } throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to convert DynamoDB entry [{0}] of type {1} to property {2} of type {3}", entry, entry.GetType().FullName, propertyStorage.PropertyName, propertyStorage.MemberType.FullName)); } }
private bool TryToList(object value, Type type, DynamoDBFlatConfig flatConfig, out DynamoDBList output) { var typeWrapper = TypeFactory.GetTypeInfo(type); if (!Utils.ImplementsInterface(type, typeof(ICollection <>))) { output = null; return(false); } IEnumerable enumerable = value as IEnumerable; // Strings are collections of chars, don't treat them as collections if (enumerable == null || value is string) { output = null; return(false); } Type elementType = typeWrapper.GetGenericArguments()[0]; SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType); output = new DynamoDBList(); foreach (var item in enumerable) { DynamoDBEntry entry; if (item == null) { entry = DynamoDBNull.Null; } else { entry = ToDynamoDBEntry(propertyStorage, item, flatConfig); } output.Add(entry); } return(true); }
private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, object value, DynamoDBFlatConfig flatConfig) { return(ToDynamoDBEntry(propertyStorage, value, flatConfig, canReturnScalarInsteadOfList: false)); }