Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of ItemUtils.toAttributeValue in DynamoDB JDK 2.X?

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.

like image 675
goldfrapp04 Avatar asked Jul 30 '19 21:07

goldfrapp04


1 Answers

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);
  }
like image 122
goldfrapp04 Avatar answered Oct 24 '22 11:10

goldfrapp04