Skip to content

Commit

Permalink
Implement DeleteDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Broglie committed Nov 18, 2014
1 parent b3d3a84 commit 2a4bcdc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
19 changes: 19 additions & 0 deletions dynamodb/dynamo_query_builder_test.go
Expand Up @@ -35,6 +35,7 @@ func TestDynamoQuery(t *testing.T) {
testGetQuery(t, table, true, `{"TableName":"DynamoDBTestMyTable","ConsistentRead":"true","Key":{"TestHashKey":{"S":"NewHashKeyVal"}}}`)
testGetQuery(t, table, false, `{"TableName":"DynamoDBTestMyTable","Key":{"TestHashKey":{"S":"NewHashKeyVal"}}}`)
testPutQuery(t, table, `{"TableName":"DynamoDBTestMyTable","Item":{"Attr1":{"S":"Attr1Val"},"Attr2":{"N":"12"},"TestHashKey":{"S":"NewHashKeyVal"}}}`)
testDeleteQuery(t, table, false, `{"TableName":"DynamoDBTestMyTable","Key":{"TestHashKey":{"S":"NewHashKeyVal"}}}`)
}

func TestDynamoQueryWithRange(t *testing.T) {
Expand Down Expand Up @@ -65,6 +66,7 @@ func TestDynamoQueryWithRange(t *testing.T) {
testGetQuery(t, table, true, `{"TableName":"DynamoDBTestMyTable","ConsistentRead":"true","Key":{"TestHashKey":{"S":"NewHashKeyVal"},"TestRangeKey":{"N":"12"}}}`)
testGetQuery(t, table, false, `{"TableName":"DynamoDBTestMyTable","Key":{"TestHashKey":{"S":"NewHashKeyVal"},"TestRangeKey":{"N":"12"}}}`)
testPutQuery(t, table, `{"TableName":"DynamoDBTestMyTable","Item":{"Attr1":{"S":"Attr1Val"},"Attr2":{"N":"12"},"TestHashKey":{"S":"NewHashKeyVal"},"TestRangeKey":{"N":"12"}}}`)
testDeleteQuery(t, table, false, `{"TableName":"DynamoDBTestMyTable","Key":{"TestHashKey":{"S":"NewHashKeyVal"},"TestRangeKey":{"N":"12"}}}`)
}

func testPutQuery(t *testing.T, table *Table, expected string) {
Expand Down Expand Up @@ -112,6 +114,23 @@ func testGetQuery(t *testing.T, table *Table, consistent bool, expected string)
compareJSONStrings(t, expected, actual)
}

func testDeleteQuery(t *testing.T, table *Table, consistent bool, expected string) {
var key *Key
if table.Key.HasRange() {
key = &Key{HashKey: "NewHashKeyVal", RangeKey: "12"}
} else {
key = &Key{HashKey: "NewHashKeyVal"}
}

q := NewDynamoQuery(table)
if err := q.AddKey(key); err != nil {
t.Error(err)
}

actual := q.String()
compareJSONStrings(t, expected, actual)
}

// What we're trying to do here is compare the JSON encoded values, but we can't
// to a simple encode + string compare since JSON encoding is not ordered. So
// what we do is JSON encode, then JSON decode into untyped maps, and then
Expand Down
37 changes: 36 additions & 1 deletion dynamodb/item.go
Expand Up @@ -187,6 +187,11 @@ func (t *Table) GetDocumentConsistent(key *Key, consistentRead bool, v interface
return err
}

// If Item is nil the item doesn't exist.
if response.Item == nil {
return ErrNotFound
}

// Delete the keys from the response.
delete(response.Item, t.Key.KeyAttribute.Name)
if t.Key.HasRange() {
Expand Down Expand Up @@ -319,7 +324,7 @@ func (t *Table) deleteItem(key *Key, expected []Attribute) (bool, error) {
q.AddExpected(expected)
}

jsonResponse, err := t.Server.queryServer(target("DeleteItem"), q)
jsonResponse, err := t.runDeleteItemQuery(q)

if err != nil {
return false, err
Expand All @@ -333,6 +338,16 @@ func (t *Table) deleteItem(key *Key, expected []Attribute) (bool, error) {
return true, nil
}

func (t *Table) runDeleteItemQuery(q Query) ([]byte, error) {
// TODO: implement retry logic
jsonResponse, err := t.Server.queryServer(target("DeleteItem"), q)
if err != nil {
return nil, err
}

return jsonResponse, nil
}

func (t *Table) DeleteItem(key *Key) (bool, error) {
return t.deleteItem(key, nil)
}
Expand All @@ -341,6 +356,26 @@ func (t *Table) ConditionalDeleteItem(key *Key, expected []Attribute) (bool, err
return t.deleteItem(key, expected)
}

func (t *Table) DeleteDocument(key *Key) error {
q := NewDynamoQuery(t)
q.AddKey(key)

jsonResponse, err := t.runDeleteItemQuery(q)
if err != nil {
return err
}

// A successful DELETE returns an empty JSON object. Simply checking for
// valid JSON here.
var response map[string]interface{}
err = json.Unmarshal(jsonResponse, &response)
if err != nil {
return err
}

return nil
}

func (t *Table) AddAttributes(key *Key, attributes []Attribute) (bool, error) {
return t.modifyAttributes(key, attributes, nil, "ADD")
}
Expand Down
7 changes: 6 additions & 1 deletion dynamodb/item_test.go
Expand Up @@ -437,5 +437,10 @@ func (s *ItemSuite) TestPutGetDeleteDocument(c *check.C) {
}
c.Check(out, check.DeepEquals, in)

// TODO: delete document
// Delete
if err := s.table.DeleteDocument(k); err != nil {
c.Fatal(err)
}
err := s.table.GetDocument(k, &out)
c.Check(err.Error(), check.Matches, "Item not found")
}

0 comments on commit 2a4bcdc

Please sign in to comment.