Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -46,10 +46,11 @@ public Document visitString(String string) {
return Document.fromString(string);
}

// TODO : A separate PR will be raised on Master and these changes will be merged separately for Master branch.
@Override
public Document visitArray(List<JsonNode> array) {
return Document.fromList(array.stream()
.map(node -> node.visit(this))
.map(node -> node == null ? Document.fromNull() : node.visit(this))
.collect(Collectors.toList()));
}

Expand Down
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 give EnhancedDocument.
Comment thread
cenedhryn marked this conversation as resolved.
Outdated
* @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
@@ -0,0 +1,140 @@
/*
* 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> {
Comment thread
cenedhryn marked this conversation as resolved.
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) {
JsonNodeToAttributeValueMapConvertor jsonNodeToAttributeValueMapConvertor = new JsonNodeToAttributeValueMapConvertor();
Comment thread
cenedhryn marked this conversation as resolved.
Outdated
return input.visit(jsonNodeToAttributeValueMapConvertor);
}

@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) {
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) {
return new StringJsonNode(value);
}

@Override
public JsonNode convertNumber(String value) {
return new NumberJsonNode(value);
}

@Override
public JsonNode convertBytes(SdkBytes value) {
return new StringJsonNode(value.asUtf8String());
}

@Override
public JsonNode convertBoolean(Boolean value) {
return new BooleanJsonNode(value);
}

@Override
public JsonNode convertSetOfStrings(List<String> value) {
return new ArrayJsonNode(value.stream().map(s -> new StringJsonNode(s)).collect(Collectors.toList()));
}

@Override
public JsonNode convertSetOfNumbers(List<String> value) {
return new ArrayJsonNode(value.stream().map(s -> new NumberJsonNode(s)).collect(Collectors.toList()));
}

@Override
public JsonNode convertSetOfBytes(List<SdkBytes> value) {
return new ArrayJsonNode(value.stream().map(sdkByte ->
new StringJsonNode(sdkByte.asUtf8String())
).collect(Collectors.toList()));
}

@Override
public JsonNode convertListOfAttributeValues(List<AttributeValue> value) {
return new ArrayJsonNode(value.stream().map(
attributeValue -> EnhancedAttributeValue.fromAttributeValue(
attributeValue).convert(VISITOR)).collect(Collectors.toList()));

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 JsonNodeToAttributeValueMapConvertor implements JsonNodeVisitor<AttributeValue> {
Comment thread
cenedhryn marked this conversation as resolved.
Outdated
@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