private static bool TryToPrimitiveList(AttributeValue attributeValue, out PrimitiveList primitiveList)
        {
            primitiveList = null;
            Primitive primitive;

            if (attributeValue.IsSetSS())
            {
                primitiveList = new PrimitiveList(DynamoDBEntryType.String);
                foreach (string item in attributeValue.SS)
                {
                    primitive = new Primitive(item);
                    primitiveList.Add(primitive);
                }
            }
            else if (attributeValue.IsSetNS())
            {
                primitiveList = new PrimitiveList(DynamoDBEntryType.Numeric);
                foreach (string item in attributeValue.NS)
                {
                    primitive = new Primitive(item, true);
                    primitiveList.Add(primitive);
                }
            }
            else if (attributeValue.IsSetBS())
            {
                primitiveList = new PrimitiveList(DynamoDBEntryType.Binary);
                foreach (MemoryStream item in attributeValue.BS)
                {
                    primitive = new Primitive(item);
                    primitiveList.Add(primitive);
                }
            }

            return(primitiveList != null);
        }
Beispiel #2
0
        internal static DynamoDBEntry AttributeValueToDynamoDBEntry(AttributeValue attributeValue)
        {
            Primitive primitive = null;

            if (attributeValue.S != null)
            {
                primitive = new Primitive(attributeValue.S);
            }
            else if (attributeValue.N != null)
            {
                primitive = new Primitive(attributeValue.N, true);
            }
            else if (attributeValue.B != null)
            {
                primitive = new Primitive(attributeValue.B);
            }
            if (primitive != null)
            {
                return(primitive);
            }

            PrimitiveList primitiveList = null;

            if (attributeValue.SS != null && attributeValue.SS.Count != 0)
            {
                primitiveList = new PrimitiveList(DynamoDBEntryType.String);
                foreach (string item in attributeValue.SS)
                {
                    primitive = new Primitive(item);
                    primitiveList.Add(primitive);
                }
            }
            else if (attributeValue.NS != null && attributeValue.NS.Count != 0)
            {
                primitiveList = new PrimitiveList(DynamoDBEntryType.Numeric);
                foreach (string item in attributeValue.NS)
                {
                    primitive = new Primitive(item, true);
                    primitiveList.Add(primitive);
                }
            }
            else if (attributeValue.BS != null && attributeValue.BS.Count != 0)
            {
                primitiveList = new PrimitiveList(DynamoDBEntryType.Binary);
                foreach (MemoryStream item in attributeValue.BS)
                {
                    primitive = new Primitive(item);
                    primitiveList.Add(primitive);
                }
            }

            if (primitiveList != null)
            {
                return(primitiveList);
            }

            return(null);
        }
Beispiel #3
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);
        }