Skip to content

Commit bcbe7c2

Browse files
committed
handled review comments
1 parent 50e0d55 commit bcbe7c2

File tree

3 files changed

+100
-81
lines changed

3 files changed

+100
-81
lines changed

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/internal/unmarshall/document/DocumentUnmarshaller.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public Document visitString(String string) {
4646
return Document.fromString(string);
4747
}
4848

49-
// TODO : A separate PR will be raised on Master and these changes will be merged separately for Master branch.
5049
@Override
5150
public Document visitArray(List<JsonNode> array) {
5251
return Document.fromList(array.stream()

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/document/DefaultEnhancedDocument.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
4949

5050
/**
51-
* Default implementation of {@link EnhancedDocument}. This class is used by SDK to create Enhanced Documents. The class
52-
* internally saves attributeValueMap which is saved to ddb as it is. The aattribute values are retrieved
53-
* by converting attributeValue from attributeValueMap at the time of get.
51+
* Default implementation of {@link EnhancedDocument}. This class is used by SDK to create Enhanced Documents.
52+
* Internally saves attributes in an attributeValueMap which can be written to DynamoDB without further conversion.
53+
* The attribute values are retrieve by converting attributeValue from attributeValueMap at the time of get.
5454
*/
5555
@Immutable
5656
@SdkInternalApi
@@ -60,12 +60,19 @@ public class DefaultEnhancedDocument implements EnhancedDocument {
6060

6161
private final ChainConverterProvider attributeConverterProviders;
6262

63-
public DefaultEnhancedDocument(Map<String, AttributeValue> attributeValueMap,
63+
private static final JsonItemAttributeConverter jsonConverter = JsonItemAttributeConverter.create();
64+
65+
private DefaultEnhancedDocument(Map<String, AttributeValue> attributeValueMap,
6466
ChainConverterProvider attributeConverterProviders) {
6567
this.attributeValueMap = attributeValueMap;
6668
this.attributeConverterProviders = attributeConverterProviders;
6769
}
6870

71+
public static DefaultEnhancedDocument fromAttributeValueMapAndConverters(Map<String, AttributeValue> attributeValueMap,
72+
ChainConverterProvider attributeConverterProviders) {
73+
return new DefaultEnhancedDocument(attributeValueMap, attributeConverterProviders);
74+
}
75+
6976
public DefaultEnhancedDocument(DefaultBuilder builder) {
7077
attributeValueMap = Collections.unmodifiableMap(builder.getAttributeValueMap());
7178
attributeConverterProviders = ChainConverterProvider.create(builder.attributeConverterProviders);
@@ -87,7 +94,7 @@ public Map<String, AttributeValue> getAttributeValueMap() {
8794

8895
@Override
8996
public boolean isNull(String attributeName) {
90-
return isPresent(attributeName) && attributeValueMap.get(attributeName).nul();
97+
return isPresent(attributeName) && NULL_ATTRIBUTE_VALUE.equals(attributeValueMap.get(attributeName));
9198
}
9299

93100
@Override
@@ -119,10 +126,13 @@ public String getString(String attributeName) {
119126
@Override
120127
public SdkNumber getSdkNumber(String attributeName) {
121128
AttributeValue attributeValue = attributeValueMap.get(attributeName);
122-
return attributeValue != null
123-
? SdkNumber.fromString(
124-
attributeConverterProviders.converterFor(EnhancedType.of(String.class)).transformTo(attributeValue))
125-
: null;
129+
130+
if (attributeValue == null) {
131+
return null;
132+
}
133+
String stringValue = attributeConverterProviders.converterFor(EnhancedType.of(String.class))
134+
.transformTo(attributeValue);
135+
return SdkNumber.fromString(stringValue);
126136
}
127137

128138
@Override
@@ -233,14 +243,15 @@ public Map<String, Object> getRawMap(String attributeName) {
233243

234244
@Override
235245
public EnhancedDocument getMapAsDocument(String attributeName) {
236-
237246
AttributeValue attributeValue = attributeValueMap.get(attributeName);
238247
if (attributeValue == null) {
239248
return null;
240249
}
241250
if (!attributeValue.hasM()) {
242-
throw new RuntimeException("Cannot get " + attributeName + " attribute as map since its of type "
243-
+ attributeValue.type());
251+
throw new RuntimeException("Cannot get "
252+
+ attributeName
253+
+ " attribute as map since its of type "
254+
+ attributeValue.type());
244255
}
245256
return new DefaultEnhancedDocument(attributeValue.m(), this.attributeConverterProviders);
246257
}
@@ -251,8 +262,7 @@ public String getJson(String attributeName) {
251262
if (attributeValueMap.get(attributeName) == null) {
252263
return null;
253264
}
254-
JsonItemAttributeConverter jsonItemAttributeConverter = JsonItemAttributeConverter.create();
255-
JsonNode jsonNode = jsonItemAttributeConverter.transformTo(attributeValueMap.get(attributeName));
265+
JsonNode jsonNode = jsonConverter.transformTo(attributeValueMap.get(attributeName));
256266
Document document = jsonNode.visit(new DocumentUnmarshaller());
257267
return document.toString();
258268
}
@@ -327,13 +337,13 @@ public String toJsonPretty() {
327337

328338
public static class DefaultBuilder implements EnhancedDocument.Builder {
329339

330-
331340
Map<String, AttributeValue> attributeValueMap = new LinkedHashMap<>();
332341
ChainConverterProvider converterProvider = ChainConverterProvider.create(DefaultAttributeConverterProvider.create());
333342
List<AttributeConverterProvider> attributeConverterProviders;
334343

335344
public DefaultBuilder(DefaultEnhancedDocument enhancedDocument) {
336-
attributeValueMap = enhancedDocument.attributeValueMap;
345+
attributeValueMap = attributeValueMap != null ? new LinkedHashMap<>(enhancedDocument.attributeValueMap)
346+
: new LinkedHashMap<>();
337347
attributeConverterProviders = enhancedDocument.attributeConverterProviders != null ?
338348
enhancedDocument.attributeConverterProviders.chainedProviders() : null;
339349
}
@@ -390,10 +400,7 @@ public Builder addStringSet(String attributeName, Set<String> values) {
390400

391401
@Override
392402
public Builder addNumberSet(String attributeName, Set<Number> values) {
393-
394-
395403
List<String> collect = values.stream().map(value -> value.toString()).collect(Collectors.toList());
396-
397404
attributeValueMap.put(attributeName, AttributeValue.fromNs(collect));
398405
return this;
399406
}
@@ -442,7 +449,6 @@ public Builder addEnhancedDocument(String attributeName, EnhancedDocument enhanc
442449

443450
@Override
444451
public Builder addAttributeConverterProvider(AttributeConverterProvider attributeConverterProvider) {
445-
446452
if (attributeConverterProviders == null) {
447453
attributeConverterProviders = new ArrayList<>();
448454
}
@@ -452,7 +458,6 @@ public Builder addAttributeConverterProvider(AttributeConverterProvider attribut
452458

453459
@Override
454460
public Builder attributeConverterProviders(List<AttributeConverterProvider> attributeConverterProviders) {
455-
456461
this.attributeConverterProviders = attributeConverterProviders != null ? new ArrayList<>(attributeConverterProviders)
457462
: null;
458463
return null;
@@ -461,10 +466,8 @@ public Builder attributeConverterProviders(List<AttributeConverterProvider> attr
461466
// TODO : Will add test for attributeConverterProvider in future revision on next PR.
462467
@Override
463468
public Builder attributeConverterProviders(AttributeConverterProvider... attributeConverterProvider) {
464-
465469
this.attributeConverterProviders = attributeConverterProvider != null
466470
? Arrays.asList(attributeConverterProvider)
467-
468471
: null;
469472
return this;
470473
}
@@ -476,9 +479,7 @@ public Builder json(String json) {
476479
if (jsonNode == null) {
477480
throw new IllegalArgumentException("Could not parse argument json " + json);
478481
}
479-
480-
JsonItemAttributeConverter jsonItemAttributeConverter = JsonItemAttributeConverter.create();
481-
AttributeValue attributeValue = jsonItemAttributeConverter.transformFrom(jsonNode);
482+
AttributeValue attributeValue = jsonConverter.transformFrom(jsonNode);
482483
this.attributeValueMap = attributeValue.m();
483484
return this;
484485
}

0 commit comments

Comments
 (0)