Skip to content

Commit 34122af

Browse files
committed
Making annotation values nullable to know if initial value is explicitly set
1 parent 6398d67 commit 34122af

File tree

2 files changed

+293
-85
lines changed

2 files changed

+293
-85
lines changed

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/VersionedRecordExtension.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static StaticAttributeTag versionAttribute() {
8989
return VERSION_ATTRIBUTE;
9090
}
9191

92-
public static StaticAttributeTag versionAttribute(int startAt, int incrementBy) {
92+
public static StaticAttributeTag versionAttribute(Integer startAt, Integer incrementBy) {
9393
return new VersionAttribute(startAt, incrementBy);
9494
}
9595
}
@@ -98,15 +98,15 @@ private static final class VersionAttribute implements StaticAttributeTag {
9898
private static final String START_AT_METADATA_KEY = "VersionedRecordExtension:StartAt";
9999
private static final String INCREMENT_BY_METADATA_KEY = "VersionedRecordExtension:IncrementBy";
100100

101-
private final int startAt;
102-
private final int incrementBy;
101+
private final Integer startAt;
102+
private final Integer incrementBy;
103103

104104
private VersionAttribute() {
105-
this.startAt = 0;
106-
this.incrementBy = 1;
105+
this.startAt = null;
106+
this.incrementBy = null;
107107
}
108108

109-
private VersionAttribute(int startAt, int incrementBy) {
109+
private VersionAttribute(Integer startAt, Integer incrementBy) {
110110
this.startAt = startAt;
111111
this.incrementBy = incrementBy;
112112
}
@@ -120,6 +120,14 @@ public Consumer<StaticTableMetadata.Builder> modifyMetadata(String attributeName
120120
"is supported.", attributeName, attributeValueType.name()));
121121
}
122122

123+
if (startAt != null && startAt < 0) {
124+
throw new IllegalArgumentException("StartAt cannot be negative.");
125+
}
126+
127+
if (incrementBy != null && incrementBy < 1) {
128+
throw new IllegalArgumentException("IncrementBy must be greater than 0.");
129+
}
130+
123131
return metadata -> metadata
124132
.addCustomMetadataObject(CUSTOM_METADATA_KEY, attributeName)
125133
.addCustomMetadataObject(START_AT_METADATA_KEY, startAt)
@@ -137,7 +145,6 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex
137145
return WriteModification.builder().build();
138146
}
139147

140-
141148
Pair<AttributeValue, Expression> updates = getRecordUpdates(versionAttributeKey.get(), context);
142149

143150
// Unpack values from Pair
@@ -169,20 +176,13 @@ private Pair<AttributeValue, Expression> getRecordUpdates(String versionAttribut
169176
}
170177

171178
private boolean isInitialVersion(AttributeValue existingVersionValue, DynamoDbExtensionContext.BeforeWrite context) {
172-
if (isNullAttributeValue(existingVersionValue)) {
173-
return true;
174-
}
175-
176-
177179
Optional<Integer> versionStartAtFromAnnotation = context.tableMetadata()
178180
.customMetadataObject(VersionAttribute.START_AT_METADATA_KEY,
179181
Integer.class);
180182

181183
return isNullAttributeValue(existingVersionValue)
182-
|| (versionStartAtFromAnnotation.isPresent() &&
183-
getExistingVersion(existingVersionValue) == versionStartAtFromAnnotation.get())
184-
|| (!versionStartAtFromAnnotation.isPresent() &&
185-
getExistingVersion(existingVersionValue) == this.startAt);
184+
|| (versionStartAtFromAnnotation.isPresent() && getExistingVersion(existingVersionValue) == versionStartAtFromAnnotation.get())
185+
|| getExistingVersion(existingVersionValue) == this.startAt;
186186
}
187187

188188
private Pair<AttributeValue, Expression> createInitialRecord(String versionAttributeKey,
@@ -262,6 +262,9 @@ private Builder() {
262262
* @return the builder instance
263263
*/
264264
public Builder startAt(int startAt) {
265+
if (startAt < 0) {
266+
throw new IllegalArgumentException("StartAt cannot be negative.");
267+
}
265268
this.startAt = startAt;
266269
return this;
267270
}
@@ -274,6 +277,9 @@ public Builder startAt(int startAt) {
274277
* @return the builder instance
275278
*/
276279
public Builder incrementBy(int incrementBy) {
280+
if (incrementBy < 1) {
281+
throw new IllegalArgumentException("IncrementBy must be greater than 0.");
282+
}
277283
this.incrementBy = incrementBy;
278284
return this;
279285
}

0 commit comments

Comments
 (0)