Reputation: 133
expr := expression.Name("field1").Equal(expression.Value("val1"))
condition, err := expression.NewBuilder().WithCondition(expr).Build()
if err != nil { return err }
updateExpr := expression.Set(expression.Name("field2"), expression.Value("field2"))
update, err := expression.NewBuilder().WithUpdate(updateExpr).Build()
updateItemInput := &dynamodb.UpdateItemInput {
TableName : aws.String("foobar"),
Key: partitionKey,
ConditionExpression: condition.Condition(),
UpdateExpression: update.Update(),
ExpressionAttributeNames: update.Names(),
ExpressionAttributeValues: update.Values(),
ReturnValues: aws.String("UPDATED_NEW"),
}
Above is what updateItemInput
looks like right now. What I am trying to achieive with this is :
if field1 == "value1" then
set field2 = "value2"
Needless to say, this fails since the ExpressionAttributeNames
and
ExpressionAttributeValues
doesnot have the context of condition
.
What I have seen online and on StackOverflow is that people mostly write ConditionExpression
string on their own, which I would want to avoid as much as possible.
So is there any way I can use dynamodbiface.expression
to avoid writing the ConditionExpression
manually?
Upvotes: 0
Views: 842
Reputation: 1111
You just need a single expression and then you use the appropriate methods based on which input values you're trying to populate:
ex, e := expression.NewBuilder().
WithCondition(expression.Name("field1").Equal(expression.Value("val1"))).
WithUpdate(expression.
Set(
expression.Name("field2"),
expression.Value("value2")).
Build()
if e != nil {
return e
}
updateItemInput := &dynamodb.UpdateItemInput{
TableName: aws.String("foobar"),
Key: partitionKey,
ConditionExpression: ex.Condition(),
ExpressionAttributeNames: ex.Names(),
ExpressionAttributeValues: ex.Values(),
UpdateExpression: ex.Update(),
ReturnValues: aws.String("UPDATED_NEW"),
}
This is a slightly modified version of something I have working in production. I can't confirm that ReturnValues
works as I don't use that but the rest is pretty much copy/paste from working code.
Upvotes: 1