forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
381 lines (333 loc) · 10.5 KB
/
update.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package dynamo
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Update represents changes to an existing item.
// It uses the UpdateItem API.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html
type Update struct {
table Table
returnType string
hashKey string
hashValue *dynamodb.AttributeValue
rangeKey string
rangeValue *dynamodb.AttributeValue
set []string
add map[string]string
del map[string]string
remove map[string]struct{}
condition string
subber
err error
cc *ConsumedCapacity
}
// Update creates a new request to modify an existing item.
func (table Table) Update(hashKey string, value interface{}) *Update {
u := &Update{
table: table,
hashKey: hashKey,
set: make([]string, 0),
add: make(map[string]string),
del: make(map[string]string),
remove: make(map[string]struct{}),
}
u.hashValue, u.err = marshal(value, "")
return u
}
// Range specifies the range key (sort key) for the item to update.
func (u *Update) Range(name string, value interface{}) *Update {
var err error
u.rangeKey = name
u.rangeValue, err = marshal(value, "")
u.setError(err)
return u
}
// Set changes path to the given value.
// If value is an empty string or nil, path will be removed instead.
// Paths that are reserved words are automatically escaped.
// Use single quotes to escape complex values like 'User'.'Count'.
func (u *Update) Set(path string, value interface{}) *Update {
if isNil(value) {
return u.Remove(path)
}
path, err := u.escape(path)
u.setError(err)
expr, err := u.subExpr("🝕 = ?", path, value)
u.setError(err)
u.set = append(u.set, expr)
return u
}
// SetSet changes a set at the given path to the given value.
// SetSet marshals value to a string set, number set, or binary set.
// If value is of zero length or nil, path will be removed instead.
// Paths that are reserved words are automatically escaped.
// Use single quotes to escape complex values like 'User'.'Count'.
func (u *Update) SetSet(path string, value interface{}) *Update {
v, err := marshal(value, "set")
if v == nil && err == nil {
// empty set
return u.Remove(path)
}
u.setError(err)
path, err = u.escape(path)
u.setError(err)
expr, err := u.subExpr("🝕 = ?", path, v)
u.setError(err)
u.set = append(u.set, expr)
return u
}
// SetIfNotExists changes path to the given value, if it does not already exist.
func (u *Update) SetIfNotExists(path string, value interface{}) *Update {
path, err := u.escape(path)
u.setError(err)
expr, err := u.subExpr("🝕 = if_not_exists(🝕, ?)", path, path, value)
u.setError(err)
u.set = append(u.set, expr)
return u
}
// SetExpr performs a custom set expression, substituting the args into expr as in filter expressions.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html#DDB-UpdateItem-request-UpdateExpression
// SetExpr("MyMap.$.$ = ?", key1, key2, val)
// SetExpr("'Counter' = 'Counter' + ?", 1)
func (u *Update) SetExpr(expr string, args ...interface{}) *Update {
expr, err := u.subExpr(expr, args...)
u.setError(err)
u.set = append(u.set, expr)
return u
}
// Append appends value to the end of the list specified by path.
func (u *Update) Append(path string, value interface{}) *Update {
path, err := u.escape(path)
u.setError(err)
expr, err := u.subExpr("🝕 = list_append(🝕, ?)", path, path, value)
u.setError(err)
u.set = append(u.set, expr)
return u
}
// Prepend inserts value to the beginning of the list specified by path.
func (u *Update) Prepend(path string, value interface{}) *Update {
path, err := u.escape(path)
u.setError(err)
expr, err := u.subExpr("🝕 = list_append(?, 🝕)", path, value, path)
u.setError(err)
u.set = append(u.set, expr)
return u
}
// Add adds value to path.
// Path can be a number or a set.
// If path represents a number, value is atomically added to the number.
// If path represents a set, value must be a slice, a map[*]struct{}, or map[*]bool.
// Path must be a top-level attribute.
func (u *Update) Add(path string, value interface{}) *Update {
path, err := u.escape(path)
u.setError(err)
vsub, err := u.subValue(value, "set")
u.setError(err)
u.add[path] = vsub
return u
}
// AddStringsToSet adds the given values to the string set specified by path.
func (u *Update) AddStringsToSet(path string, values ...string) *Update {
return u.Add(path, values)
}
// AddIntsToSet adds the given values to the number set specified by path.
func (u *Update) AddIntsToSet(path string, values ...int) *Update {
return u.Add(path, values)
}
// AddFloatsToSet adds the given values to the number set specified by path.
func (u *Update) AddFloatsToSet(path string, values ...float64) *Update {
return u.Add(path, values)
}
func (u *Update) delete(path string, value interface{}) *Update {
path, err := u.escape(path)
u.setError(err)
vsub, err := u.subValue(value, "set")
u.setError(err)
u.del[path] = vsub
return u
}
// DeleteStringsFromSet deletes the given values from the string set specified by path.
func (u *Update) DeleteStringsFromSet(path string, values ...string) *Update {
return u.delete(path, values)
}
// DeleteIntsFromSet deletes the given values from the number set specified by path.
func (u *Update) DeleteIntsFromSet(path string, values ...int) *Update {
return u.delete(path, values)
}
// DeleteFloatsFromSet deletes the given values from the number set specified by path.
func (u *Update) DeleteFloatsFromSet(path string, values ...float64) *Update {
return u.delete(path, values)
}
// Remove removes the paths from this item, deleting the specified attributes.
func (u *Update) Remove(paths ...string) *Update {
for _, n := range paths {
n, err := u.escape(n)
u.setError(err)
u.remove[n] = struct{}{}
}
return u
}
// RemoveExpr performs a custom remove expression, substituting the args into expr as in filter expressions.
// RemoveExpr("MyList[$]", 5)
func (u *Update) RemoveExpr(expr string, args ...interface{}) *Update {
expr, err := u.subExpr(expr, args...)
u.setError(err)
u.remove[expr] = struct{}{}
return u
}
// If specifies a conditional expression for this update to succeed.
// Use single quotes to specificy reserved names inline (like 'Count').
// Use the placeholder ? within the expression to substitute values, and use $ for names.
// You need to use quoted or placeholder names when the name is a reserved word in DynamoDB.
// Multiple calls to Update will be combined with AND.
func (u *Update) If(expr string, args ...interface{}) *Update {
expr = wrapExpr(expr)
cond, err := u.subExpr(expr, args...)
u.setError(err)
if u.condition != "" {
u.condition += " AND "
}
u.condition += cond
return u
}
// ConsumedCapacity will measure the throughput capacity consumed by this operation and add it to cc.
func (u *Update) ConsumedCapacity(cc *ConsumedCapacity) *Update {
u.cc = cc
return u
}
// Run executes this update.
func (u *Update) Run() error {
ctx, cancel := defaultContext()
defer cancel()
return u.RunWithContext(ctx)
}
func (u *Update) RunWithContext(ctx aws.Context) error {
u.returnType = "NONE"
_, err := u.run(ctx)
return err
}
// Value executes this update, encoding out with the new value.
func (u *Update) Value(out interface{}) error {
ctx, cancel := defaultContext()
defer cancel()
return u.ValueWithContext(ctx, out)
}
func (u *Update) ValueWithContext(ctx aws.Context, out interface{}) error {
u.returnType = "ALL_NEW"
output, err := u.run(ctx)
if err != nil {
return err
}
return unmarshalItem(output.Attributes, out)
}
// OldValue executes this update, encoding out with the previous value.
func (u *Update) OldValue(out interface{}) error {
ctx, cancel := defaultContext()
defer cancel()
return u.OldValueWithContext(ctx, out)
}
func (u *Update) OldValueWithContext(ctx aws.Context, out interface{}) error {
u.returnType = "ALL_OLD"
output, err := u.run(ctx)
if err != nil {
return err
}
return unmarshalItem(output.Attributes, out)
}
func (u *Update) run(ctx aws.Context) (*dynamodb.UpdateItemOutput, error) {
if u.err != nil {
return nil, u.err
}
input := u.updateInput()
var output *dynamodb.UpdateItemOutput
err := retry(ctx, func() error {
var err error
output, err = u.table.db.client.UpdateItemWithContext(ctx, input)
return err
})
if u.cc != nil {
addConsumedCapacity(u.cc, output.ConsumedCapacity)
}
return output, err
}
func (u *Update) updateInput() *dynamodb.UpdateItemInput {
input := &dynamodb.UpdateItemInput{
TableName: &u.table.name,
Key: u.key(),
UpdateExpression: u.updateExpr(),
ExpressionAttributeNames: u.nameExpr,
ExpressionAttributeValues: u.valueExpr,
ReturnValues: &u.returnType,
}
if u.condition != "" {
input.ConditionExpression = &u.condition
}
if u.cc != nil {
input.ReturnConsumedCapacity = aws.String(dynamodb.ReturnConsumedCapacityIndexes)
}
return input
}
func (u *Update) writeTxItem() (*dynamodb.TransactWriteItem, error) {
if u.err != nil {
return nil, u.err
}
input := u.updateInput()
item := &dynamodb.TransactWriteItem{
Update: &dynamodb.Update{
TableName: input.TableName,
Key: input.Key,
UpdateExpression: input.UpdateExpression,
ExpressionAttributeNames: input.ExpressionAttributeNames,
ExpressionAttributeValues: input.ExpressionAttributeValues,
ConditionExpression: input.ConditionExpression,
// TODO: return values
},
}
return item, nil
}
func (u *Update) key() map[string]*dynamodb.AttributeValue {
key := map[string]*dynamodb.AttributeValue{
u.hashKey: u.hashValue,
}
if u.rangeKey != "" {
key[u.rangeKey] = u.rangeValue
}
return key
}
func (u *Update) updateExpr() *string {
var expr []string
if len(u.set) > 0 {
expr = append(expr, "SET", strings.Join(u.set, ", "))
}
adds := make([]string, 0, len(u.add))
for k, v := range u.add {
adds = append(adds, fmt.Sprintf("%s %s", k, v))
}
if len(adds) > 0 {
expr = append(expr, "ADD", strings.Join(adds, ", "))
}
dels := make([]string, 0, len(u.del))
for k, v := range u.del {
dels = append(dels, fmt.Sprintf("%s %s", k, v))
}
if len(dels) > 0 {
expr = append(expr, "DELETE", strings.Join(dels, ", "))
}
rems := make([]string, 0, len(u.remove))
for k := range u.remove {
rems = append(rems, k)
}
if len(rems) > 0 {
expr = append(expr, "REMOVE", strings.Join(rems, ", "))
}
joined := strings.Join(expr, " ")
return &joined
}
func (u *Update) setError(err error) {
if u.err == nil {
u.err = err
}
}