-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from guregu/tx
Transactions
- Loading branch information
Showing
13 changed files
with
654 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package dynamo | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
) | ||
|
||
// ConditionCheck represents a condition for a write transaction to succeed. | ||
// It is used along with WriteTx.Check. | ||
type ConditionCheck struct { | ||
table Table | ||
hashKey string | ||
hashValue *dynamodb.AttributeValue | ||
rangeKey string | ||
rangeValue *dynamodb.AttributeValue | ||
|
||
condition string | ||
subber | ||
|
||
err error | ||
} | ||
|
||
// Check creates a new ConditionCheck, which represents a condition for a write transaction to succeed. | ||
// hashKey specifies the name of the table's hash key and value specifies the value of the hash key. | ||
// You must use Range to specify a range key for tables with hash and range keys. | ||
func (table Table) Check(hashKey string, value interface{}) *ConditionCheck { | ||
check := &ConditionCheck{ | ||
table: table, | ||
hashKey: hashKey, | ||
} | ||
check.hashValue, check.err = marshal(value, "") | ||
return check | ||
} | ||
|
||
// Range specifies the name and value of the range key for this item. | ||
func (check *ConditionCheck) Range(rangeKey string, value interface{}) *ConditionCheck { | ||
check.rangeKey = rangeKey | ||
var err error | ||
check.rangeValue, err = marshal(value, "") | ||
check.setError(err) | ||
return check | ||
} | ||
|
||
// If specifies a conditional expression for this coniditon check to succeed. | ||
// Use single quotes to specificy reserved names inline (like 'Count'). | ||
// Use the placeholder ? within the expression to substitute values, and use $ for names. | ||
// You need to use quoted or placeholder names when the name is a reserved word in DynamoDB. | ||
func (check *ConditionCheck) If(expr string, args ...interface{}) *ConditionCheck { | ||
cond, err := check.subExpr(expr, args...) | ||
check.setError(err) | ||
check.condition = cond | ||
return check | ||
} | ||
|
||
// IfExists sets this check to succeed if the item exists. | ||
func (check *ConditionCheck) IfExists() *ConditionCheck { | ||
return check.If("attribute_exists($)", check.hashKey) | ||
} | ||
|
||
// IfNotExists sets this check to succeed if the item does not exist. | ||
func (check *ConditionCheck) IfNotExists() *ConditionCheck { | ||
return check.If("attribute_not_exists($)", check.hashKey) | ||
} | ||
|
||
func (check *ConditionCheck) writeTxItem() (*dynamodb.TransactWriteItem, error) { | ||
if check.err != nil { | ||
return nil, check.err | ||
} | ||
item := &dynamodb.ConditionCheck{ | ||
TableName: aws.String(check.table.name), | ||
Key: check.keys(), | ||
ExpressionAttributeNames: check.nameExpr, | ||
ExpressionAttributeValues: check.valueExpr, | ||
} | ||
if check.condition != "" { | ||
item.ConditionExpression = aws.String(check.condition) | ||
} | ||
return &dynamodb.TransactWriteItem{ | ||
ConditionCheck: item, | ||
}, nil | ||
} | ||
|
||
func (check *ConditionCheck) keys() map[string]*dynamodb.AttributeValue { | ||
keys := map[string]*dynamodb.AttributeValue{check.hashKey: check.hashValue} | ||
if check.rangeKey != "" { | ||
keys[check.rangeKey] = check.rangeValue | ||
} | ||
return keys | ||
} | ||
|
||
func (check *ConditionCheck) setError(err error) { | ||
if check.err == nil { | ||
check.err = err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/guregu/dynamo | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.16.15 | ||
github.com/cenkalti/backoff v2.1.1+incompatible | ||
github.com/gofrs/uuid v3.1.0+incompatible | ||
github.com/stretchr/testify v1.3.0 // indirect | ||
golang.org/x/net v0.0.0-20190108155000-395948e2f546 | ||
golang.org/x/text v0.3.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
github.com/aws/aws-sdk-go v1.16.15 h1:kQyxfRyjAwIYjf0225sn/pn+WAlncKyI8dmT3+ItMFE= | ||
github.com/aws/aws-sdk-go v1.16.15/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= | ||
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY= | ||
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedyHx0uzKwA= | ||
github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/net v0.0.0-20190108155000-395948e2f546 h1:tkMg6+6TF2qZ/3I8fw+DiNgPSsABxdVIqWE90w8Vxzk= | ||
golang.org/x/net v0.0.0-20190108155000-395948e2f546/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.