/
delete.go
95 lines (83 loc) · 3.35 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package procedure
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/nisimpson/ezddb"
)
// Delete functions generate dynamodb put input data given some context.
type Delete func(context.Context) (*dynamodb.DeleteItemInput, error)
// Invoke is a wrapper around the function invocation for stylistic purposes.
func (d Delete) Invoke(ctx context.Context) (*dynamodb.DeleteItemInput, error) {
return d(ctx)
}
// DeleteModifier makes modifications to the input before the procedure is executed.
type DeleteModifier interface {
// ModifyDeleteItemInput is invoked when this modifier is applied to the provided input.
ModifyDeleteItemInput(context.Context, *dynamodb.DeleteItemInput) error
}
// DeleteModifierFunc is a function that implements DeleteModifier.
type DeleteModifierFunc modifier[dynamodb.DeleteItemInput]
func (d DeleteModifierFunc) ModifyDeleteItemInput(ctx context.Context, input *dynamodb.DeleteItemInput) error {
return d(ctx, input)
}
// Modify adds modifying functions to the procedure, transforming the input
// before it is executed.
func (d Delete) Modify(modifiers ...DeleteModifier) Delete {
mapper := func(ctx context.Context, input *dynamodb.DeleteItemInput, mod DeleteModifier) error {
return mod.ModifyDeleteItemInput(ctx, input)
}
return func(ctx context.Context) (*dynamodb.DeleteItemInput, error) {
return modify[dynamodb.DeleteItemInput](ctx, d, newModiferGroup(modifiers, mapper).Join())
}
}
// Execute executes the procedure, returning the API result.
func (d Delete) Execute(ctx context.Context,
deleter ezddb.Deleter, options ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error) {
if input, err := d.Invoke(ctx); err != nil {
return nil, err
} else {
return deleter.DeleteItem(ctx, input, options...)
}
}
// ModifyTransactWriteItemsInput implements the TransactWriteModifier interface.
func (d Delete) ModifyTransactWriteItemsInput(ctx context.Context, input *dynamodb.TransactWriteItemsInput) error {
if deletes, err := d.Invoke(ctx); err != nil {
return err
} else {
input.TransactItems = append(input.TransactItems, types.TransactWriteItem{
Delete: &types.Delete{
TableName: deletes.TableName,
Key: deletes.Key,
ConditionExpression: deletes.ConditionExpression,
ExpressionAttributeNames: deletes.ExpressionAttributeNames,
ExpressionAttributeValues: deletes.ExpressionAttributeValues,
},
})
return nil
}
}
// ModifyBatchWriteItemInput implements the BatchWriteModifier interface.
func (d Delete) ModifyBatchWriteItemInput(ctx context.Context, input *dynamodb.BatchWriteItemInput) error {
if input.RequestItems == nil {
input.RequestItems = make(map[string][]types.WriteRequest)
}
if deletes, err := d.Invoke(ctx); err != nil {
return err
} else if deletes.TableName == nil {
return fmt.Errorf("delete procedure has empty table name; cannot creat batch procedure")
} else if requests, ok := input.RequestItems[*deletes.TableName]; !ok {
input.RequestItems[*deletes.TableName] = []types.WriteRequest{
{
DeleteRequest: &types.DeleteRequest{Key: deletes.Key},
},
}
} else {
requests = append(requests, types.WriteRequest{
DeleteRequest: &types.DeleteRequest{Key: deletes.Key},
})
input.RequestItems[*deletes.TableName] = requests
}
return nil
}