Ejemplo n.º 1
0
        internal Key MakeKey(IDictionary <string, DynamoDBEntry> doc)
        {
            Key key = new Key();

            foreach (var kvp in Keys)
            {
                string         keyName     = kvp.Key;
                KeyDescription description = kvp.Value;
                DynamoDBEntry  value;
                if (!doc.TryGetValue(keyName, out value))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Document does not contain value for key {0}", keyName));
                }
                value = value.ToConvertedEntry(Conversion);
                if (StoreAsEpoch.Contains(keyName))
                {
                    value = Document.DateTimeToEpochSeconds(value, keyName);
                }

                Primitive primitive = value.AsPrimitive();
                if (primitive == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Key attribute {0} must be a Primitive type", keyName));
                }
                if (primitive.Type != description.Type)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Key attribute {0} must be of type {1}", keyName, description.Type));
                }

                key[keyName] = primitive.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(Conversion));
            }
            return(key);
        }
Ejemplo n.º 2
0
        internal Key MakeKey(Primitive hashKey, Primitive rangeKey)
        {
            Key newKey = new Key();

            if (HashKeys.Count != 1)
            {
                throw new InvalidOperationException("Must have one hash key defined for the table " + TableName);
            }
            string         hashKeyName        = HashKeys[0];
            KeyDescription hashKeyDescription = Keys[hashKeyName];

            if (this.StoreAsEpoch.Contains(hashKeyName))
            {
                hashKey = KeyDateTimeToEpochSeconds(hashKey, hashKeyName);
            }

            if (hashKeyDescription.Type != hashKey.Type)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Schema for table {0}, hash key {1}, is inconsistent with specified hash key value.", TableName, hashKeyName));
            }

            var hashKeyAttributeValue = hashKey.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(Conversion));

            newKey[hashKeyName] = hashKeyAttributeValue;

            if ((rangeKey == null) != (RangeKeys.Count == 0))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Schema for table {0}, range key {1}, is inconsistent with specified range key value.", TableName, hashKeyName));
            }
            else if (rangeKey != null)
            {
                string         rangeKeyName        = RangeKeys[0];
                KeyDescription rangeKeyDescription = Keys[rangeKeyName];

                if (this.StoreAsEpoch.Contains(rangeKeyName))
                {
                    rangeKey = KeyDateTimeToEpochSeconds(rangeKey, rangeKeyName);
                }

                if (rangeKeyDescription.Type != rangeKey.Type)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                      "Schema for table {0}, range key {1}, is inconsistent with specified range key value.", TableName, hashKeyName));
                }
                var rangeKeyAttributeValue = rangeKey.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(Conversion));
                newKey[rangeKeyName] = rangeKeyAttributeValue;
            }

            return(newKey);
        }