Ejemplo n.º 1
0
        /// <summary>
        /// Implement the Clone method.s
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            DynamoDBList list = new DynamoDBList();

            foreach (DynamoDBEntry entry in this.Entries)
            {
                list.Add(entry.Clone() as DynamoDBEntry);
            }

            return(list);
        }
Ejemplo n.º 2
0
        public static DynamoDBList Create <T>(IEnumerable <T> items)
        {
            var list = new DynamoDBList();

            foreach (var item in items)
            {
                var entry = new UnconvertedDynamoDBEntry(item);
                list.Entries.Add(entry);
            }

            return(list);
        }
        private static bool TryToDynamoDBList(AttributeValue attributeValue, out DynamoDBList list)
        {
            list = null;

            if (attributeValue.IsSetL())
            {
                var items   = attributeValue.L;
                var entries = items.Select <AttributeValue, DynamoDBEntry>(AttributeValueToDynamoDBEntry);
                list = new DynamoDBList(entries);
            }

            return(list != null);
        }
Ejemplo n.º 4
0
        // Attempts to decode a particular DynamoDBEntry.
        // May throw exceptions, in particular if the data is not base64 encoded
        private static bool TryDecodeBase64(DynamoDBEntry entry, out DynamoDBEntry decodedEntry)
        {
            decodedEntry = null;

            // Convert string primitive (S) to binary primitive (B)
            var primitive = entry as Primitive;

            if (primitive != null && primitive.Type == DynamoDBEntryType.String)
            {
                // Decode the contents
                var    base64 = primitive.Value as string;
                byte[] bytes;
                if (!TryDecodeBase64(base64, out bytes))
                {
                    return(false);
                }

                // Store as binary primitive (B)
                decodedEntry = new Primitive(bytes);
                return(true);
            }

            // Convert string set (SS) to binary set (BS)
            var primitiveList = entry as PrimitiveList;

            if (primitiveList != null && primitiveList.Type == DynamoDBEntryType.String)
            {
                var decodedList = new PrimitiveList(DynamoDBEntryType.Binary);
                foreach (var item in primitiveList.Entries)
                {
                    // Attempt to decode
                    DynamoDBEntry decodedItem;
                    if (!TryDecodeBase64(item, out decodedItem))
                    {
                        return(false);
                    }

                    // The decoded item must be a Primitive
                    Primitive decodedPrimitive = decodedItem as Primitive;
                    if (decodedPrimitive == null)
                    {
                        return(false);
                    }

                    decodedList.Add(decodedPrimitive);
                }

                decodedEntry = decodedList;
                return(true);
            }

            // In a given list (L), convert every string primitive (S) to binary primitive (B)
            // Non-strings and strings that cannot be converted will be left as-is
            var dynamoDBList = entry as DynamoDBList;

            if (dynamoDBList != null)
            {
                var decodedList = new DynamoDBList();
                foreach (var item in dynamoDBList.Entries)
                {
                    DynamoDBEntry decodedItem;
                    if (!TryDecodeBase64(item, out decodedItem))
                    {
                        // if decoding could not succeed, store same item
                        decodedItem = item;
                    }

                    decodedList.Add(decodedItem);
                }

                decodedEntry = decodedList;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        // Returns a DynamoDB entry for the given JSON data
        private static DynamoDBEntry ToEntry(JsonData data, DynamoDBEntryConversion conversion)
        {
            if (data == null)
            {
                return(new DynamoDBNull());
            }

            if (data.IsObject)
            {
                var document = new Document();
                foreach (var propertyName in data.PropertyNames)
                {
                    var nestedData = data[propertyName];
                    var entry      = ToEntry(nestedData, conversion);
                    document[propertyName] = entry;
                }
                return(document);
            }

            if (data.IsArray)
            {
                var list = new DynamoDBList();
                for (int i = 0; i < data.Count; i++)
                {
                    var item  = data[i];
                    var entry = ToEntry(item, conversion);
                    list.Add(entry);
                }
                return(list);
            }

            if (data.IsBoolean)
            {
                return(new UnconvertedDynamoDBEntry((bool)data).Convert(conversion));
            }

            if (data.IsDouble)
            {
                return(new UnconvertedDynamoDBEntry((double)data).Convert(conversion));
            }

            if (data.IsInt)
            {
                return(new UnconvertedDynamoDBEntry((int)data).Convert(conversion));
            }

            if (data.IsUInt)
            {
                return(new UnconvertedDynamoDBEntry((uint)data).Convert(conversion));
            }

            if (data.IsLong)
            {
                return(new UnconvertedDynamoDBEntry((long)data).Convert(conversion));
            }

            if (data.IsULong)
            {
                return(new UnconvertedDynamoDBEntry((ulong)data).Convert(conversion));
            }

            if (data.IsString)
            {
                return(new UnconvertedDynamoDBEntry((string)data).Convert(conversion));
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                              "Unable to convert JSON data of type {0} to DynamoDB type.", data.GetJsonType()));
        }