-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteitem.go
104 lines (94 loc) · 2.54 KB
/
deleteitem.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
96
97
98
99
100
101
102
103
104
package dynago
import (
"fmt"
"reflect"
"time"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// DeleteItem represents a delete item operation.
type DeleteItem struct {
item Keyer
input *dynamodb.DeleteItemInput
dynago *Dynago
err error
}
// DeleteItem creates a DeleteItem operation.
func (d *Dynago) DeleteItem(item Keyer) *DeleteItem {
return &DeleteItem{
item: item,
input: &dynamodb.DeleteItemInput{
TableName: &d.config.DefaultTableName,
},
dynago: d,
}
}
// TableName sets the table.
func (q *DeleteItem) TableName(name string) *DeleteItem {
q.input.TableName = &name
return q
}
// ExpressionAttributeValue sets an ExpressionAttributeValue.
func (q *DeleteItem) ExpressionAttributeValue(key string, val interface{}, layout ...string) *DeleteItem {
if q.input.ExpressionAttributeValues == nil {
q.input.ExpressionAttributeValues = make(map[string]*dynamodb.AttributeValue)
}
lay := time.RFC3339
if len(layout) > 0 {
lay = layout[0]
}
av, err := q.dynago.simpleMarshal(reflect.ValueOf(val), lay)
if err != nil {
q.err = err
}
q.input.ExpressionAttributeValues[key] = av
return q
}
// ConditionExpression sets the ConditionExpression.
func (q *DeleteItem) ConditionExpression(exp string) *DeleteItem {
q.input.ConditionExpression = &exp
return q
}
// ExpressionAttributeName sets a ExpressionAttributeName.
func (q *DeleteItem) ExpressionAttributeName(name string, sub string) *DeleteItem {
if q.input.ExpressionAttributeNames == nil {
q.input.ExpressionAttributeNames = make(map[string]*string)
}
q.input.ExpressionAttributeNames[name] = &sub
return q
}
// Exec executes the operation.
func (q *DeleteItem) Exec() error {
if q.err != nil {
return q.err
}
var err error
q.input.Key, err = q.dynago.key(q.item)
if err != nil {
return fmt.Errorf("q.dynago.key: %w", err)
}
_, err = q.dynago.ddb.DeleteItem(q.input)
if err != nil {
return fmt.Errorf("d.ddb.DeleteItem: %w", err)
}
return nil
}
// TransactionWriteItem implements the TransactionWriteItemer
// interface.
func (q *DeleteItem) TransactionWriteItem() (*dynamodb.TransactWriteItem, error) {
if q.err != nil {
return nil, q.err
}
key, err := q.dynago.key(q.item)
if err != nil {
return nil, err
}
return &dynamodb.TransactWriteItem{
Delete: &dynamodb.Delete{
Key: key,
TableName: q.input.TableName,
ConditionExpression: q.input.ConditionExpression,
ExpressionAttributeNames: q.input.ExpressionAttributeNames,
ExpressionAttributeValues: q.input.ExpressionAttributeValues,
},
}, nil
}