1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
macro_rules! aws_sdk_macro {
(
feature = $feature:literal,
crate_name = $crate_name:ident,
mod_name = $mod_name:ident,
attribute_value_path = $attribute_value_path:path,
blob_path = $blob_path:path,
aws_version = $version:literal,
config_version = $config_version:literal,
) => {
#[cfg(feature = $feature)]
#[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
pub mod $mod_name {
# version ", $version)]
//!
//! Because [aws-sdk-dynamodb] has not yet reached version 1.0, a feature is required to
//! enable support. Add the following to your dependencies.
//!
//! ```toml
//! [dependencies]
#![doc = concat!("aws-config = ", stringify!($config_version))]
#![doc = concat!("aws-sdk-dynamodb = ", stringify!($version))]
#![doc = concat!("serde_dynamo = { version = \"4\", features = [", stringify!($feature), "] }")]
//! ```
//!
//!
//! ## Parsing items as strongly-typed data structures.
//!
//! Items received from a [aws-sdk-dynamodb] call can be run through [`from_items`].
//!
//! ```
#![doc = concat!("# use ", stringify!($crate_name), "::client::Client;")]
//! # use serde_derive::{Serialize, Deserialize};
//! # use serde_dynamo::from_items;
//! #
//! # async fn scan(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
//! #[derive(Serialize, Deserialize)]
//! pub struct User {
//! id: String,
//! name: String,
//! age: u8,
//! };
//!
//! // Get documents from DynamoDB
//! let result = client.scan().table_name("user").send().await?;
//!
//! // And deserialize them as strongly-typed data structures
//! let items = result.items().to_vec();
//! let users: Vec<User> = from_items(items)?;
//! println!("Got {} users", users.len());
//! # Ok(())
//! # }
//! ```
//!
//! Alternatively, to deserialize one item at a time, [`from_item`] can be used.
//!
//! ```
#![doc = concat!("# use ", stringify!($crate_name), "::client::Client;")]
//! # use serde_derive::{Serialize, Deserialize};
//! # use serde_dynamo::from_item;
//! #
//! # async fn scan(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
//! #[derive(Serialize, Deserialize)]
//! pub struct User {
//! id: String,
//! name: String,
//! age: u8,
//! };
//!
//! // Get documents from DynamoDB
//! let result = client.scan().table_name("user").send().await?;
//!
//! // And deserialize them as strongly-typed data structures
//! for item in result.items().to_vec() {
//! let user: User = from_item(item)?;
//! println!("{} is {}", user.name, user.age);
//! }
//! # Ok(())
//! # }
//! ```
//!
//!
//! ## Creating items by serializing data structures
//!
//! Writing an entire data structure to DynamoDB typically involves using [`to_item`] to serialize
//! it.
//!
//! ```
#![doc = concat!("# use ", stringify!($crate_name), "::client::Client;")]
//! # use serde_derive::{Serialize, Deserialize};
//! # use serde_dynamo::to_item;
//! #
//! # async fn put(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
//! #[derive(Serialize, Deserialize)]
//! pub struct User {
//! id: String,
//! name: String,
//! age: u8,
//! };
//!
//! // Create a user
//! let user = User {
//! id: "fSsgVtal8TpP".to_string(),
//! name: "Arthur Dent".to_string(),
//! age: 42,
//! };
//!
//! // Turn it into an item that aws-sdk-dynamodb understands
//! let item = to_item(user)?;
//!
//! // And write it!
//! client.put_item().table_name("users").set_item(Some(item)).send().await?;
//! # Ok(())
//! # }
//! ```
//!
//!
//! ## Using to_attribute_value for more control
//!
//! In some circumstances, building [aws_sdk_dynamodb::model::AttributeValue]s directly is required.
//!
//! For example, when generating a key to supply to [get_item].
//!
//! ```
//! use serde_dynamo::to_attribute_value;
#![doc = concat!("# use ", stringify!($crate_name), "::client::Client;")]
//! # use std::collections::HashMap;
//! #
//! # async fn get(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
//! #
//! # struct User { id: String };
//! # let user = User { id: "fSsgVtal8TpP".to_string() };
//!
//! // Create the unique key of the record in DynamoDB in a way rusoto understands
//! let key = HashMap::from([
//! (String::from("id"), to_attribute_value(&user.id)?),
//! ]);
//!
//! // And get the record
//! client.get_item().table_name("users").set_key(Some(key)).send().await?;
//! # Ok(())
//! # }
//! ```
//!
//! Or when generating attribute values in a [query] call.
//!
//! ```
//! use serde_dynamo::to_attribute_value;
#![doc = concat!("# use ", stringify!($crate_name), "::client::Client;")]
//! # use std::collections::HashMap;
//! #
//! # async fn query(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
//! # let user_type = "user";
//! # let yesterday = "1985-04-21";
//!
//! // Declare all of the expression inputs for a query call
//! let expression_attribute_values = HashMap::from([
//! (String::from(":user_type"), to_attribute_value(user_type)?),
//! (String::from(":last_login"), to_attribute_value(yesterday)?),
//! ]);
//!
//! client.query()
//! .table_name("users")
//! .index_name("by_type_and_last_login")
//! .key_condition_expression("user_type = :user_type AND last_login > :last_login")
//! .set_expression_attribute_values(Some(expression_attribute_values))
//! .send()
//! .await?;
//! # Ok(())
//! # }
//! ```
//! [aws-sdk-dynamodb]: https://docs.rs/aws-sdk-dynamodb
//! [get_item]: https://docs.rs/aws-sdk-dynamodb/*/aws_sdk_dynamodb/client/struct.Client.html#method.get_item
//! [put_item]: https://docs.rs/aws-sdk-dynamodb/*/aws_sdk_dynamodb/client/struct.Client.html#method.put_item
//! [query]: https://docs.rs/aws-sdk-dynamodb/*/aws_sdk_dynamodb/client/struct.Client.html#method.query
//! [aws_sdk_dynamodb::model::AttributeValue]: https://docs.rs/rusoto_dynamodb/0.47.0/rusoto_dynamodb/struct.AttributeValue.html
use crate::Result;
use $attribute_value_path;
use $blob_path;
impl From<crate::AttributeValue> for AttributeValue {
fn from(attribute_value: crate::AttributeValue) -> AttributeValue {
match attribute_value {
crate::AttributeValue::N(n) => AttributeValue::N(n),
crate::AttributeValue::S(s) => AttributeValue::S(s),
crate::AttributeValue::Bool(b) => AttributeValue::Bool(b),
crate::AttributeValue::B(v) => AttributeValue::B(Blob::new(v)),
crate::AttributeValue::Null(null) => AttributeValue::Null(null),
crate::AttributeValue::M(m) => AttributeValue::M(m.into_iter().map(|(key, attribute_value)| (key, AttributeValue::from(attribute_value))).collect()),
crate::AttributeValue::L(l) => AttributeValue::L(l.into_iter().map(AttributeValue::from).collect()),
crate::AttributeValue::Ss(ss) => AttributeValue::Ss(ss),
crate::AttributeValue::Ns(ns) => AttributeValue::Ns(ns),
crate::AttributeValue::Bs(bs) => AttributeValue::Bs(bs.into_iter().map(Blob::new).collect()),
}
}
}
impl From<AttributeValue> for crate::AttributeValue {
fn from(attribute_value: AttributeValue) -> crate::AttributeValue {
match attribute_value {
AttributeValue::N(n) => crate::AttributeValue::N(n),
AttributeValue::S(s) => crate::AttributeValue::S(s),
AttributeValue::Bool(b) => crate::AttributeValue::Bool(b),
AttributeValue::B(v) => crate::AttributeValue::B(v.into_inner()),
AttributeValue::Null(null) => crate::AttributeValue::Null(null),
AttributeValue::M(m) => crate::AttributeValue::M(m.into_iter().map(|(key, attribute_value)| (key, crate::AttributeValue::from(attribute_value))).collect()),
AttributeValue::L(l) => crate::AttributeValue::L(l.into_iter().map(crate::AttributeValue::from).collect()),
AttributeValue::Ss(ss) => crate::AttributeValue::Ss(ss),
AttributeValue::Ns(ns) => crate::AttributeValue::Ns(ns),
AttributeValue::Bs(bs) => crate::AttributeValue::Bs(bs.into_iter().map(Blob::into_inner).collect()),
_ => panic!("Unexpectedly did not match any possible data types"),
}
}
}
/// A version of [`crate::to_attribute_value`] where the `AV` generic is tied to
/// [`aws-sdk-dynamodb::model::AttributeValue`](AttributeValue).
///
/// Useful in very generic code where the type checker can't determine the type of
/// `AV`.
pub fn to_attribute_value<T>(value: T) -> Result<AttributeValue>
where
T: serde_core::ser::Serialize,
{
crate::ser::to_attribute_value(value)
}
/// A version of [`crate::to_item`] where the `AV` generic is tied to
/// [`aws-sdk-dynamodb::model::AttributeValue`](AttributeValue).
///
/// Useful in very generic code where the type checker can't determine the type of
/// `AV`.
pub fn to_item<T>(value: T) -> Result<std::collections::HashMap<String, AttributeValue>>
where
T: serde_core::ser::Serialize,
{
crate::ser::to_item(value)
}
/// A version of [`crate::from_attribute_value`] where the `AV` generic is tied to
/// [`aws-sdk-dynamodb::model::AttributeValue`](AttributeValue).
///
/// Useful in very generic code where the type checker can't determine the type of
/// `AV`.
pub fn from_attribute_value<'a, T>(attribute_value: AttributeValue) -> Result<T>
where
T: serde_core::de::Deserialize<'a>,
{
crate::de::from_attribute_value(attribute_value)
}
/// A version of [`crate::from_item`] where the `AV` generic is tied to
/// [`aws-sdk-dynamodb::model::AttributeValue`](AttributeValue).
///
/// Useful in very generic code where the type checker can't determine the type of
/// `AV`.
pub fn from_item<'a, T>(
item: std::collections::HashMap<String, AttributeValue>,
) -> Result<T>
where
T: serde_core::de::Deserialize<'a>,
{
crate::de::from_item(item)
}
/// A version of [`crate::from_items`] where the `AV` generic is tied to
/// [`aws-sdk-dynamodb::model::AttributeValue`](AttributeValue).
///
/// Useful in very generic code where the type checker can't determine the type of
/// `AV`.
pub fn from_items<'a, T>(
items: Vec<std::collections::HashMap<String, AttributeValue>>,
) -> Result<Vec<T>>
where
T: serde_core::de::Deserialize<'a>,
{
crate::de::from_items(items)
}
}
#[cfg(feature = $feature)]
#[doc(hidden)]
#[deprecated(since = "4.0.0", note = "The double-underscore is no longer necessary")]
pub mod $crate_name {
use crate::Result;
use $attribute_value_path;
#[deprecated(since = "4.0.0", note = "The double-underscore on the mod name is no longer necessary")]
pub fn to_attribute_value<T>(value: T) -> Result<AttributeValue>
where
T: serde_core::ser::Serialize,
{
crate::ser::to_attribute_value(value)
}
#[deprecated(since = "4.0.0", note = "The double-underscore on the mod name is no longer necessary")]
pub fn to_item<T>(value: T) -> Result<std::collections::HashMap<String, AttributeValue>>
where
T: serde_core::ser::Serialize,
{
crate::ser::to_item(value)
}
#[deprecated(since = "4.0.0", note = "The double-underscore on the mod name is no longer necessary")]
pub fn from_attribute_value<'a, T>(attribute_value: AttributeValue) -> Result<T>
where
T: serde_core::de::Deserialize<'a>,
{
crate::de::from_attribute_value(attribute_value)
}
#[deprecated(since = "4.0.0", note = "The double-underscore on the mod name is no longer necessary")]
pub fn from_item<'a, T>(
item: std::collections::HashMap<String, AttributeValue>,
) -> Result<T>
where
T: serde_core::de::Deserialize<'a>,
{
crate::de::from_item(item)
}
#[deprecated(since = "4.0.0", note = "The double-underscore on the mod name is no longer necessary")]
pub fn from_items<'a, T>(
items: Vec<std::collections::HashMap<String, AttributeValue>>,
) -> Result<Vec<T>>
where
T: serde_core::de::Deserialize<'a>,
{
crate::de::from_items(items)
}
}
};
}
pub(crate) use aws_sdk_macro;