In com.amazonaws:aws-java-sdk-bundle 1.X, there is a convenient helper method ItemUtils.toAttributeValue
that converts any Object
to an AttributeValue
with the right type:
... if (value instanceof Boolean) {
return result.withBOOL((Boolean)value);
} else if (value instanceof String) {
return result.withS((String) value);
} else if ( ...
Is there an equivalent of this method in AWS JDK 2.X? Specifically, I'm working with DynamoDB and software.amazon.awssdk:dynamodb 2.X. The AttributeValue
classes in 1.X and 2.X aren't even the same, so using ItemUtils.toAttributeValue
from 1.X is not an option.
In the mean time, I just replicated the functionalities I need from 1.X:
static AttributeValue toAttributeValue(Object value) {
if (value == null) return AttributeValue.builder().nul(true).build();
if (value instanceof AttributeValue) return (AttributeValue) value;
if (value instanceof String) return AttributeValue.builder().s((String) value).build();
if (value instanceof Number) return AttributeValue.builder().n(value.toString()).build();
if (value instanceof Map) return AttributeValue.builder().m(
((Map<String, Object>) value).entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
e -> toAttributeValue(e.getValue())
))).build();
throw new UnsupportedOperationException("Time to impl new path for " + value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With