[][src]Crate serde_dynamodb

DynamoDB

In its low level API, DynamoDB works with JSON objects with extra levels to set the type of the values.

{
    "Item": {
        "Age": {"N": "8"},
        "Colors": {
            "L": [
                {"S": "White"},
                {"S": "Brown"},
                {"S": "Black"}
            ]
        },
        "Name": {"S": "Fido"},
        "Vaccinations": {
            "M": {
                "Rabies": {
                    "L": [
                        {"S": "2009-03-17"},
                        {"S": "2011-09-21"},
                        {"S": "2014-07-08"}
                    ]
                },
                "Distemper": {"S": "2015-10-13"}
            }
        },
        "Breed": {"S": "Beagle"},
        "AnimalType": {"S": "Dog"}
    }
}

The allowed type keys are described here.

Rusoto DynamoDB

Rusoto DynamoDB map those values to AttributeValue, and functions to get/set/... from DynamoDB use HashMap<String, AttributeValue> as a way to represent the data.

Parsing HashMap as strongly typed data structures

Serde provides a powerful way of mapping HashMap data into Rust data structures largely automatically by using serde_dynamodb::from_hashmap

This example is not tested
extern crate serde;
extern crate serde_dynamodb;

extern crate rusoto_core;
extern crate rusoto_dynamodb;

#[macro_use]
extern crate serde_derive;

use std::collections::HashMap;

use rusoto_core::Region;
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, QueryInput, AttributeValue};

use serde_dynamodb::Error;

#[derive(Serialize, Deserialize, Debug)]
struct Person {
    surname: String,
    age: u8,
    phones: Vec<String>,
}

fn typed_example() -> Result<(), Error> {

    let client = DynamoDbClient::simple(Region::UsEast1);

    let mut query = HashMap::new();
    query.insert(String::from(":surname"), AttributeValue {
        s: Some(String::from("Smith")),
        ..Default::default()
    });
    // get data from DynamoDB
    let persons: Vec<Person> = client
        .query(&QueryInput {
            table_name: String::from("person"),
            key_condition_expression: Some(String::from("surname = :surname")),
            expression_attribute_values: Some(query),
            ..Default::default()
        })
        .sync()
        .unwrap()
        .items
        .unwrap_or_else(|| vec![])
        .into_iter()
        .map(|item| serde_dynamodb::from_hashmap(item).unwrap())
        .collect();


    // Do things just like with any other Rust data structure.
    for p in persons {
        println!("Please call {} at the number {}", p.surname, p.phones[0]);
    }

    Ok(())
}

Creating an HashMap by serializing data structures

A data structure can be converted to an HashMap by serde_dynamodb::to_hashmap.

extern crate serde;
extern crate serde_dynamodb;

extern crate rusoto_core;
extern crate rusoto_dynamodb;

#[macro_use]
extern crate serde_derive;

use serde_dynamodb::Error;

#[derive(Serialize, Deserialize, Debug)]
struct Address {
    street: String,
    city: String,
}

fn print_an_address() -> Result<(), Error> {
    // Some data structure.
    let address = Address {
        street: "10 Downing Street".to_owned(),
        city: "London".to_owned(),
    };

    // Serialize it to an HashMap.
    let j = serde_dynamodb::to_hashmap(&address)?;

    // Print, write to a file, or send to an HTTP server.
    println!("{:?}", j);

    Ok(())
}

Re-exports

pub use error::Error;

Modules

error

This type represents all possible errors that can occur when serializing or deserializing DynamoDB data.

Traits

ToQueryInput

A data structure that can be used as a DynamoDB QueryInput

Functions

from_hashmap

Deserialize an instance of type T from an HashMap<String, AttributeValue>.

to_hashmap

Serialize the given data structure as an HashMap<String, AttributeValue>.