Skip to content

Commit fa1ac02

Browse files
authored
Update EnhancedAttributeValue to accept Collection (#3516)
* Update EnhancedAttributeValue to accept Collection for fromSetOf* methods * Update EnhancedAttributeValue to accept Collection for fromSetOf* methods
1 parent cca889b commit fa1ac02

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "L-Applin",
5+
"description": "Update EnhancedAttributeValue to accept Collection"
6+
}

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/converter/attribute/EnhancedAttributeValue.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
package software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute;
1717

18+
import java.util.ArrayList;
1819
import java.util.Arrays;
20+
import java.util.Collection;
1921
import java.util.Collections;
2022
import java.util.List;
2123
import java.util.Map;
@@ -195,6 +197,22 @@ public static EnhancedAttributeValue fromSetOfStrings(String... setOfStringsValu
195197
return fromSetOfStrings(Arrays.asList(setOfStringsValue));
196198
}
197199

200+
/**
201+
* Create an {@link EnhancedAttributeValue} for a set-of-strings (ss) DynamoDB type.
202+
*
203+
* <p>
204+
* Equivalent to: {@code EnhancedAttributeValue.fromGeneratedAttributeValue(AttributeValue.builder().ss(...).build())}
205+
*
206+
* <p>
207+
* This call will fail with a {@link RuntimeException} if the provided value is null or contains a null value. Use
208+
* {@link #fromListOfAttributeValues(List)} for null values. This <i>will not</i> validate that there are no
209+
* duplicate values.
210+
*/
211+
public static EnhancedAttributeValue fromSetOfStrings(Collection<String> setOfStringsValue) {
212+
Validate.paramNotNull(setOfStringsValue, "setOfStringsValue");
213+
return fromSetOfStrings(new ArrayList<>(setOfStringsValue));
214+
}
215+
198216
/**
199217
* Create an {@link EnhancedAttributeValue} for a set-of-strings (ss) DynamoDB type.
200218
*
@@ -228,6 +246,22 @@ public static EnhancedAttributeValue fromSetOfNumbers(String... setOfNumbersValu
228246
return fromSetOfNumbers(Arrays.asList(setOfNumbersValue));
229247
}
230248

249+
/**
250+
* Create an {@link EnhancedAttributeValue} for a set-of-numbers (ns) DynamoDB type.
251+
*
252+
* <p>
253+
* Equivalent to: {@code EnhancedAttributeValue.fromGeneratedAttributeValue(AttributeValue.builder().ns(...).build())}
254+
*
255+
* <p>
256+
* This call will fail with a {@link RuntimeException} if the provided value is null or contains a null value. Use
257+
* {@link #fromListOfAttributeValues(List)} for null values. This <i>will not</i> validate that there are no
258+
* duplicate values.
259+
*/
260+
public static EnhancedAttributeValue fromSetOfNumbers(Collection<String> setOfNumbersValue) {
261+
Validate.paramNotNull(setOfNumbersValue, "setOfNumbersValue");
262+
return fromSetOfNumbers(new ArrayList<>(setOfNumbersValue));
263+
}
264+
231265
/**
232266
* Create an {@link EnhancedAttributeValue} for a set-of-numbers (ns) DynamoDB type.
233267
*
@@ -245,6 +279,22 @@ public static EnhancedAttributeValue fromSetOfNumbers(List<String> setOfNumbersV
245279
return new InternalBuilder().setOfNumbersValue(setOfNumbersValue).build();
246280
}
247281

282+
/**
283+
* Create an {@link EnhancedAttributeValue} for a set-of-bytes (bs) DynamoDB type.
284+
*
285+
* <p>
286+
* Equivalent to: {@code EnhancedAttributeValue.fromGeneratedAttributeValue(AttributeValue.builder().bs(...).build())}
287+
*
288+
* <p>
289+
* This call will fail with a {@link RuntimeException} if the provided value is null or contains a null value. Use
290+
* {@link #fromListOfAttributeValues(List)} for null values. This <i>will not</i> validate that there are no
291+
* duplicate values.
292+
*/
293+
public static EnhancedAttributeValue fromSetOfBytes(Collection<SdkBytes> setOfBytesValue) {
294+
Validate.paramNotNull(setOfBytesValue, "setOfBytesValue");
295+
return fromSetOfBytes(new ArrayList<>(setOfBytesValue));
296+
}
297+
248298
/**
249299
* Create an {@link EnhancedAttributeValue} for a set-of-bytes (bs) DynamoDB type.
250300
*

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/converters/attribute/EnhancedAttributeValueTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2020

2121
import java.util.Arrays;
22+
import java.util.Collection;
2223
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
@@ -66,12 +67,24 @@ public void simpleFromMethodsCreateCorrectTypes() {
6667
assertThat(v.type()).isEqualTo(AttributeValueType.SS);
6768
});
6869

70+
assertThat(EnhancedAttributeValue.fromSetOfStrings((Collection<String>) Arrays.asList("a", "b"))).satisfies(v -> {
71+
assertThat(v.isSetOfStrings()).isTrue();
72+
assertThat(v.asSetOfStrings()).containsExactly("a", "b");
73+
assertThat(v.type()).isEqualTo(AttributeValueType.SS);
74+
});
75+
6976
assertThat(EnhancedAttributeValue.fromSetOfNumbers(Arrays.asList("1", "2"))).satisfies(v -> {
7077
assertThat(v.isSetOfNumbers()).isTrue();
7178
assertThat(v.asSetOfNumbers()).containsExactly("1", "2");
7279
assertThat(v.type()).isEqualTo(AttributeValueType.NS);
7380
});
7481

82+
assertThat(EnhancedAttributeValue.fromSetOfNumbers((Collection<String>) Arrays.asList("1", "2"))).satisfies(v -> {
83+
assertThat(v.isSetOfNumbers()).isTrue();
84+
assertThat(v.asSetOfNumbers()).containsExactly("1", "2");
85+
assertThat(v.type()).isEqualTo(AttributeValueType.NS);
86+
});
87+
7588
assertThat(EnhancedAttributeValue.fromSetOfBytes(Arrays.asList(SdkBytes.fromUtf8String("foo"),
7689
SdkBytes.fromUtf8String("foo2")))).satisfies(v -> {
7790
assertThat(v.isSetOfBytes()).isTrue();
@@ -80,6 +93,15 @@ public void simpleFromMethodsCreateCorrectTypes() {
8093
assertThat(v.type()).isEqualTo(AttributeValueType.BS);
8194
});
8295

96+
assertThat(EnhancedAttributeValue
97+
.fromSetOfBytes((Collection<SdkBytes>) Arrays.asList(SdkBytes.fromUtf8String("foo"),
98+
SdkBytes.fromUtf8String("foo2")))).satisfies(v -> {
99+
assertThat(v.isSetOfBytes()).isTrue();
100+
assertThat(v.asSetOfBytes().get(0).asUtf8String()).isEqualTo("foo");
101+
assertThat(v.asSetOfBytes().get(1).asUtf8String()).isEqualTo("foo2");
102+
assertThat(v.type()).isEqualTo(AttributeValueType.BS);
103+
});
104+
83105
assertThat(EnhancedAttributeValue.fromListOfAttributeValues(Arrays.asList(AttributeValue.builder().s("foo").build(),
84106
AttributeValue.builder().bool(true).build()))).satisfies(v -> {
85107
assertThat(v.isListOfAttributeValues()).isTrue();

0 commit comments

Comments
 (0)