-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_transact.go
142 lines (131 loc) · 3.44 KB
/
op_transact.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package rotor
import (
"context"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
// TransactUpdateItem TransactUpdateItem
type TransactUpdateItem struct {
Key PrimaryKeyType
Builder *expression.Builder
checkKey PrimaryKeyType
checkBuilder *expression.Builder
}
// TransactDeleteItem TransactDeleteItem
type TransactDeleteItem struct {
Key PrimaryKeyType
Builder *expression.Builder
}
// TransactPutItem TransactPutItem
type TransactPutItem struct {
Item interface{}
Builder *expression.Builder
}
// TransactOptions TransactOptions
type TransactOptions struct {
UpdateItems []TransactUpdateItem
PutItems []TransactPutItem
DeleteItems []TransactDeleteItem
}
func defaultTransactOptions() *TransactOptions {
return &TransactOptions{
UpdateItems: []TransactUpdateItem{},
PutItems: []TransactPutItem{},
DeleteItems: []TransactDeleteItem{},
}
}
// TransactOption TransactOption
type TransactOption func(options *TransactOptions)
// Transact Transact
func (rs *Service) Transact(ctx context.Context, opts ...TransactOption) error {
options := defaultTransactOptions()
for _, opt := range opts {
opt(options)
}
writeLen := len(options.UpdateItems) + len(options.PutItems) + len(options.DeleteItems)
if writeLen == 0 {
return ErrInput
}
if writeLen > maxWriteNum {
return ErrInput
}
inItems := make([]*dynamodb.TransactWriteItem, writeLen)
for i, update := range options.UpdateItems {
var expr expression.Expression
var err error
if update.Builder != nil {
expr, err = update.Builder.Build()
if err != nil {
return err
}
}
inItems[i] = &dynamodb.TransactWriteItem{
Update: &dynamodb.Update{
TableName: rs.tableName,
Key: update.Key,
UpdateExpression: expr.Update(),
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
},
}
}
for i, put := range options.PutItems {
var expr expression.Expression
var err error
if put.Builder != nil {
expr, err = put.Builder.Build()
if err != nil {
return err
}
}
item, err := rs.codec.MarshalMap(put.Item)
if err != nil {
return err
}
inItems[i] = &dynamodb.TransactWriteItem{
Put: &dynamodb.Put{
TableName: rs.tableName,
Item: item,
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
},
}
}
for i, delete := range options.DeleteItems {
var expr expression.Expression
var err error
if delete.Builder != nil {
expr, err = delete.Builder.Build()
if err != nil {
return err
}
}
inItems[i] = &dynamodb.TransactWriteItem{
Delete: &dynamodb.Delete{
TableName: rs.tableName,
Key: delete.Key,
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
},
}
}
_, err := rs.dynamo.TransactWriteItemsWithContext(ctx, &dynamodb.TransactWriteItemsInput{
TransactItems: inItems,
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeConditionalCheckFailedException:
return ErrConditionalCheck
default:
return err
}
}
return err
}
return nil
}