-
Notifications
You must be signed in to change notification settings - Fork 45
/
dynamodb_update.go
50 lines (43 loc) · 1.54 KB
/
dynamodb_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
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package utils
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// DynamoUpdateExpression helps build update expression
type DynamoUpdateExpression struct {
Expression string
ExpressionAttributeNames map[string]*string
ExpressionAttributeValues map[string]*dynamodb.AttributeValue
}
// NewDynamoUpdateExpression create DynamoUpdateExpression builder
func NewDynamoUpdateExpression() *DynamoUpdateExpression {
return &DynamoUpdateExpression{
Expression: "",
ExpressionAttributeNames: make(map[string]*string),
ExpressionAttributeValues: make(map[string]*dynamodb.AttributeValue),
}
}
// AddUpdateExpression the update expression if condition satisfied
func (d *DynamoUpdateExpression) AddUpdateExpression(columnUpdateExp string, condition bool) {
if condition {
if d.Expression == "" {
d.Expression = "SET " + columnUpdateExp
} else {
d.Expression = d.Expression + ", " + columnUpdateExp
}
}
}
// AddAttributeName adds the attribute if condition satisfied
func (d *DynamoUpdateExpression) AddAttributeName(name, columName string, condition bool) {
if condition {
d.ExpressionAttributeNames[name] = aws.String(columName)
}
}
// AddAttributeValue adds the attribute value if condition satisfied
func (d *DynamoUpdateExpression) AddAttributeValue(name string, val *dynamodb.AttributeValue, condition bool) {
if condition {
d.ExpressionAttributeValues[name] = val
}
}