Skip to content

Commit

Permalink
Deprecating and updating parts of the DynamoDB namespace that do not …
Browse files Browse the repository at this point in the history
…support the new document model.
  • Loading branch information
jeremeamia committed Oct 8, 2014
1 parent a397c34 commit a9492c8
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 40 deletions.
19 changes: 16 additions & 3 deletions CHANGELOG.md
@@ -1,9 +1,22 @@
CHANGELOG
=========

Next Release:
-------------

Next Release
------------

* Added document model support to the Amazon DynamoDB client, including support
for the new data types (`L`, `M`, `BOOL`, and `NULL`), nested attributes, and
expressions.
* Deprecated the `Aws\DynamoDb\Model\Attribute`, `Aws\DynamoDb\Model\Item`,
and `Aws\DynamoDb\Iterator\ItemIterator` classes, and the
`Aws\DynamoDb\DynamoDbClient::formatValue` and
`Aws\DynamoDb\DynamoDbClient::formatAttribute` methods, since they are
incompatible with the new document model. These deprecated classes and methods
only work reliably if the attributes being worked with do not include the new
types (i.e., only include `S`, `N`, `B`, `SS`, `NS`, and `BS`).
* Updated the Amazon DynamoDB client to permanently disable client-side
parameter validation. This needed to be done in order to support the new
document model features.
* Updated the Amazon EC2 client to sign requests with Signature V4.
* Fixed an issue in the S3 service description to make the `VersionId`
work in `S3Client::restoreObject`.
Expand Down
5 changes: 0 additions & 5 deletions docs/service-dynamodb.rst
Expand Up @@ -73,11 +73,6 @@ You can add an item to our *errors* table using the
`putItem() <http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_putItem>`_
method of the client.

.. example:: DynamoDb/Integration/DynamoDb_20120810_Test.php testAddItem

As you can see, the ``formatAttributes()`` method of the client can be used to more easily format the attributes of the
item. Alternatively, you can provide the item attributes without using the helper method:

.. example:: DynamoDb/Integration/DynamoDb_20120810_Test.php testAddItemWithoutHelperMethod

You can also add items in batches of up to 25 items using the `BatchWriteItem()
Expand Down
4 changes: 4 additions & 0 deletions src/Aws/DynamoDb/DynamoDbClient.php
Expand Up @@ -134,6 +134,8 @@ private static function createBackoffPlugin(JsonQueryExceptionParser $exceptionP
* @param string $format The type of format (e.g. put, update).
*
* @return array The formatted value.
* @deprecated The new DynamoDB document model, including the new types
* (L, M, BOOL, NULL), is not supported by this method.
*/
public function formatValue($value, $format = Attribute::FORMAT_PUT)
{
Expand All @@ -147,6 +149,8 @@ public function formatValue($value, $format = Attribute::FORMAT_PUT)
* @param string $format The type of format (e.g. put, update).
*
* @return array The formatted values.
* @deprecated The new DynamoDB document model, including the new types
* (L, M, BOOL, NULL), is not supported by this method.
*/
public function formatAttributes(array $values, $format = Attribute::FORMAT_PUT)
{
Expand Down
8 changes: 6 additions & 2 deletions src/Aws/DynamoDb/Iterator/ItemIterator.php
Expand Up @@ -21,8 +21,12 @@
use Guzzle\Common\ToArrayInterface;

/**
* Converts items to a simple associative array form with type information removed. Also performs base64_decode on
* values specified as binary. Each item is yielded as an array-accessible Collection object
* Converts items to a simple associative array form with type information
* removed. Also performs base64_decode on values specified as binary. Each item
* is yielded as an array-accessible Collection object.
*
* @deprecated The new DynamoDB document model, including the new types (L, M,
* BOOL, NULL), is not supported by this class.
*/
class ItemIterator extends \IteratorIterator implements \Countable, ToArrayInterface
{
Expand Down
3 changes: 3 additions & 0 deletions src/Aws/DynamoDb/Model/Attribute.php
Expand Up @@ -23,6 +23,9 @@
/**
* Class representing a DynamoDB item attribute. Contains helpers for building
* attributes and arrays of attributes.
*
* @deprecated The new DynamoDB document model, including the new types (L, M,
* BOOL, NULL), is not supported by this class.
*/
class Attribute implements ToArrayInterface
{
Expand Down
28 changes: 15 additions & 13 deletions src/Aws/DynamoDb/Model/BatchRequest/PutRequest.php
Expand Up @@ -26,7 +26,7 @@
class PutRequest extends AbstractWriteRequest
{
/**
* @var Item The item to be inserted into the DynamoDB table
* @var array The item to be inserted into the DynamoDB table
*/
protected $item;

Expand All @@ -49,27 +49,29 @@ public static function fromCommand(AbstractCommand $command)
$table = $command->get('TableName');
$item = $command->get('Item');

// Create an Item object from the 'item' command data
if (!($item instanceof Item)) {
$item = new Item($item, $table);
}

// Return an instantiated PutRequest object
return new PutRequest($item, $table);
}

/**
* Constructs a new put request
*
* @param Item $item The item to put into DynamoDB
* @param string $tableName The name of the table which has the item
* @param array|Item $item The item to put into DynamoDB
* @param string $tableName The name of the table which has the item
*
* @throw InvalidArgumentException if the table name is not provided
*/
public function __construct(Item $item, $tableName = null)
public function __construct($item, $tableName = null)
{
$this->item = $item;
$this->tableName = $tableName ?: $item->getTableName();
if ($item instanceof Item) {
$this->item = $item->toArray();
$this->tableName = $tableName ?: $item->getTableName();
} elseif (is_array($item)) {
$this->item = $item;
$this->tableName = $tableName;
} else {
throw new InvalidArgumentException('The item must be an array or an Item object.');
}

if (!$this->tableName) {
throw new InvalidArgumentException('A table name is required to create a PutRequest.');
Expand All @@ -83,7 +85,7 @@ public function __construct(Item $item, $tableName = null)
*/
public function toArray()
{
return array('PutRequest' => array('Item' => $this->item->toArray()));
return array('PutRequest' => array('Item' => $this->item));
}

/**
Expand All @@ -93,6 +95,6 @@ public function toArray()
*/
public function getItem()
{
return $this->item;
return new Item($this->item);
}
}
3 changes: 3 additions & 0 deletions src/Aws/DynamoDb/Model/Item.php
Expand Up @@ -20,6 +20,9 @@

/**
* Amazon DynamoDB item model
*
* @deprecated The new DynamoDB document model, including the new types (L, M,
* BOOL, NULL), is not supported by this class.
*/
class Item implements \ArrayAccess, \IteratorAggregate, ToArrayInterface, \Countable
{
Expand Down
Expand Up @@ -256,6 +256,9 @@ public function testAddItemWithoutHelperMethod($time)
)
));

// The result will always contain ConsumedCapacityUnits
echo $result->getPath('ConsumedCapacity/CapacityUnits') . "\n";

// @end
return $time;
}
Expand Down
Expand Up @@ -98,11 +98,10 @@ public function testWriteRequestBatchForPuts()
$putBatch = WriteRequestBatch::factory($client);
for ($i = 0; $i < 55; $i++) {
$itemIds[] = $itemId = uniqid();
$item = Item::fromArray(array(
'id' => $itemId,
'timestamp' => time(),
));
$putBatch->add(new PutRequest($item, $tableName));
$putBatch->add(new PutRequest(array(
'id' => array('S' => $itemId),
'timestamp' => array('N' => (string) time()),
), $tableName));
}
$putBatch->flush();
// @end
Expand Down
23 changes: 11 additions & 12 deletions tests/Aws/Tests/DynamoDb/Model/BatchRequest/PutRequestTest.php
Expand Up @@ -20,33 +20,32 @@
use Aws\DynamoDb\Model\Item;

/**
* @covers Aws\DynamoDb\Model\BatchRequest\PutRequest
* @covers \Aws\DynamoDb\Model\BatchRequest\PutRequest
*/
class PutRequestTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testConstructorSetsValues()
{
$item = $this->getMock('Aws\DynamoDb\Model\Item');

// Instantiate with Item object.
$item = new Item(['id' => ['S' => 'foo']]);
$putRequest = new PutRequest($item, 'table');
$this->assertEquals($item->toArray(), $putRequest->getItem()->toArray());

$this->assertSame($item, $putRequest->getItem());
// Instantiate with item array.
$item = ['id' => ['S' => 'foo']];
$putRequest = new PutRequest(['id' => ['S' => 'foo']], 'table');
$this->assertEquals($item, $putRequest->getItem()->toArray());
}

public function testConstructorSetsValuesWhenItemContainsTable()
{
$item = $this->getMock('Aws\DynamoDb\Model\Item');
$item->expects($this->any())
->method('getTableName')
->will($this->returnValue('table'));

$item = new Item(['id' => ['S' => 'foo']], 'table');
$putRequest = new PutRequest($item);

$this->assertSame($item, $putRequest->getItem());
$this->assertSame($item->toArray(), $putRequest->getItem()->toArray());
}

/**
* @expectedException Aws\Common\Exception\InvalidArgumentException
* @expectedException \Aws\Common\Exception\InvalidArgumentException
*/
public function testConstructorThrowsExceptionWithoutTable()
{
Expand Down

0 comments on commit a9492c8

Please sign in to comment.