Search in sources :

Example 1 with Primitive

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive in project aws-sdk-android by aws-amplify.

the class Table method makeKey.

private Key makeKey(Primitive hashKey, Primitive rangeKey) {
    final Key key = new Key();
    if (this.hashKeys.size() != 1) {
        throw new IllegalStateException("no hashkeys in table");
    }
    final String hashKeyName = this.hashKeys.get(0);
    final KeyDescription keyDescription = this.keys.get(hashKeyName);
    if (keyDescription.getType() != hashKey.getType()) {
        throw new IllegalStateException("hash key type does not match the one in table definition");
    }
    final AttributeValue av = hashKey.convertToAttributeValue();
    key.put(hashKeyName, av);
    if (rangeKey == null && this.rangeKeys.size() > 0) {
        throw new IllegalStateException("range key not specificed for a table with range keys");
    } else if (rangeKey != null) {
        final String rangeKeyName = this.rangeKeys.get(0);
        final KeyDescription kd = this.keys.get(rangeKeyName);
        if (kd.getType() != rangeKey.getType()) {
            throw new IllegalStateException("range key type does not match that of table definition");
        }
        final AttributeValue rangeKeyAttributeValue = rangeKey.convertToAttributeValue();
        key.put(rangeKeyName, rangeKeyAttributeValue);
    }
    return key;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) Key(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)

Example 2 with Primitive

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive in project aws-sdk-android by aws-amplify.

the class JsonUtils method toEntry.

private static DynamoDBEntry toEntry(JsonElement data) {
    if (data == null || data.isJsonNull()) {
        return DynamoDBNull.NULL;
    }
    if (data.isJsonObject()) {
        final Document doc = new Document();
        for (final Entry<String, JsonElement> entry : data.getAsJsonObject().entrySet()) {
            final String key = entry.getKey();
            final JsonElement element = entry.getValue();
            doc.put(key, toEntry(element));
        }
        return doc;
    }
    if (data.isJsonArray()) {
        final DynamoDBList list = new DynamoDBList();
        final JsonArray array = data.getAsJsonArray();
        for (final Iterator<JsonElement> iterator = array.iterator(); iterator.hasNext(); ) {
            final JsonElement type = iterator.next();
            list.add(toEntry(type));
        }
        return list;
    }
    final JsonPrimitive primitive = data.getAsJsonPrimitive();
    if (primitive.isBoolean()) {
        return new DynamoDBBool(primitive.getAsBoolean());
    }
    if (primitive.isString()) {
        return new Primitive(primitive.getAsString());
    }
    if (primitive.isNumber()) {
        return new Primitive(primitive.getAsNumber());
    }
    throw new JsonParseException("unable to parse json for key " + data.toString());
}
Also used : JsonArray(com.google.gson.JsonArray) Primitive(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive) JsonPrimitive(com.google.gson.JsonPrimitive) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) DynamoDBBool(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBBool) Document(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document) JsonParseException(com.google.gson.JsonParseException) DynamoDBList(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBList)

Example 3 with Primitive

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive in project aws-sdk-android by aws-amplify.

the class Table method makeKey.

private Key makeKey(Document doc) {
    final Key key = new Key();
    for (final Entry<String, KeyDescription> kvp : this.keys.entrySet()) {
        final String keyName = kvp.getKey();
        final KeyDescription description = kvp.getValue();
        final DynamoDBEntry value = doc.get(keyName);
        if (value == null) {
            throw new IllegalStateException("no value for key " + keyName);
        }
        final Primitive primitive = value.asPrimitive();
        if (primitive == null) {
            throw new IllegalStateException("Key attribute " + keyName + " must be a Primitive type");
        }
        if (primitive.getType() != description.getType()) {
            throw new IllegalStateException("Key attribute " + keyName + " must be of type " + description.getType());
        }
        key.put(keyName, primitive.convertToAttributeValue());
    }
    return key;
}
Also used : DynamoDBEntry(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry) Primitive(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive) KeyDescription(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription) Key(com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)

Example 4 with Primitive

use of com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive in project aws-sdk-android by aws-amplify.

the class JsonUtils method writeJson.

private static void writeJson(DynamoDBEntry entry, JsonWriter writer) throws IOException {
    if (entry instanceof Document) {
        writer.beginObject();
        final Document doc = (Document) entry;
        for (final Entry<String, DynamoDBEntry> docEntry : doc.entrySet()) {
            final String key = docEntry.getKey();
            final DynamoDBEntry value = docEntry.getValue();
            writer.name(key);
            writeJson(value, writer);
        }
        writer.endObject();
        return;
    }
    if (entry instanceof Primitive) {
        final Primitive p = (Primitive) entry;
        writePrimitive(p.getType(), writer, p);
        return;
    }
    if (entry instanceof PrimitiveList) {
        final PrimitiveList pl = (PrimitiveList) entry;
        writer.beginArray();
        for (final DynamoDBEntry e : pl.getEntries()) {
            writePrimitive(pl.getType(), writer, e);
        }
        writer.endArray();
        return;
    }
    if (entry instanceof DynamoDBList) {
        final DynamoDBList pl = (DynamoDBList) entry;
        writer.beginArray();
        for (final DynamoDBEntry e : pl.getEntries()) {
            writeJson(e, writer);
        }
        writer.endArray();
        return;
    }
    if (entry instanceof DynamoDBBool) {
        writer.value(((DynamoDBBool) entry).asBoolean());
        return;
    }
    if (entry instanceof DynamoDBNull) {
        writer.nullValue();
        return;
    }
    throw new JsonParseException("unable to convert to json " + entry);
}
Also used : DynamoDBEntry(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry) Primitive(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive) JsonPrimitive(com.google.gson.JsonPrimitive) PrimitiveList(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.PrimitiveList) DynamoDBBool(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBBool) DynamoDBNull(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBNull) Document(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document) JsonParseException(com.google.gson.JsonParseException) DynamoDBList(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBList)

Aggregations

Primitive (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive)3 Document (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)2 DynamoDBBool (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBBool)2 DynamoDBEntry (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBEntry)2 DynamoDBList (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBList)2 Key (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.Key)2 KeyDescription (com.amazonaws.mobileconnectors.dynamodbv2.document.internal.KeyDescription)2 JsonParseException (com.google.gson.JsonParseException)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 DynamoDBNull (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.DynamoDBNull)1 PrimitiveList (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.PrimitiveList)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1