/
op_DeleteItem.go
254 lines (205 loc) · 7.76 KB
/
op_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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package dyno
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
ddb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
ddbTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ericmaustin/dyno/condition"
"sync"
)
// DeleteItem executes DeleteItem operation and returns a DeleteItem operation
func (s *Session) DeleteItem(input *ddb.DeleteItemInput, mw ...DeleteItemMiddleWare) *DeleteItem {
return NewDeleteItem(input, mw...).Invoke(s.ctx, s.ddb)
}
// DeleteItem executes a DeleteItem operation with a DeleteItemInput in this pool and returns the DeleteItem operation
func (p *Pool) DeleteItem(input *ddb.DeleteItemInput, mw ...DeleteItemMiddleWare) *DeleteItem {
op := NewDeleteItem(input, mw...)
p.Do(op) // run the operation in the pool
return op
}
// DeleteItemContext represents an exhaustive DeleteItem operation request context
type DeleteItemContext struct {
context.Context
Input *ddb.DeleteItemInput
Client *ddb.Client
}
// DeleteItemOutput represents the output for the DeleteItem opration
type DeleteItemOutput struct {
out *ddb.DeleteItemOutput
err error
mu sync.RWMutex
}
// Set sets the output
func (o *DeleteItemOutput) Set(out *ddb.DeleteItemOutput, err error) {
o.mu.Lock()
o.out = out
o.err = err
o.mu.Unlock()
}
// Get gets the output
func (o *DeleteItemOutput) Get() (out *ddb.DeleteItemOutput, err error) {
o.mu.Lock()
out = o.out
err = o.err
o.mu.Unlock()
return
}
// DeleteItemHandler represents a handler for DeleteItem requests
type DeleteItemHandler interface {
HandleDeleteItem(ctx *DeleteItemContext, output *DeleteItemOutput)
}
// DeleteItemHandlerFunc is a DeleteItemHandler function
type DeleteItemHandlerFunc func(ctx *DeleteItemContext, output *DeleteItemOutput)
// HandleDeleteItem implements DeleteItemHandler
func (h DeleteItemHandlerFunc) HandleDeleteItem(ctx *DeleteItemContext, output *DeleteItemOutput) {
h(ctx, output)
}
// DeleteItemFinalHandler is the final DeleteItemHandler that executes a dynamodb DeleteItem operation
type DeleteItemFinalHandler struct{}
// HandleDeleteItem implements the DeleteItemHandler
func (h *DeleteItemFinalHandler) HandleDeleteItem(ctx *DeleteItemContext, output *DeleteItemOutput) {
output.Set(ctx.Client.DeleteItem(ctx, ctx.Input))
}
// DeleteItemMiddleWare is a middleware function use for wrapping DeleteItemHandler requests
type DeleteItemMiddleWare interface {
DeleteItemMiddleWare(next DeleteItemHandler) DeleteItemHandler
}
// DeleteItemMiddleWareFunc is a functional DeleteItemMiddleWare
type DeleteItemMiddleWareFunc func(handler DeleteItemHandler) DeleteItemHandler
// DeleteItemMiddleWare implements the DeleteItemMiddleWare interface
func (mw DeleteItemMiddleWareFunc) DeleteItemMiddleWare(h DeleteItemHandler) DeleteItemHandler {
return mw(h)
}
// DeleteItem represents a DeleteItem operation
type DeleteItem struct {
*BaseOperation
input *ddb.DeleteItemInput
middleWares []DeleteItemMiddleWare
}
// NewDeleteItem creates a new DeleteItem
func NewDeleteItem(input *ddb.DeleteItemInput, mws ...DeleteItemMiddleWare) *DeleteItem {
return &DeleteItem{
BaseOperation: NewOperation(),
input: input,
middleWares: mws,
}
}
// Invoke invokes the DeleteItem operation in a goroutine and returns a BatchGetItemAllOperation
func (op *DeleteItem) Invoke(ctx context.Context, client *ddb.Client) *DeleteItem {
op.SetRunning() // operation now waiting for a response
go op.InvokeDynoOperation(ctx, client)
return op
}
// InvokeDynoOperation invokes the DeleteItem operation
func (op *DeleteItem) InvokeDynoOperation(ctx context.Context, client *ddb.Client) {
output := new(DeleteItemOutput)
defer func() { op.SetResponse(output.Get()) }()
var h DeleteItemHandler
h = new(DeleteItemFinalHandler)
// loop in reverse to preserve middleware order
for i := len(op.middleWares) - 1; i >= 0; i-- {
h = op.middleWares[i].DeleteItemMiddleWare(h)
}
requestCtx := &DeleteItemContext{
Context: ctx,
Client: client,
Input: op.input,
}
h.HandleDeleteItem(requestCtx, output)
}
// Await waits for the DeleteItem to be fulfilled and then returns a DeleteItemOutput and error
func (op *DeleteItem) Await() (*ddb.DeleteItemOutput, error) {
out, err := op.BaseOperation.Await()
if out == nil {
return nil, err
}
return out.(*ddb.DeleteItemOutput), err
}
// NewDeleteItemInput creates a DeleteItemInput with a given table name and key
func NewDeleteItemInput(tableName *string, key map[string]ddbTypes.AttributeValue) *ddb.DeleteItemInput {
return &ddb.DeleteItemInput{
Key: key,
TableName: tableName,
ReturnConsumedCapacity: ddbTypes.ReturnConsumedCapacityNone,
ReturnItemCollectionMetrics: ddbTypes.ReturnItemCollectionMetricsNone,
ReturnValues: ddbTypes.ReturnValueNone,
}
}
// DeleteItemBuilder is used for dynamically building a DeleteItemInput
type DeleteItemBuilder struct {
*ddb.DeleteItemInput
cnd condition.Builder
}
// NewDeleteItemBuilder creates a new DeleteItemInput with DeleteItemOpt
func NewDeleteItemBuilder(input *ddb.DeleteItemInput) *DeleteItemBuilder {
bld := &DeleteItemBuilder{}
if input != nil {
bld.DeleteItemInput = input
} else {
bld.DeleteItemInput = NewDeleteItemInput(nil, nil)
}
return bld
}
// SetKey sets the target key for the item to tbe deleted
func (bld *DeleteItemBuilder) SetKey(key map[string]ddbTypes.AttributeValue) *DeleteItemBuilder {
bld.Key = key
return bld
}
// AddCondition adds a condition to this update
// adding multiple conditions by calling this multiple times will join the conditions with
// an AND
func (bld *DeleteItemBuilder) AddCondition(cnd expression.ConditionBuilder) *DeleteItemBuilder {
bld.cnd.And(cnd)
return bld
}
// SetConditionExpression sets the ConditionExpression field's value.
func (bld *DeleteItemBuilder) SetConditionExpression(v string) *DeleteItemBuilder {
bld.ConditionExpression = &v
return bld
}
// SetExpressionAttributeNames sets the ExpressionAttributeNames field's value.
func (bld *DeleteItemBuilder) SetExpressionAttributeNames(v map[string]string) *DeleteItemBuilder {
bld.ExpressionAttributeNames = v
return bld
}
// SetExpressionAttributeValues sets the ExpressionAttributeValues field's value.
func (bld *DeleteItemBuilder) SetExpressionAttributeValues(v map[string]ddbTypes.AttributeValue) *DeleteItemBuilder {
bld.ExpressionAttributeValues = v
return bld
}
// SetReturnConsumedCapacity sets the ReturnConsumedCapacity field's value.
func (bld *DeleteItemBuilder) SetReturnConsumedCapacity(v ddbTypes.ReturnConsumedCapacity) *DeleteItemBuilder {
bld.ReturnConsumedCapacity = v
return bld
}
// SetReturnItemCollectionMetrics sets the ReturnItemCollectionMetrics field's value.
func (bld *DeleteItemBuilder) SetReturnItemCollectionMetrics(v ddbTypes.ReturnItemCollectionMetrics) *DeleteItemBuilder {
bld.ReturnItemCollectionMetrics = v
return bld
}
// SetReturnValues sets the ReturnValues field's value.
func (bld *DeleteItemBuilder) SetReturnValues(v ddbTypes.ReturnValue) *DeleteItemBuilder {
bld.ReturnValues = v
return bld
}
// SetTableName sets the TableName field's value.
func (bld *DeleteItemBuilder) SetTableName(v string) *DeleteItemBuilder {
bld.TableName = &v
return bld
}
// Build builds the dynamodb.DeleteItemInput
// returns error if expression builder returns an error
func (bld *DeleteItemBuilder) Build() (*ddb.DeleteItemInput, error) {
if !bld.cnd.Empty() {
expr := expression.NewBuilder().WithCondition(bld.cnd.Builder())
e, err := expr.Build()
if err != nil {
return nil, fmt.Errorf("DeleteItemInput.GetDynamoGlobalSecondaryIndex() encountered an error while attempting to build an expression: %v", err)
}
bld.ConditionExpression = e.Condition()
bld.ExpressionAttributeNames = e.Names()
bld.ExpressionAttributeValues = e.Values()
}
return bld.DeleteItemInput, nil
}