-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_expression.go
461 lines (394 loc) · 14 KB
/
aws_expression.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package dynamodb
import (
"errors"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
// Operator DynamoDB operators for string and numbers
type Operator int
const (
// EQUAL equality operator
EQUAL Operator = iota + 1
// LT less than operator
LT
// LE less or equal
LE
// GT greater than
GT
// GE greater or equal
GE
// BETWEEN upper and lower
BETWEEN
)
// FromToDate which to be used in constructing the between operations for date
type FromToDate struct {
FromDate uint64
ToDate uint64
}
// AwsExpressionWrapper ...
type AwsExpressionWrapper struct {
updateExpression expression.UpdateBuilder
conditionExpression expression.ConditionBuilder
keyCondition expression.KeyConditionBuilder
projection expression.ProjectionBuilder
partitionKeyValue *dynamodb.AttributeValue
sortKeyValue *dynamodb.AttributeValue
exclusiveStartKey map[string]*dynamodb.AttributeValue
scanIndexForward *bool
partitionKeyName string
sortKeyName string
dynamoDBTable string
dynamoDBIndex string
limit *int64
}
// NewExpressionWrapper creates new expression wrapper
func NewExpressionWrapper(tableName string) *AwsExpressionWrapper {
return &AwsExpressionWrapper{
updateExpression: expression.UpdateBuilder{},
conditionExpression: expression.ConditionBuilder{},
keyCondition: expression.KeyConditionBuilder{},
projection: expression.ProjectionBuilder{},
dynamoDBTable: tableName,
}
}
// WithIndexName set the index name, this is used in case get is reading an index
func (expr *AwsExpressionWrapper) WithIndexName(indexName string) *AwsExpressionWrapper {
expr.dynamoDBIndex = indexName
return expr
}
// WithProjection add projected fields to retrieve
func (expr *AwsExpressionWrapper) WithProjection(fields ...string) *AwsExpressionWrapper {
names := make([]expression.NameBuilder, 0)
for _, field := range fields {
if field != "" {
names = append(names, expression.Name(field))
}
}
if len(names) == 1 {
expr.projection = expression.NamesList(names[0])
}
if len(names) > 1 {
expr.projection = expression.NamesList(names[0], names[1:]...)
}
return expr
}
// WithUpdateField sets update expression value for a specific field name
func (expr *AwsExpressionWrapper) WithUpdateField(name string, value interface{}) *AwsExpressionWrapper {
if reflect.DeepEqual(expr.updateExpression, expression.UpdateBuilder{}) {
expr.updateExpression = expression.Set(
expression.Name(name),
expression.Value(value),
)
return expr
}
expr.updateExpression.Set(
expression.Name(name),
expression.Value(value),
)
return expr
}
// WithLimit sets the maximum number of items to evaluate
func (expr *AwsExpressionWrapper) WithLimit(limit int64) *AwsExpressionWrapper {
expr.limit = aws.Int64(limit)
return expr
}
// WithCondition sets the initial condition
func (expr *AwsExpressionWrapper) WithCondition(
name string, value interface{}, operator Operator,
) *AwsExpressionWrapper {
expr.conditionExpression = createCondition(name, value, operator)
return expr
}
// AndCondition adds to the initial condition an AND condition if exists or create new condition
func (expr *AwsExpressionWrapper) AndCondition(
name string, value interface{}, operator Operator,
) *AwsExpressionWrapper {
if reflect.DeepEqual(expr.conditionExpression, expression.ConditionBuilder{}) {
expr.WithCondition(name, value, operator)
return expr
}
condition := createCondition(name, value, operator)
newConditionExpr := expr.conditionExpression.And(condition)
expr.conditionExpression = newConditionExpr
return expr
}
// OrCondition adds to the initial condition an OR condition if exists or create new condition
func (expr *AwsExpressionWrapper) OrCondition(
name string, value interface{}, operator Operator,
) *AwsExpressionWrapper {
if reflect.DeepEqual(expr.conditionExpression, expression.ConditionBuilder{}) {
expr.WithCondition(name, value, operator)
return expr
}
condition := createCondition(name, value, operator)
newConditionExpr := expr.conditionExpression.Or(condition)
expr.conditionExpression = newConditionExpr
return expr
}
// WithKeyCondition sets the initial key condition
// first key should always be using EQUAL operator as it represents the partition key
func (expr *AwsExpressionWrapper) WithKeyCondition(
name string, value interface{}, operator Operator,
) *AwsExpressionWrapper {
expr.keyCondition = createKeyCondition(name, value, operator)
return expr
}
// AndKeyCondition adds to the initial condition an AND condition if exists or create composite condition with and
func (expr *AwsExpressionWrapper) AndKeyCondition(
name string, value interface{}, operator Operator,
) *AwsExpressionWrapper {
if reflect.DeepEqual(expr.keyCondition, expression.KeyConditionBuilder{}) {
expr.WithKeyCondition(name, value, operator)
return expr
}
cond1 := expr.keyCondition
cond2 := createKeyCondition(name, value, operator)
expr.keyCondition = expression.KeyAnd(cond1, cond2)
return expr
}
// WithPartitionKey adds partition key
func (expr *AwsExpressionWrapper) WithPartitionKey(pKey string, pValue string) *AwsExpressionWrapper {
expr.partitionKeyName = pKey
if len(pValue) > 0 {
expr.partitionKeyValue = &dynamodb.AttributeValue{S: aws.String(pValue)}
}
return expr
}
// WithSortingKey adds sorting key if available
func (expr *AwsExpressionWrapper) WithSortingKey(sKey string, sValue string) *AwsExpressionWrapper {
expr.sortKeyName = sKey
if len(sValue) > 0 {
expr.sortKeyValue = &dynamodb.AttributeValue{S: aws.String(sValue)}
}
return expr
}
// WithLastEvaluatedKey defines the last evaluated key
func (expr *AwsExpressionWrapper) WithLastEvaluatedKey(pKeyName, pKeyVal string, sKeyName, sKeyVal *string) *AwsExpressionWrapper {
lastEvaluatedKey := make(map[string]*dynamodb.AttributeValue)
lastEvaluatedKey[pKeyName] = &dynamodb.AttributeValue{
S: aws.String(pKeyVal),
}
if sKeyName != nil && sKeyVal != nil {
lastEvaluatedKey[*sKeyName] = &dynamodb.AttributeValue{
S: aws.String(*sKeyVal),
}
}
expr.exclusiveStartKey = lastEvaluatedKey
return expr
}
// WithScanIndexForward DESC should be FALSE and ASC should be TRUE
// it is used to return th values sorted on the basis of sorting key
func (expr *AwsExpressionWrapper) WithScanIndexForward(asc bool) *AwsExpressionWrapper {
expr.scanIndexForward = aws.Bool(asc)
return expr
}
// WithExlusiveStartingKey to return Starting key of next page, key will be in form of structure
func (expr *AwsExpressionWrapper) WithExlusiveStartingKey(lastEvaluatedKey map[string]*dynamodb.AttributeValue) *AwsExpressionWrapper {
expr.exclusiveStartKey = lastEvaluatedKey
return expr
}
// BuildUpdateInput build the update input out of the update expression
func (expr *AwsExpressionWrapper) BuildUpdateInput() (*dynamodb.UpdateItemInput, error) {
if reflect.DeepEqual(expr.updateExpression, expression.UpdateBuilder{}) {
return nil, errors.New("their is nothing set to be updated, please use WithUpdateField")
}
keys, keyErr := expr.CreateQueryKeys()
if keyErr != nil {
return nil, keyErr
}
builder := expression.NewBuilder().WithUpdate(expr.updateExpression)
awsExpressionBuilder, err := builder.Build()
return &dynamodb.UpdateItemInput{
ExpressionAttributeNames: awsExpressionBuilder.Names(),
ExpressionAttributeValues: awsExpressionBuilder.Values(),
UpdateExpression: awsExpressionBuilder.Update(),
Key: keys,
TableName: aws.String(expr.dynamoDBTable),
}, err
}
// BuildQueryInput builds the expression and return the input to be used for the get
func (expr *AwsExpressionWrapper) BuildQueryInput() (*dynamodb.QueryInput, error) {
builder := expression.NewBuilder()
// check for available condition
if !reflect.DeepEqual(expr.conditionExpression, expression.ConditionBuilder{}) {
builder = builder.WithFilter(expr.conditionExpression)
}
if !reflect.DeepEqual(expr.projection, expression.ProjectionBuilder{}) {
builder = builder.WithProjection(expr.projection)
}
awsExpressionBuilder, err := builder.
WithKeyCondition(expr.keyCondition).
Build()
if err != nil {
return nil, err
}
input := dynamodb.QueryInput{
ExpressionAttributeNames: awsExpressionBuilder.Names(),
ExpressionAttributeValues: awsExpressionBuilder.Values(),
KeyConditionExpression: awsExpressionBuilder.KeyCondition(),
TableName: aws.String(expr.dynamoDBTable),
}
// if there is a projection already defined add the projection
if awsExpressionBuilder.Projection() != nil {
input.ProjectionExpression = awsExpressionBuilder.Projection()
}
// if there is a filter already defined add the filter
if !reflect.DeepEqual(expr.conditionExpression, expression.ConditionBuilder{}) {
input.FilterExpression = awsExpressionBuilder.Filter()
}
// check if index is available
if len(expr.dynamoDBIndex) > 0 {
input.IndexName = aws.String(expr.dynamoDBIndex)
}
if expr.scanIndexForward != nil {
input.ScanIndexForward = expr.scanIndexForward
}
if expr.limit != nil && *expr.limit >= 1 {
input.Limit = expr.limit
}
if len(expr.exclusiveStartKey) > 0 {
input.ExclusiveStartKey = expr.exclusiveStartKey
}
return &input, nil
}
// BuildScanInput create scan query expression
func (expr *AwsExpressionWrapper) BuildScanInput() (*dynamodb.ScanInput, error) {
if len(expr.dynamoDBTable) == 0 {
return nil, errors.New("missing table-name")
}
input := dynamodb.ScanInput{
TableName: aws.String(expr.dynamoDBTable),
}
builder := expression.NewBuilder()
// check for available condition
if !reflect.DeepEqual(expr.conditionExpression, expression.ConditionBuilder{}) {
builder = builder.WithFilter(expr.conditionExpression)
awsExpressionBuilder, _ := builder.Build()
input = dynamodb.ScanInput{
ExpressionAttributeNames: awsExpressionBuilder.Names(),
ExpressionAttributeValues: awsExpressionBuilder.Values(),
FilterExpression: awsExpressionBuilder.Filter(),
TableName: aws.String(expr.dynamoDBTable),
}
}
if !reflect.DeepEqual(expr.keyCondition, expression.ConditionBuilder{}) {
builder = builder.WithKeyCondition(expr.keyCondition)
awsExpressionBuilder, _ := builder.Build()
input = dynamodb.ScanInput{
ExpressionAttributeNames: awsExpressionBuilder.Names(),
ExpressionAttributeValues: awsExpressionBuilder.Values(),
FilterExpression: awsExpressionBuilder.KeyCondition(),
TableName: aws.String(expr.dynamoDBTable),
}
}
if expr.limit != nil && *expr.limit >= 1 {
input.Limit = expr.limit
}
if len(expr.exclusiveStartKey) > 0 {
input.ExclusiveStartKey = expr.exclusiveStartKey
}
return &input, nil
}
// BuildGetInput build get input expression
func (expr *AwsExpressionWrapper) BuildGetInput() (*dynamodb.GetItemInput, error) {
if len(expr.dynamoDBTable) < 1 {
return nil, errors.New("missing table name")
}
keys, err := expr.CreateQueryKeys()
if err != nil {
return nil, err
}
return &dynamodb.GetItemInput{
TableName: aws.String(expr.dynamoDBTable),
Key: keys,
}, nil
}
// BuildDeleteInput build delete input
func (expr *AwsExpressionWrapper) BuildDeleteInput() (*dynamodb.DeleteItemInput, error) {
if len(expr.dynamoDBTable) < 1 {
return nil, errors.New("missing table name")
}
keys, keyErr := expr.CreateQueryKeys()
if keyErr != nil {
return nil, keyErr
}
input := dynamodb.DeleteItemInput{
Key: keys,
TableName: aws.String(expr.dynamoDBTable),
}
if !reflect.DeepEqual(expr.conditionExpression, expression.ConditionBuilder{}) {
awsExpressionBuilder, err := expression.NewBuilder().
WithCondition(expr.conditionExpression).
Build()
if err != nil {
return &input, err
}
input.ExpressionAttributeNames = awsExpressionBuilder.Names()
input.ExpressionAttributeValues = awsExpressionBuilder.Values()
input.ConditionExpression = awsExpressionBuilder.Condition()
}
return &input, nil
}
// CreateQueryKeys creates a query keys
func (expr *AwsExpressionWrapper) CreateQueryKeys() (map[string]*dynamodb.AttributeValue, error) {
if len(expr.partitionKeyName) < 1 || expr.partitionKeyValue == nil {
return nil, errors.New("missing partition key")
}
attributeValues := map[string]*dynamodb.AttributeValue{
expr.partitionKeyName: expr.partitionKeyValue,
}
if len(expr.sortKeyName) > 0 {
attributeValues[expr.sortKeyName] = expr.sortKeyValue
}
return attributeValues, nil
}
// createCondition creates the condition builder
func createCondition(name string, value interface{}, operator Operator) expression.ConditionBuilder {
// check if the interface can be cast to FromToDate as the operation will be different
switch obj := value.(type) {
case FromToDate:
switch operator {
case BETWEEN:
return expression.Name(name).Between(
expression.Value(obj.FromDate),
expression.Value(obj.ToDate),
)
default:
// failsafe as the minimum value is going to be 0 for epoch
return expression.Name(name).GreaterThanEqual(expression.Value(obj.FromDate))
}
}
switch operator {
case EQUAL:
return expression.Name(name).Equal(expression.Value(value))
case LT:
return expression.Name(name).LessThan(expression.Value(value))
case LE:
return expression.Name(name).LessThanEqual(expression.Value(value))
case GT:
return expression.Name(name).GreaterThan(expression.Value(value))
case GE:
return expression.Name(name).GreaterThanEqual(expression.Value(value))
default:
return expression.Name(name).Equal(expression.Value(value))
}
}
// createKeyCondition creates the condition builder
func createKeyCondition(name string, value interface{}, operator Operator) expression.KeyConditionBuilder {
switch operator {
case EQUAL:
return expression.Key(name).Equal(expression.Value(value))
case LT:
return expression.Key(name).LessThan(expression.Value(value))
case LE:
return expression.Key(name).LessThanEqual(expression.Value(value))
case GT:
return expression.Key(name).GreaterThan(expression.Value(value))
case GE:
return expression.Key(name).GreaterThanEqual(expression.Value(value))
default:
return expression.Key(name).Equal(expression.Value(value))
}
}