Пример #1
0
        // 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));
            }
        }
        public object FromEntry(DynamoDBEntry entry, Type targetType)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }

            object value;

            if (TryFromEntry(entry, targetType, out value))
            {
                return(value);
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                              "Unable to convert [{0}] of type {1} to {2}", entry, entry.GetType().FullName, targetType.FullName));
        }
        /// <summary>
        /// Convert the DynamoDBEntry to the specified type.
        /// </summary>
        /// <typeparam name="TOutput"></typeparam>
        /// <param name="entry"></param>
        /// <returns></returns>
        public TOutput ConvertFromEntry <TOutput>(DynamoDBEntry entry)
        {
            TOutput output;

            if (TryConvertFromEntry <TOutput>(entry, out output))
            {
                return(output);
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                              "Unable to convert [{0}] of type {1} to {2}", entry, entry.GetType().FullName, typeof(TOutput).FullName));
        }
Пример #4
0
        /// <summary>
        /// Convert DyanmoDBEntry to object using the V1 converter rules.
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public object FromEntry(DynamoDBEntry entry)
        {
            IEnumerable <object> items = null;

            var pl = entry as PrimitiveList;

            if (pl != null)
            {
                var primitives = pl.Entries;
                items = Conversion.ConvertFromEntries(elementType, primitives.Cast <DynamoDBEntry>());
            }

            var l = entry as DynamoDBList;

            if (l != null)
            {
                var entries = l.Entries;
                items = Conversion.ConvertFromEntries(elementType, entries);
            }

            object collection;

            if (items == null ||
                !Utils.ItemsToCollection(collectionType, items, out collection))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Unable to convert entry [{0}] of type {1} to {2}", entry, entry.GetType().FullName, collectionType.FullName));
            }

            return(collection);
        }