Skip to content

Commit

Permalink
Addressed pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudh9391 committed Sep 3, 2024
1 parent 9ba36aa commit 227d00f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class EnhancedClientUtils {
private static final Set<Character> SPECIAL_CHARACTERS = Stream.of(
'*', '.', '-', '#', '+', ':', '/', '(', ')', ' ',
'&', '<', '>', '?', '=', '!', '@', '%', '$', '|').collect(Collectors.toSet());
private static final Pattern PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE);
private static final Pattern NESTED_OBJECT_PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE);

private EnhancedClientUtils() {

Expand Down Expand Up @@ -81,7 +81,7 @@ private static boolean isNestedAttribute(String key) {
public static String keyRef(String key) {
String cleanAttributeName = cleanAttributeName(key);
cleanAttributeName = isNestedAttribute(cleanAttributeName) ?
PATTERN.matcher(cleanAttributeName).replaceAll(".#AMZN_MAPPED_")
NESTED_OBJECT_PATTERN.matcher(cleanAttributeName).replaceAll(".#AMZN_MAPPED_")
: cleanAttributeName;
return "#AMZN_MAPPED_" + cleanAttributeName;
}
Expand All @@ -92,7 +92,7 @@ public static String keyRef(String key) {
public static String valueRef(String value) {
String cleanAttributeName = cleanAttributeName(value);
cleanAttributeName = isNestedAttribute(cleanAttributeName) ?
PATTERN.matcher(cleanAttributeName).replaceAll("_")
NESTED_OBJECT_PATTERN.matcher(cleanAttributeName).replaceAll("_")
: cleanAttributeName;
return ":AMZN_MAPPED_" + cleanAttributeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,8 @@ private Map<String, AttributeValue> nestedItemToMap(Map<String, AttributeValue>
}

private boolean isNotEmptyMap(Map<String, AttributeValue> map) {
if (map.isEmpty()) {
return false;
}

// Checks if a fully empty map is being set. If that is the case, no input transformations are applied to the map
for (Map.Entry<String, AttributeValue> entry : map.entrySet()) {
if (attributeValueNonNullOrShouldWriteNull(entry.getValue())) {
return true;
}
}
return false;
return !map.isEmpty() && map.values().stream()
.anyMatch(this::attributeValueNonNullOrShouldWriteNull);
}

private boolean attributeValueNonNullOrShouldWriteNull(AttributeValue attributeValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static List<RemoveAction> removeActionsFor(Map<String, AttributeValue> a
private static RemoveAction remove(String attributeName) {
return RemoveAction.builder()
.path(keyRef(attributeName))
.expressionNames(expressionNamesFor(attributeName))
.expressionNames(Collections.singletonMap(keyRef(attributeName), attributeName))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.time.Instant;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.After;
Expand All @@ -18,7 +19,10 @@
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.NestedRecordWithUpdateBehavior;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.RecordWithUpdateBehaviors;
import software.amazon.awssdk.enhanced.dynamodb.internal.client.ExtensionResolver;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;

public class UpdateBehaviorTest extends LocalDynamoDbSyncTestBase {
private static final Instant INSTANT_1 = Instant.parse("2020-05-03T10:00:00Z");
Expand Down Expand Up @@ -303,17 +307,31 @@ public void when_updatingNestedNonScalarObject_DynamoDBExceptionIsThrown() {
}

@Test
public void test() {
String randomId = "id123";
public void when_emptyNestedRecordIsSet_emotyMapIsStoredInTable() {
String key = "id123";

RecordWithUpdateBehaviors record = new RecordWithUpdateBehaviors();
record.setId(randomId);
record.setId(key);
record.setNestedRecord(new NestedRecordWithUpdateBehavior());

mappedTable.updateItem(r -> r.item(record).ignoreNulls(true));

RecordWithUpdateBehaviors persistedRecord = mappedTable.getItem(r -> r.key(k -> k.partitionValue("id123")));
assertThat(persistedRecord.getNestedRecord()).isNull();
GetItemResponse getItemResponse = getDynamoDbClient().getItem(GetItemRequest.builder()
.key(Collections.singletonMap("id",
AttributeValue.fromS(key)))
.tableName(getConcreteTableName("table-name"))
.build());

assertThat(getItemResponse.item().get("nestedRecord")).isNotNull();
assertThat(getItemResponse.item().get("nestedRecord").toString()).isEqualTo("AttributeValue(M={nestedTimeAttribute"
+ "=AttributeValue(NUL=true), "
+ "nestedRecord=AttributeValue(NUL=true), "
+ "attribute=AttributeValue(NUL=true), "
+ "id=AttributeValue(NUL=true), "
+ "nestedUpdateBehaviorAttribute=AttributeValue"
+ "(NUL=true), nestedCounter=AttributeValue"
+ "(NUL=true), nestedVersionedAttribute"
+ "=AttributeValue(NUL=true)})");
}


Expand Down

0 comments on commit 227d00f

Please sign in to comment.