-
Notifications
You must be signed in to change notification settings - Fork 912
DefaultEnhancedDocument implementation #3718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joviegas
merged 6 commits into
feature/master/joviegas_document_db_impl
from
feature/master/joviegas_document_db_impl_2
Feb 1, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5ae056
DefaultEnhancedDocument implementation
joviegas 50e0d55
Updated Null check in the conveter itself while iterating Arrays of A…
joviegas bcbe7c2
handled review comments
joviegas 816c5b6
Update test cases for JsonAttributeCOnverter
joviegas 956ccad
Removed ctor and added a builder
joviegas 999bfcc
Removed ctor for toBuilder
joviegas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...zon/awssdk/enhanced/dynamodb/internal/converter/attribute/JsonItemAttributeConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import software.amazon.awssdk.annotations.Immutable; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.annotations.ThreadSafe; | ||
import software.amazon.awssdk.core.SdkBytes; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; | ||
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.TypeConvertingVisitor; | ||
import software.amazon.awssdk.protocols.jsoncore.JsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.internal.ArrayJsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.internal.BooleanJsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.internal.NullJsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.internal.NumberJsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.internal.ObjectJsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.internal.StringJsonNode; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
||
/** | ||
* An Internal converter between JsonNode and {@link AttributeValue}. | ||
* | ||
* <p> | ||
* This converts the Attribute Value read from the DDB to JsonNode. | ||
*/ | ||
@SdkInternalApi | ||
@ThreadSafe | ||
@Immutable | ||
public final class JsonItemAttributeConverter implements AttributeConverter<JsonNode> { | ||
private static final Visitor VISITOR = new Visitor(); | ||
|
||
private JsonItemAttributeConverter() { | ||
} | ||
|
||
public static JsonItemAttributeConverter create() { | ||
return new JsonItemAttributeConverter(); | ||
} | ||
|
||
@Override | ||
public EnhancedType<JsonNode> type() { | ||
return EnhancedType.of(JsonNode.class); | ||
} | ||
|
||
@Override | ||
public AttributeValueType attributeValueType() { | ||
return AttributeValueType.M; | ||
} | ||
|
||
@Override | ||
public AttributeValue transformFrom(JsonNode input) { | ||
JsonNodeToAttributeValueMapConverter attributeValueMapConverter = JsonNodeToAttributeValueMapConverter.instance(); | ||
return input.visit(attributeValueMapConverter); | ||
} | ||
|
||
@Override | ||
public JsonNode transformTo(AttributeValue input) { | ||
return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR); | ||
} | ||
|
||
private static final class Visitor extends TypeConvertingVisitor<JsonNode> { | ||
private Visitor() { | ||
super(JsonNode.class, JsonItemAttributeConverter.class); | ||
} | ||
|
||
@Override | ||
public JsonNode convertMap(Map<String, AttributeValue> value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
Map<String, JsonNode> jsonNodeMap = new LinkedHashMap<>(); | ||
value.entrySet().forEach( | ||
k -> { | ||
JsonNode jsonNode = this.convert(EnhancedAttributeValue.fromAttributeValue(k.getValue())); | ||
jsonNodeMap.put(k.getKey(), jsonNode == null ? NullJsonNode.instance() : jsonNode); | ||
}); | ||
return new ObjectJsonNode(jsonNodeMap); | ||
} | ||
|
||
@Override | ||
public JsonNode convertString(String value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new StringJsonNode(value); | ||
} | ||
|
||
@Override | ||
public JsonNode convertNumber(String value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new NumberJsonNode(value); | ||
} | ||
|
||
@Override | ||
public JsonNode convertBytes(SdkBytes value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new StringJsonNode(value.asUtf8String()); | ||
} | ||
|
||
@Override | ||
public JsonNode convertBoolean(Boolean value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new BooleanJsonNode(value); | ||
} | ||
|
||
@Override | ||
public JsonNode convertSetOfStrings(List<String> value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new ArrayJsonNode(value.stream().map(s -> new StringJsonNode(s)).collect(Collectors.toList())); | ||
} | ||
|
||
@Override | ||
public JsonNode convertSetOfNumbers(List<String> value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new ArrayJsonNode(value.stream().map(s -> new NumberJsonNode(s)).collect(Collectors.toList())); | ||
} | ||
|
||
@Override | ||
public JsonNode convertSetOfBytes(List<SdkBytes> value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new ArrayJsonNode(value.stream().map(sdkByte -> | ||
new StringJsonNode(sdkByte.asUtf8String()) | ||
).collect(Collectors.toList())); | ||
} | ||
|
||
@Override | ||
public JsonNode convertListOfAttributeValues(List<AttributeValue> value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return new ArrayJsonNode(value.stream().map( | ||
attributeValue -> { | ||
EnhancedAttributeValue enhancedAttributeValue = EnhancedAttributeValue.fromAttributeValue(attributeValue); | ||
return enhancedAttributeValue.isNull() ? NullJsonNode.instance() : enhancedAttributeValue.convert(VISITOR); | ||
}).collect(Collectors.toList())); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
.../enhanced/dynamodb/internal/converter/attribute/JsonNodeToAttributeValueMapConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.protocols.jsoncore.JsonNode; | ||
import software.amazon.awssdk.protocols.jsoncore.JsonNodeVisitor; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
||
@SdkInternalApi | ||
public class JsonNodeToAttributeValueMapConverter implements JsonNodeVisitor<AttributeValue> { | ||
|
||
private static final JsonNodeToAttributeValueMapConverter INSTANCE = new JsonNodeToAttributeValueMapConverter(); | ||
|
||
private JsonNodeToAttributeValueMapConverter() { | ||
} | ||
|
||
public static JsonNodeToAttributeValueMapConverter instance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public AttributeValue visitNull() { | ||
return AttributeValue.builder().build(); | ||
} | ||
|
||
@Override | ||
public AttributeValue visitBoolean(boolean bool) { | ||
return AttributeValue.builder().bool(bool).build(); | ||
} | ||
|
||
@Override | ||
public AttributeValue visitNumber(String number) { | ||
return AttributeValue.builder().n(number).build(); | ||
} | ||
|
||
@Override | ||
public AttributeValue visitString(String string) { | ||
return AttributeValue.builder().s(string).build(); | ||
} | ||
|
||
@Override | ||
public AttributeValue visitArray(List<JsonNode> array) { | ||
return AttributeValue.builder().l(array.stream() | ||
.map(node -> node.visit(this)) | ||
.collect(Collectors.toList())) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public AttributeValue visitObject(Map<String, JsonNode> object) { | ||
return AttributeValue.builder().m(object.entrySet().stream() | ||
.collect(Collectors.toMap( | ||
entry -> entry.getKey(), | ||
entry -> entry.getValue().visit(this), | ||
(left, right) -> left, LinkedHashMap::new))).build(); | ||
} | ||
|
||
@Override | ||
public AttributeValue visitEmbeddedObject(Object embeddedObject) { | ||
throw new UnsupportedOperationException("Embedded objects are not supported within Document types."); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.