use of software.amazon.awssdk.enhanced.dynamodb.mapper.UpdateBehavior in project aws-sdk-java-v2 by aws.
the class UpdateItemOperation method generateUpdateExpression.
private static Expression generateUpdateExpression(Map<String, AttributeValue> attributeValuesToUpdate, TableMetadata tableMetadata) {
// Sort the updates into 'SET' or 'REMOVE' based on null value
List<String> updateSetActions = new ArrayList<>();
List<String> updateRemoveActions = new ArrayList<>();
attributeValuesToUpdate.forEach((key, value) -> {
if (!isNullAttributeValue(value)) {
UpdateBehavior updateBehavior = UpdateBehaviorTag.resolveForAttribute(key, tableMetadata);
updateSetActions.add(EXPRESSION_KEY_MAPPER.apply(key) + " = " + updateExpressionMapperForBehavior(updateBehavior).apply(key));
} else {
updateRemoveActions.add(EXPRESSION_KEY_MAPPER.apply(key));
}
});
// Combine the expressions
List<String> updateActions = new ArrayList<>();
if (!updateSetActions.isEmpty()) {
updateActions.add("SET " + String.join(", ", updateSetActions));
}
if (!updateRemoveActions.isEmpty()) {
updateActions.add("REMOVE " + String.join(", ", updateRemoveActions));
}
String updateExpression = String.join(" ", updateActions);
Map<String, AttributeValue> expressionAttributeValues = attributeValuesToUpdate.entrySet().stream().filter(entry -> !isNullAttributeValue(entry.getValue())).collect(Collectors.toMap(entry -> EXPRESSION_VALUE_KEY_MAPPER.apply(entry.getKey()), Map.Entry::getValue));
Map<String, String> expressionAttributeNames = attributeValuesToUpdate.keySet().stream().collect(Collectors.toMap(EXPRESSION_KEY_MAPPER, key -> key));
return Expression.builder().expression(updateExpression).expressionValues(Collections.unmodifiableMap(expressionAttributeValues)).expressionNames(expressionAttributeNames).build();
}
Aggregations