Skip to content

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider;
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType;
import software.amazon.awssdk.enhanced.dynamodb.KeyAttributeMetadata;

/**
* Interface representing Document API for DynamoDB. Document API operations are used to carry open content i.e. data with no
Expand Down Expand Up @@ -145,8 +144,6 @@ static Builder builder() {
* @param attributeName Name of the attribute.
* @return the value of the specified attribute in the current document as SdkBytes; or null if the attribute either
* doesn't exist or the attribute value is null.
* @throws UnsupportedOperationException If the attribute value involves a byte buffer which is not backed by an accessible
* array
*/
SdkBytes getSdkBytes(String attributeName);

Expand Down Expand Up @@ -189,6 +186,15 @@ static Builder builder() {

<T> List<T> getList(String attributeName, EnhancedType<T> type);


/**
* Gets the List of values for the given attribute in the current document.
* @param attributeName Name of the attribute.
* @return value of the specified attribute in the current document as a list; or null if the
* attribute either doesn't exist or the attribute value is null.
*/
List<?> getList(String attributeName);

/**
* Gets the Map with Key as String and values as type T for the given attribute in the current document.
* <p>Note that any numeric type of map is always canonicalized into {@link SdkNumber}, and therefore if <code>T</code>
Expand Down Expand Up @@ -261,13 +267,16 @@ <T extends Number> Map<String, T> getMapOfNumbers(String attributeName,
* @return value of the specified attribute in the current document as a JSON string with pretty indentation; or null if the
* attribute either doesn't exist or the attribute value is null.
*/
String getJSONPretty(String attributeName);
String getJsonPretty(String attributeName);

/**
* Gets the {@link Boolean} value for the specified attribute.
*
* @param attributeName Name of the attribute.
* @return value of the specified attribute in the current document as a non-null Boolean.
* @throws RuntimeException
* if either the attribute doesn't exist or if the attribute
* value cannot be converted into a boolean value.
*/
Boolean getBoolean(String attributeName);

Expand Down Expand Up @@ -440,13 +449,12 @@ interface Builder {
Builder addJson(String attributeName, String json);

/**
* Convenience builder methods that sets an attribute of this document for the specified key attribute name and value.
*
* @param keyAttrName Name of the attribute that needs to be added in the Document.
* @param keyAttrValue The value that needs to be set.
* Appends an attribute of name attributeName with specified value of the given EnhancedDocument.
* @param attributeName Name of the attribute that needs to be added in the Document.
* @param enhancedDocument that needs to be added as a value to a key attribute.
* @return Builder instance to construct a {@link EnhancedDocument}
*/
Builder keyComponent(KeyAttributeMetadata keyAttrName, Object keyAttrValue);
Builder addEnhancedDocument(String attributeName, EnhancedDocument enhancedDocument);

/**
* Appends collection of attributeConverterProvider to the document builder. These
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider;
Expand Down Expand Up @@ -67,4 +68,22 @@ public <T> AttributeConverter<T> converterFor(EnhancedType<T> enhancedType) {
.map(p -> p.converterFor(enhancedType))
.findFirst().orElse(null);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ChainConverterProvider that = (ChainConverterProvider) o;
return Objects.equals(providerChain, that.providerChain);
}

@Override
public int hashCode() {
return providerChain != null ? providerChain.hashCode() : 0;
}

}
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()));
}
}
}
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.");
}
}
Loading