Java Code Examples for com.amazonaws.services.dynamodbv2.model.AttributeValue#setL()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.AttributeValue#setL() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttributeValueCoderTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPassForListType() throws IOException {
  AttributeValue expected = new AttributeValue();

  List<AttributeValue> listAttr = new ArrayList<>();
  listAttr.add(new AttributeValue("innerMapValue1"));
  listAttr.add(new AttributeValue().withN("8976234"));

  expected.setL(listAttr);

  AttributeValueCoder coder = AttributeValueCoder.of();
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  coder.encode(expected, output);

  ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());

  AttributeValue actual = coder.decode(in);

  Assert.assertEquals(expected, actual);
}
 
Example 2
Source File: AttributeValueCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public AttributeValue decode(InputStream inStream) throws IOException {
  AttributeValue attrValue = new AttributeValue();

  String type = StringUtf8Coder.of().decode(inStream);
  AttributeValueType attrType = AttributeValueType.valueOf(type);

  switch (attrType) {
    case s:
      attrValue.setS(StringUtf8Coder.of().decode(inStream));
      break;
    case n:
      attrValue.setN(StringUtf8Coder.of().decode(inStream));
      break;
    case bOOL:
      attrValue.setBOOL(BooleanCoder.of().decode(inStream));
      break;
    case b:
      attrValue.setB(ByteBuffer.wrap(ByteArrayCoder.of().decode(inStream)));
      break;
    case sS:
      attrValue.setSS(LIST_STRING_CODER.decode(inStream));
      break;
    case nS:
      attrValue.setNS(LIST_STRING_CODER.decode(inStream));
      break;
    case bS:
      attrValue.setBS(convertToListByteBuffer(LIST_BYTE_CODER.decode(inStream)));
      break;
    case l:
      attrValue.setL(LIST_ATTRIBUTE_CODER.decode(inStream));
      break;
    case m:
      attrValue.setM(MAP_ATTRIBUTE_CODER.decode(inStream));
      break;
    case nULLValue:
      attrValue.setNULL(BooleanCoder.of().decode(inStream));
      break;
    default:
      throw new CoderException("Unknown Type");
  }

  return attrValue;
}
 
Example 3
Source File: DynamoDbDelegate.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method that can clone an Attribute Value
 *
 * @param val the AttributeValue to copy
 * @param sourceDestinationMap used to avoid loops by keeping track of references
 * @return a copy of val
 */
public static AttributeValue clone(final AttributeValue val, final IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap) {
    if (val == null) {
        return null;
    }

    if (sourceDestinationMap.containsKey(val)) {
        return sourceDestinationMap.get(val);
    }

    final AttributeValue clonedVal = new AttributeValue();
    sourceDestinationMap.put(val, clonedVal);
    if (val.getN() != null) {
        clonedVal.setN(val.getN());
    } else if (val.getS() != null) {
        clonedVal.setS(val.getS());
    } else if (val.getB() != null) {
        clonedVal.setB(val.getB());
    } else if (val.getNS() != null) {
        clonedVal.setNS(val.getNS());
    } else if (val.getSS() != null) {
        clonedVal.setSS(val.getSS());
    } else if (val.getBS() != null) {
        clonedVal.setBS(val.getBS());
    } else if (val.getBOOL() != null) {
        clonedVal.setBOOL(val.getBOOL());
    } else if (val.getNULL() != null) {
        clonedVal.setNULL(val.getNULL());
    } else if (val.getL() != null) {
        final List<AttributeValue> list = new ArrayList<>(val.getL().size());
        for (AttributeValue listItemValue : val.getL()) {
            if (!sourceDestinationMap.containsKey(listItemValue)) {
                sourceDestinationMap.put(listItemValue, clone(listItemValue, sourceDestinationMap));
            }
            list.add(sourceDestinationMap.get(listItemValue));
        }
        clonedVal.setL(list);
    } else if (val.getM() != null) {
        final Map<String, AttributeValue> map = new HashMap<>(val.getM().size());
        for (Entry<String, AttributeValue> pair : val.getM().entrySet()) {
            if (!sourceDestinationMap.containsKey(pair.getValue())) {
                sourceDestinationMap.put(pair.getValue(), clone(pair.getValue(), sourceDestinationMap));
            }
            map.put(pair.getKey(), sourceDestinationMap.get(pair.getValue()));
        }
        clonedVal.setM(map);
    }
    return clonedVal;
}
 
Example 4
Source File: AttributeValueMarshaller.java    From aws-dynamodb-encryption-java with Apache License 2.0 4 votes vote down vote up
private static AttributeValue unmarshall(final DataInputStream in) throws IOException {
    char type = in.readChar();
    AttributeValue result = new AttributeValue();
    switch (type) {
    case '\0':
        result.setNULL(Boolean.TRUE);
        break;
    case 'b':
        result.setB(readBytes(in));
        break;
    case 'B':
        result.setBS(readBytesList(in));
        break;
    case 'n':
        result.setN(readString(in));
        break;
    case 'N':
        result.setNS(readStringList(in));
        break;
    case 's':
        result.setS(readString(in));
        break;
    case 'S':
        result.setSS(readStringList(in));
        break;
    case '?':
        final byte boolValue = in.readByte();

        if (boolValue == TRUE_FLAG) {
            result.setBOOL(Boolean.TRUE);
        } else if (boolValue == FALSE_FLAG) {
            result.setBOOL(Boolean.FALSE);
        } else {
            throw new IllegalArgumentException("Improperly formatted data");
        }
        break;
    case 'L':
        final int lCount = in.readInt();
        final List<AttributeValue> l = new ArrayList<AttributeValue>(lCount);
        for (int lIdx = 0; lIdx < lCount; lIdx++) {
            l.add(unmarshall(in));
        }
        result.setL(l);
        break;
    case 'M':
        final int mCount = in.readInt();
        final Map<String, AttributeValue> m = new HashMap<String, AttributeValue>();
        for (int mIdx = 0; mIdx < mCount; mIdx++) {
            final AttributeValue key = unmarshall(in);
            if (key.getS() == null) {
                throw new IllegalArgumentException("Improperly formatted data");
            }
            AttributeValue value = unmarshall(in);
            m.put(key.getS(), value);
        }
        result.setM(m);
        break;
    default:
        throw new IllegalArgumentException("Unsupported data encoding");
    }

    return result;
}