Skip to content

Commit

Permalink
support ReturnValuesOnConditionCheckFailure (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Aug 22, 2024
1 parent eed9493 commit 1519c9c
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 120 deletions.
1 change: 1 addition & 0 deletions conditioncheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (check *ConditionCheck) writeTxItem() (*types.TransactWriteItem, error) {
}
if check.condition != "" {
item.ConditionExpression = aws.String(check.condition)
item.ReturnValuesOnConditionCheckFailure = types.ReturnValuesOnConditionCheckFailureAllOld
}
return &types.TransactWriteItem{
ConditionCheck: item,
Expand Down
43 changes: 41 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,45 @@ func IsCondCheckFailed(err error) bool {
return false
}

// type noopLogger struct{}
// Unmarshals an item from a ConditionalCheckFailedException into `out`, with the same behavior as [UnmarshalItem].
// The return value boolean `match` will be true if condCheckErr is a ConditionalCheckFailedException,
// otherwise false if it is nil or a different error.
func UnmarshalItemFromCondCheckFailed(condCheckErr error, out any) (match bool, err error) {
if condCheckErr == nil {
return false, nil
}
var cfe *types.ConditionalCheckFailedException
if errors.As(condCheckErr, &cfe) {
if cfe.Item == nil {
return true, fmt.Errorf("dynamo: ConditionalCheckFailedException does not contain item")
}
return true, UnmarshalItem(cfe.Item, out)
}
return false, condCheckErr
}

// func (noopLogger) Log(...interface{}) {}
// Unmarshals items from a TransactionCanceledException by appending them to `out`, which must be a pointer to a slice.
// The return value boolean `match` will be true if txCancelErr is a TransactionCanceledException with at least one ConditionalCheckFailed cancellation reason,
// otherwise false if it is nil or a different error.
func UnmarshalItemsFromTxCondCheckFailed(txCancelErr error, out any) (match bool, err error) {
if txCancelErr == nil {
return false, nil
}
unmarshal := unmarshalAppendTo(out)
var txe *types.TransactionCanceledException
if errors.As(txCancelErr, &txe) {
for _, cr := range txe.CancellationReasons {
if cr.Code != nil && *cr.Code == "ConditionalCheckFailed" {
if cr.Item == nil {
return true, fmt.Errorf("dynamo: TransactionCanceledException.CancellationReasons does not contain item")
}
if err = unmarshal(cr.Item, out); err != nil {
return true, err
}
match = true
}
}
return match, nil
}
return false, txCancelErr
}
40 changes: 31 additions & 9 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html
type Delete struct {
table Table
returnType string
returnType types.ReturnValue

hashKey string
hashValue types.AttributeValue
Expand Down Expand Up @@ -79,15 +79,15 @@ func (d *Delete) ConsumedCapacity(cc *ConsumedCapacity) *Delete {

// Run executes this delete request.
func (d *Delete) Run(ctx context.Context) error {
d.returnType = "NONE"
d.returnType = types.ReturnValueNone
_, err := d.run(ctx)
return err
}

// OldValue executes this delete request, unmarshaling the previous value to out.
// Returns ErrNotFound is there was no previous value.
func (d *Delete) OldValue(ctx context.Context, out interface{}) error {
d.returnType = "ALL_OLD"
d.returnType = types.ReturnValueAllOld
output, err := d.run(ctx)
switch {
case err != nil:
Expand All @@ -98,6 +98,26 @@ func (d *Delete) OldValue(ctx context.Context, out interface{}) error {
return unmarshalItem(output.Attributes, out)
}

// CurrentValue executes this delete.
// If successful, the return value `deleted` will be true, and nothing will be unmarshaled to `out`
//
// If the delete is unsuccessful because of a condition check failure, `deleted` will be false, the current value of the item will be unmarshaled to `out`, and `err` will be nil.
//
// If the delete is unsuccessful for any other reason, `deleted` will be false and `err` will be non-nil.
//
// See also: [UnmarshalItemFromCondCheckFailed].
func (d *Delete) CurrentValue(ctx context.Context, out interface{}) (wrote bool, err error) {
d.returnType = types.ReturnValueNone
_, err = d.run(ctx)
if err != nil {
if ok, err := UnmarshalItemFromCondCheckFailed(err, out); ok {
return false, err
}
return false, err
}
return true, nil
}

func (d *Delete) run(ctx context.Context) (*dynamodb.DeleteItemOutput, error) {
if d.err != nil {
return nil, d.err
Expand All @@ -121,12 +141,13 @@ func (d *Delete) deleteInput() *dynamodb.DeleteItemInput {
input := &dynamodb.DeleteItemInput{
TableName: &d.table.name,
Key: d.key(),
ReturnValues: types.ReturnValue(d.returnType),
ReturnValues: d.returnType,
ExpressionAttributeNames: d.nameExpr,
ExpressionAttributeValues: d.valueExpr,
}
if d.condition != "" {
input.ConditionExpression = &d.condition
input.ReturnValuesOnConditionCheckFailure = types.ReturnValuesOnConditionCheckFailureAllOld
}
if d.cc != nil {
input.ReturnConsumedCapacity = types.ReturnConsumedCapacityIndexes
Expand All @@ -141,11 +162,12 @@ func (d *Delete) writeTxItem() (*types.TransactWriteItem, error) {
input := d.deleteInput()
item := &types.TransactWriteItem{
Delete: &types.Delete{
TableName: input.TableName,
Key: input.Key,
ExpressionAttributeNames: input.ExpressionAttributeNames,
ExpressionAttributeValues: input.ExpressionAttributeValues,
ConditionExpression: input.ConditionExpression,
TableName: input.TableName,
Key: input.Key,
ExpressionAttributeNames: input.ExpressionAttributeNames,
ExpressionAttributeValues: input.ExpressionAttributeValues,
ConditionExpression: input.ConditionExpression,
ReturnValuesOnConditionCheckFailure: input.ReturnValuesOnConditionCheckFailure,
},
}
return item, nil
Expand Down
12 changes: 8 additions & 4 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ func TestDelete(t *testing.T) {
}

// fail to delete it
err = table.Delete("UserID", item.UserID).
var curr widget
wrote, err := table.Delete("UserID", item.UserID).
Range("Time", item.Time).
If("Meta.'color' = ?", "octarine").
If("Msg = ?", "wrong msg").
Run(ctx)
if !IsCondCheckFailed(err) {
t.Error("expected ConditionalCheckFailedException, not", err)
CurrentValue(ctx, &curr)
if wrote {
t.Error("wrote should be false")
}
if !reflect.DeepEqual(curr, item) {
t.Errorf("bad value. %#v ≠ %#v", curr, item)
}

// delete it
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module github.com/guregu/dynamo/v2

require (
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2 v1.30.4
github.com/aws/aws-sdk-go-v2/config v1.11.0
github.com/aws/aws-sdk-go-v2/credentials v1.6.4
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.14.9
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.3
github.com/aws/smithy-go v1.20.3
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.14.11
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.34.5
github.com/aws/smithy-go v1.20.4
github.com/cenkalti/backoff/v4 v4.3.0
golang.org/x/sync v0.7.0
golang.org/x/sync v0.8.0
)

require (
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.22.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.16 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.11.1 // indirect
Expand Down
Loading

0 comments on commit 1519c9c

Please sign in to comment.