Skip to content

Commit b1013a3

Browse files
committed
Add EnhancedType parameters to static builder methods of StaticTableSchema and StaticImmitableTableSchema
1 parent 69dcfc4 commit b1013a3

File tree

11 files changed

+252
-20
lines changed

11 files changed

+252
-20
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "DynamoDB Enhanced Client",
3+
"contributor": "bmaizels",
4+
"type": "feature",
5+
"description": "Add EnhancedType parameters to static builder methods of StaticTableSchema and StaticImmitableTableSchema"
6+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ static <T> StaticTableSchema.Builder<T> builder(Class<T> itemClass) {
5353
return StaticTableSchema.builder(itemClass);
5454
}
5555

56+
/**
57+
* Returns a builder for the {@link StaticTableSchema} implementation of this interface which allows all attributes,
58+
* tags and table structure to be directly declared in the builder.
59+
*
60+
* @param itemType The {@link EnhancedType} of the item this {@link TableSchema} will map records to.
61+
* @param <T> The type of the item this {@link TableSchema} will map records to.
62+
* @return A newly initialized {@link StaticTableSchema.Builder}.
63+
*/
64+
static <T> StaticTableSchema.Builder<T> builder(EnhancedType<T> itemType) {
65+
return StaticTableSchema.builder(itemType);
66+
}
67+
5668
/**
5769
* Returns a builder for the {@link StaticImmutableTableSchema} implementation of this interface which allows all
5870
* attributes, tags and table structure to be directly declared in the builder.
@@ -69,6 +81,22 @@ static <T, B> StaticImmutableTableSchema.Builder<T, B> builder(Class<T> immutabl
6981
return StaticImmutableTableSchema.builder(immutableItemClass, immutableBuilderClass);
7082
}
7183

84+
/**
85+
* Returns a builder for the {@link StaticImmutableTableSchema} implementation of this interface which allows all
86+
* attributes, tags and table structure to be directly declared in the builder.
87+
*
88+
* @param immutableItemType The {@link EnhancedType} of the immutable item this {@link TableSchema} will map records to.
89+
* @param immutableBuilderType The {@link EnhancedType} of the class that can be used to construct immutable items this
90+
* {@link TableSchema} maps records to.
91+
* @param <T> The type of the immutable item this {@link TableSchema} will map records to.
92+
* @param <B> The type of the builder used by this {@link TableSchema} to construct immutable items with.
93+
* @return A newly initialized {@link StaticImmutableTableSchema.Builder}
94+
*/
95+
static <T, B> StaticImmutableTableSchema.Builder<T, B> builder(EnhancedType<T> immutableItemType,
96+
EnhancedType<B> immutableBuilderType) {
97+
return StaticImmutableTableSchema.builder(immutableItemType, immutableBuilderType);
98+
}
99+
72100
/**
73101
* Scans a bean class that has been annotated with DynamoDb bean annotations and then returns a
74102
* {@link BeanTableSchema} implementation of this interface that can map records to and from items of that bean

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/mapper/ImmutableAttribute.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ public static <T, B, R> Builder<T, B, R> builder(Class<T> itemClass,
9191
return new Builder<>(attributeType);
9292
}
9393

94+
/**
95+
* Constructs a new builder for this class using supplied types.
96+
* @param itemType The {@link EnhancedType} of the immutable item that this attribute composes.
97+
* @param builderType The {@link EnhancedType} of the builder for the immutable item that this attribute composes.
98+
* @param attributeType A {@link EnhancedType} that represents the type of the value this attribute stores.
99+
* @return A new typed builder for an attribute.
100+
*/
101+
public static <T, B, R> Builder<T, B, R> builder(EnhancedType<T> itemType,
102+
EnhancedType<B> builderType,
103+
EnhancedType<R> attributeType) {
104+
return new Builder<>(attributeType);
105+
}
106+
94107
/**
95108
* Constructs a new builder for this class using supplied types.
96109
* @param itemClass The class of the item that this attribute composes.

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/mapper/StaticAttribute.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ private StaticAttribute(Builder<T, R> builder) {
6969
* @return A new typed builder for an attribute.
7070
*/
7171
public static <T, R> Builder<T, R> builder(Class<T> itemClass, EnhancedType<R> attributeType) {
72-
return new Builder<>(itemClass, attributeType);
72+
return new Builder<>(EnhancedType.of(itemClass), attributeType);
73+
}
74+
75+
/**
76+
* Constructs a new builder for this class using supplied types.
77+
* @param itemType The {@link EnhancedType} of the item that this attribute composes.
78+
* @param attributeType A {@link EnhancedType} that represents the type of the value this attribute stores.
79+
* @return A new typed builder for an attribute.
80+
*/
81+
public static <T, R> Builder<T, R> builder(EnhancedType<T> itemType, EnhancedType<R> attributeType) {
82+
return new Builder<>(itemType, attributeType);
7383
}
7484

7585
/**
@@ -79,7 +89,7 @@ public static <T, R> Builder<T, R> builder(Class<T> itemClass, EnhancedType<R> a
7989
* @return A new typed builder for an attribute.
8090
*/
8191
public static <T, R> Builder<T, R> builder(Class<T> itemClass, Class<R> attributeClass) {
82-
return new Builder<>(itemClass, EnhancedType.of(attributeClass));
92+
return new Builder<>(EnhancedType.of(itemClass), EnhancedType.of(attributeClass));
8393
}
8494

8595
/**
@@ -146,8 +156,8 @@ ImmutableAttribute<T, T, R> toImmutableAttribute() {
146156
public static final class Builder<T, R> {
147157
private final ImmutableAttribute.Builder<T, T, R> delegateBuilder;
148158

149-
private Builder(Class<T> itemClass, EnhancedType<R> type) {
150-
this.delegateBuilder = ImmutableAttribute.builder(itemClass, itemClass, type);
159+
private Builder(EnhancedType<T> itemType, EnhancedType<R> type) {
160+
this.delegateBuilder = ImmutableAttribute.builder(itemType, itemType, type);
151161
}
152162

153163
private Builder(ImmutableAttribute.Builder<T, T, R> delegateBuilder) {

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/mapper/StaticImmutableTableSchema.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private StaticImmutableTableSchema(Builder<T, B> builder) {
210210
this.newBuilderSupplier = builder.newBuilderSupplier;
211211
this.buildItemFunction = builder.buildItemFunction;
212212
this.tableMetadata = tableMetadataBuilder.build();
213-
this.itemType = EnhancedType.of(builder.itemClass);
213+
this.itemType = builder.itemType;
214214
}
215215

216216
/**
@@ -220,7 +220,18 @@ private StaticImmutableTableSchema(Builder<T, B> builder) {
220220
* @return A newly initialized builder
221221
*/
222222
public static <T, B> Builder<T, B> builder(Class<T> itemClass, Class<B> builderClass) {
223-
return new Builder<>(itemClass, builderClass);
223+
return new Builder<>(EnhancedType.of(itemClass), EnhancedType.of(builderClass));
224+
}
225+
226+
/**
227+
* Creates a builder for a {@link StaticImmutableTableSchema} typed to specific immutable data item class.
228+
* @param itemType The {@link EnhancedType} of the immutable data item class object that the
229+
* {@link StaticImmutableTableSchema} is to map to.
230+
* @param builderType The builder {@link EnhancedType} that can be used to construct instances of the immutable data item.
231+
* @return A newly initialized builder
232+
*/
233+
public static <T, B> Builder<T, B> builder(EnhancedType<T> itemType, EnhancedType<B> builderType) {
234+
return new Builder<>(itemType, builderType);
224235
}
225236

226237
/**
@@ -230,8 +241,8 @@ public static <T, B> Builder<T, B> builder(Class<T> itemClass, Class<B> builderC
230241
*/
231242
@NotThreadSafe
232243
public static final class Builder<T, B> {
233-
private final Class<T> itemClass;
234-
private final Class<B> builderClass;
244+
private final EnhancedType<T> itemType;
245+
private final EnhancedType<B> builderType;
235246
private final List<ResolvedImmutableAttribute<T, B>> additionalAttributes = new ArrayList<>();
236247
private final List<FlattenedMapper<T, B, ?>> flattenedMappers = new ArrayList<>();
237248

@@ -242,9 +253,9 @@ public static final class Builder<T, B> {
242253
private List<AttributeConverterProvider> attributeConverterProviders =
243254
Collections.singletonList(ConverterProviderResolver.defaultConverterProvider());
244255

245-
private Builder(Class<T> itemClass, Class<B> builderClass) {
246-
this.itemClass = itemClass;
247-
this.builderClass = builderClass;
256+
private Builder(EnhancedType<T> itemType, EnhancedType<B> builderType) {
257+
this.itemType = itemType;
258+
this.builderType = builderType;
248259
}
249260

250261
/**
@@ -285,7 +296,7 @@ public <R> Builder<T, B> addAttribute(EnhancedType<R> attributeType,
285296
Consumer<ImmutableAttribute.Builder<T, B, R>> immutableAttribute) {
286297

287298
ImmutableAttribute.Builder<T, B, R> builder =
288-
ImmutableAttribute.builder(itemClass, builderClass, attributeType);
299+
ImmutableAttribute.builder(itemType, builderType, attributeType);
289300
immutableAttribute.accept(builder);
290301
return addAttribute(builder.build());
291302
}

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/mapper/StaticTableSchema.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ private StaticTableSchema(Builder<T> builder) {
7575
* @return A newly initialized builder
7676
*/
7777
public static <T> Builder<T> builder(Class<T> itemClass) {
78-
return new Builder<>(itemClass);
78+
return new Builder<>(EnhancedType.of(itemClass));
79+
}
80+
81+
/**
82+
* Creates a builder for a {@link StaticTableSchema} typed to specific data item class.
83+
* @param itemType The {@link EnhancedType} of the data item class object that the {@link StaticTableSchema} is to map to.
84+
* @return A newly initialized builder
85+
*/
86+
public static <T> Builder<T> builder(EnhancedType<T> itemType) {
87+
return new Builder<>(itemType);
7988
}
8089

8190
/**
@@ -85,11 +94,11 @@ public static <T> Builder<T> builder(Class<T> itemClass) {
8594
@NotThreadSafe
8695
public static final class Builder<T> {
8796
private final StaticImmutableTableSchema.Builder<T, T> delegateBuilder;
88-
private final Class<T> itemClass;
97+
private final EnhancedType<T> itemType;
8998

90-
private Builder(Class<T> itemClass) {
91-
this.delegateBuilder = StaticImmutableTableSchema.builder(itemClass, itemClass);
92-
this.itemClass = itemClass;
99+
private Builder(EnhancedType<T> itemType) {
100+
this.delegateBuilder = StaticImmutableTableSchema.builder(itemType, itemType);
101+
this.itemType = itemType;
93102
}
94103

95104
/**
@@ -130,7 +139,7 @@ public Builder<T> attributes(Collection<StaticAttribute<T, ?>> staticAttributes)
130139
*/
131140
public <R> Builder<T> addAttribute(EnhancedType<R> attributeType,
132141
Consumer<StaticAttribute.Builder<T, R>> staticAttribute) {
133-
StaticAttribute.Builder<T, R> builder = StaticAttribute.builder(itemClass, attributeType);
142+
StaticAttribute.Builder<T, R> builder = StaticAttribute.builder(itemType, attributeType);
134143
staticAttribute.accept(builder);
135144
this.delegateBuilder.addAttribute(builder.build().toImmutableAttribute());
136145
return this;
@@ -142,7 +151,7 @@ public <R> Builder<T> addAttribute(EnhancedType<R> attributeType,
142151
*/
143152
public <R> Builder<T> addAttribute(Class<R> attributeClass,
144153
Consumer<StaticAttribute.Builder<T, R>> staticAttribute) {
145-
StaticAttribute.Builder<T, R> builder = StaticAttribute.builder(itemClass, attributeClass);
154+
StaticAttribute.Builder<T, R> builder = StaticAttribute.builder(itemType, EnhancedType.of(attributeClass));
146155
staticAttribute.accept(builder);
147156
this.delegateBuilder.addAttribute(builder.build().toImmutableAttribute());
148157
return this;

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/TableSchemaTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem;
2424
import software.amazon.awssdk.enhanced.dynamodb.mapper.BeanTableSchema;
2525
import software.amazon.awssdk.enhanced.dynamodb.mapper.ImmutableTableSchema;
26+
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema;
2627
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema;
2728
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.InvalidBean;
2829
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.SimpleBean;
@@ -33,11 +34,31 @@ public class TableSchemaTest {
3334
public ExpectedException exception = ExpectedException.none();
3435

3536
@Test
36-
public void builder_constructsStaticTableSchemaBuilder() {
37+
public void builder_constructsStaticTableSchemaBuilder_fromClass() {
3738
StaticTableSchema.Builder<FakeItem> builder = TableSchema.builder(FakeItem.class);
3839
assertThat(builder).isNotNull();
3940
}
4041

42+
@Test
43+
public void builder_constructsStaticTableSchemaBuilder_fromEnhancedType() {
44+
StaticTableSchema.Builder<FakeItem> builder = TableSchema.builder(EnhancedType.of(FakeItem.class));
45+
assertThat(builder).isNotNull();
46+
}
47+
48+
@Test
49+
public void builder_constructsStaticImmutableTableSchemaBuilder_fromClass() {
50+
StaticImmutableTableSchema.Builder<SimpleImmutable, SimpleImmutable.Builder> builder =
51+
TableSchema.builder(SimpleImmutable.class, SimpleImmutable.Builder.class);
52+
assertThat(builder).isNotNull();
53+
}
54+
55+
@Test
56+
public void builder_constructsStaticImmutableTableSchemaBuilder_fromEnhancedType() {
57+
StaticImmutableTableSchema.Builder<SimpleImmutable, SimpleImmutable.Builder> builder =
58+
TableSchema.builder(EnhancedType.of(SimpleImmutable.class), EnhancedType.of(SimpleImmutable.Builder.class));
59+
assertThat(builder).isNotNull();
60+
}
61+
4162
@Test
4263
public void fromBean_constructsBeanTableSchema() {
4364
BeanTableSchema<SimpleBean> beanBeanTableSchema = TableSchema.fromBean(SimpleBean.class);

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/mapper/StaticImmutableTableSchemaTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem;
6060
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemComposedClass;
6161
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort;
62+
import software.amazon.awssdk.enhanced.dynamodb.mapper.testimmutables.EntityEnvelopeImmutable;
6263
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
6364

6465
@RunWith(MockitoJUnitRunner.class)
@@ -801,6 +802,17 @@ public void itemType_returnsCorrectClass() {
801802
assertThat(FakeItem.getTableSchema().itemType(), is(equalTo(EnhancedType.of(FakeItem.class))));
802803
}
803804

805+
@Test
806+
public void itemType_returnsCorrectClassWhenBuiltWithEnhancedType() {
807+
StaticImmutableTableSchema<FakeMappedItem, FakeMappedItem.Builder> tableSchema =
808+
StaticImmutableTableSchema.builder(EnhancedType.of(FakeMappedItem.class),
809+
EnhancedType.of(FakeMappedItem.Builder.class))
810+
.newItemBuilder(FakeMappedItem::builder, FakeMappedItem.Builder::build)
811+
.build();
812+
813+
assertThat(tableSchema.itemType(), is(equalTo(EnhancedType.of(FakeMappedItem.class))));
814+
}
815+
804816
@Test
805817
public void getTableMetadata_hasCorrectFields() {
806818
TableMetadata tableMetadata = FakeItemWithSort.getTableSchema().tableMetadata();
@@ -1538,6 +1550,27 @@ public void noConverterProvider_handlesCorrectly_whenAttributeConvertersAreSuppl
15381550
assertThat(resultMap.get("aString").s(), is(expectedString));
15391551
}
15401552

1553+
@Test
1554+
public void builder_canBuildForGenericClassType() {
1555+
StaticImmutableTableSchema<EntityEnvelopeImmutable<String>, EntityEnvelopeImmutable.Builder<String>> envelopeTableSchema =
1556+
StaticImmutableTableSchema.builder(new EnhancedType<EntityEnvelopeImmutable<String>>() {},
1557+
new EnhancedType<EntityEnvelopeImmutable.Builder<String>>() {})
1558+
.newItemBuilder(EntityEnvelopeImmutable.Builder::new, EntityEnvelopeImmutable.Builder::build)
1559+
.addAttribute(String.class,
1560+
a -> a.name("entity")
1561+
.getter(EntityEnvelopeImmutable::entity)
1562+
.setter(EntityEnvelopeImmutable.Builder::setEntity))
1563+
.build();
1564+
1565+
EntityEnvelopeImmutable<String> testEnvelope = new EntityEnvelopeImmutable<>("test-value");
1566+
1567+
Map<String, AttributeValue> expectedMap =
1568+
Collections.singletonMap("entity", AttributeValue.fromS("test-value"));
1569+
1570+
assertThat(envelopeTableSchema.itemToMap(testEnvelope, false), equalTo(expectedMap));
1571+
assertThat(envelopeTableSchema.mapToItem(expectedMap).entity(), equalTo("test-value"));
1572+
}
1573+
15411574
private <R> void verifyAttribute(EnhancedType<R> attributeType,
15421575
Consumer<StaticAttribute.Builder<FakeMappedItem, R>> staticAttribute,
15431576
FakeMappedItem fakeMappedItem,

services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/mapper/StaticTableSchemaTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem;
5858
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemComposedClass;
5959
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort;
60+
import software.amazon.awssdk.enhanced.dynamodb.mapper.testbeans.EntityEnvelopeBean;
6061
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
6162

6263
@RunWith(MockitoJUnitRunner.class)
@@ -799,6 +800,16 @@ public void itemType_returnsCorrectClass() {
799800
assertThat(FakeItem.getTableSchema().itemType(), is(equalTo(EnhancedType.of(FakeItem.class))));
800801
}
801802

803+
@Test
804+
public void itemType_returnsCorrectClassWhenBuiltWithEnhancedType() {
805+
StaticTableSchema<FakeMappedItem> tableSchema = StaticTableSchema.builder(EnhancedType.of(FakeMappedItem.class))
806+
.newItemSupplier(FakeMappedItem::new)
807+
.attributes(ATTRIBUTES)
808+
.build();
809+
810+
assertThat(tableSchema.itemType(), is(equalTo(EnhancedType.of(FakeMappedItem.class))));
811+
}
812+
802813
@Test
803814
public void getTableMetadata_hasCorrectFields() {
804815
TableMetadata tableMetadata = FakeItemWithSort.getTableSchema().tableMetadata();
@@ -1485,6 +1496,27 @@ public void noConverterProvider_handlesCorrectly_whenAttributeConvertersAreSuppl
14851496
assertThat(resultMap.get("aString").s(), is(expectedString));
14861497
}
14871498

1499+
@Test
1500+
public void builder_canBuildForGenericClassType() {
1501+
StaticTableSchema<EntityEnvelopeBean<String>> envelopeTableSchema =
1502+
StaticTableSchema.builder(new EnhancedType<EntityEnvelopeBean<String>>() {})
1503+
.newItemSupplier(EntityEnvelopeBean::new)
1504+
.addAttribute(String.class,
1505+
a -> a.name("entity")
1506+
.getter(EntityEnvelopeBean::getEntity)
1507+
.setter(EntityEnvelopeBean::setEntity))
1508+
.build();
1509+
1510+
EntityEnvelopeBean<String> testEnvelope = new EntityEnvelopeBean<>();
1511+
testEnvelope.setEntity("test-value");
1512+
1513+
Map<String, AttributeValue> expectedMap =
1514+
Collections.singletonMap("entity", AttributeValue.fromS("test-value"));
1515+
1516+
assertThat(envelopeTableSchema.itemToMap(testEnvelope, false), equalTo(expectedMap));
1517+
assertThat(envelopeTableSchema.mapToItem(expectedMap).getEntity(), equalTo("test-value"));
1518+
}
1519+
14881520
private <R> void verifyAttribute(EnhancedType<R> attributeType,
14891521
Consumer<StaticAttribute.Builder<FakeMappedItem, R>> staticAttribute,
14901522
FakeMappedItem fakeMappedItem,

0 commit comments

Comments
 (0)