This is a Rust macro for defining DynamoDB items for use with rusoto_dynamodb
. It also requires that the hashmap!
macro from the maplit
crate be in scope.
Last active
August 28, 2017 15:34
-
-
Save whmountains/eac78a84b61ad8033729731b329dfc1f to your computer and use it in GitHub Desktop.
Rust macro for defining DynamoDB items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate maplit; | |
use std::collections::HashMap; | |
macro_rules! ddb_item { | |
($($p:tt: $t:tt => $x:expr),*) => { | |
hashmap!{ | |
$( | |
String::from(stringify!($p)) => AttributeValue { | |
$t: Some($x), | |
..Default::default() | |
}, | |
)* | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate maplit; | |
extern crate rusoto_core; | |
extern crate rusoto_dynamodb; | |
extern crate uuid; | |
use std::default::Default; | |
use uuid::Uuid; | |
use rusoto_core::{default_tls_client, DefaultCredentialsProvider, Region}; | |
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, PutItemInput, AttributeValue}; | |
macro_rules! ddb_item { | |
($($p:tt: $t:tt => $x:expr),*) => { | |
hashmap!{ | |
$( | |
String::from(stringify!($p)) => AttributeValue { | |
$t: Some($x), | |
..Default::default() | |
}, | |
)* | |
} | |
} | |
} | |
fn main() { | |
let credentials = DefaultCredentialsProvider::new().unwrap(); | |
let client = DynamoDbClient::new(default_tls_client().unwrap(), credentials, Region::SaEast1); | |
let entry_id = Uuid::new_v4(); | |
let query_params = PutItemInput { | |
table_name: String::from("TABLE NAME"), | |
item: ddb_item!{ | |
Id: s => format!("{}", entry_id), | |
Status: s => String::from("some status") | |
}, | |
..Default::default() | |
}; | |
match client.put_item(&query_params) { | |
Ok(_) => { | |
println!("Successfully created entry") | |
} | |
Err(error) => { | |
println!("Error: {:?}", error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment