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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#![allow(unused)]
//! DynamoDB Data Utilities
//! 
//! API Example:
//! ```
//! use dynamodb_data::*;
//! use std::collections::HashMap;
//! 
//! let payload: HashMap<String, rusoto_dynamodb::AttributeValue> = fields!{
//!     id => ::uuid::Uuid::new_v4(),
//!     name => "user name",
//!     counter => 0
//! };
//! let get_item_query = rusoto_dynamodb::GetItemInput {
//!     key: fields!{
//!         id => ::uuid::Uuid::new_v4()
//!     },
//!     ..Default::default()
//! };
//! ```
use std::collections::HashMap;
use serde;
use serde::{Serialize, Deserialize};
use serde_json::{self, Value};
use rusoto_core;
use rusoto_core::Region;
use rusoto_dynamodb;
use rusoto_dynamodb::{
    DynamoDb,
    DynamoDbClient,
    QueryInput,
    AttributeValue,
};


///////////////////////////////////////////////////////////////////////////////
// EXTERNAL API - FUNCTIONS
///////////////////////////////////////////////////////////////////////////////

/// Converts any serializable value to a `rusoto_dynamodb::AttributeValue`.
/// ```
/// let msg: rusoto_dynamodb::AttributeValue = dynamodb_data::to_attribute_value("Hello World")
///     .expect("serde issue");
/// ```
pub fn to_attribute_value<A: Serialize>(value: A) -> Result<AttributeValue, serde_json::Error> {
    match serde_json::to_value(value) {
        Ok(x) => Ok(json_to_attribute_value(x)),
        Err(e) => Err(e),
    }
}

/// Converts any serializable value from a `rusoto_dynamodb::AttributeValue`.
/// ```
/// let msg: rusoto_dynamodb::AttributeValue = dynamodb_data::to_attribute_value("Hello World").expect("serde issue");
/// let msg: String = dynamodb_data::from_attribute_value(msg).expect("serde issue");
/// ```
pub fn from_attribute_value<A: serde::de::DeserializeOwned>(value: AttributeValue) -> Result<A, serde_json::Error> {
    let value: Value = attribute_value_to_json(value);
    serde_json::from_value(value)
}

/// Must be something that serializes to a JSON Object.
/// ```
/// use std::collections::HashMap;
/// use std::iter::FromIterator;
/// let mut object: HashMap<String, u32> = HashMap::from_iter(vec![
///     (String::from("red"), 1),
///     (String::from("green"), 2),
///     (String::from("blue"), 3)
/// ]);
/// let msg: HashMap<String, rusoto_dynamodb::AttributeValue> = dynamodb_data::to_fields(object).expect("to_fields issue");
/// ```
pub fn to_fields<A: Serialize>(value: A) -> Result<HashMap<String, AttributeValue>, serde_json::Error> {
    match serde_json::to_value(value) {
        Ok(x) => Ok(json_to_attribute_value_hashmap(x)),
        Err(e) => Err(e),
    }
}

/// Must be something that serializes from a JSON Object.
/// ```
/// use std::collections::HashMap;
/// use std::iter::FromIterator;
/// let mut object: HashMap<String, u32> = HashMap::from_iter(vec![
///     (String::from("red"), 1),
///     (String::from("green"), 2),
///     (String::from("blue"), 3)
/// ]);
/// let msg: HashMap<String, rusoto_dynamodb::AttributeValue> = dynamodb_data::to_fields(object).expect("to_fields issue");
/// let msg: HashMap<String, u32> = dynamodb_data::from_fields(msg).expect("from_fields failed");
/// ```
pub fn from_fields<A: serde::de::DeserializeOwned>(value: HashMap<String, AttributeValue>) -> Result<A, serde_json::Error> {
    let value: serde_json::Map<String, Value> = attribute_value_hashmap_to_json_map(value);
    serde_json::from_value(Value::Object(value))
}


///////////////////////////////////////////////////////////////////////////////
// EXTERNAL API - MACROS
///////////////////////////////////////////////////////////////////////////////

/// Internal
#[doc(hidden)]
#[macro_export]
macro_rules! i_field_key {
    ($k:ident) => {
        stringify!($k).to_owned()
    };
    ($k:expr) => {{
        let value: String = $k.to_owned();
        value
    }};
}

/// Converts the given fields to `HashMap<String, AttributeValue>`, automatically
/// serializing the keys to `AttributeValue` VIA `serde_json`.
/// 
/// Example 1:
/// ```
/// use dynamodb_data::*;
/// use std::collections::HashMap;
/// 
/// let payload: HashMap<String, rusoto_dynamodb::AttributeValue> = fields!{
///     id => ::uuid::Uuid::new_v4(),
///     name => "user name",
///     counter => 0
/// };
/// 
/// // Str keys are also supported
/// let payload: HashMap<String, rusoto_dynamodb::AttributeValue> = fields!{
///     "id" => ::uuid::Uuid::new_v4(),
///     // `name` is a DynamoDB reserved word.
///     ":name" => "user name",
///     "counter" => 0,
///     // Others can still be identifiers though
///     some_other_field => 0
/// };
/// ```
/// 
/// Example 2:
/// ```
/// use dynamodb_data::*;
/// let get_item_query = rusoto_dynamodb::GetItemInput {
///     key: fields!{
///         id => ::uuid::Uuid::new_v4()
///     },
///     ..Default::default()
/// };
/// ```
#[macro_export]
macro_rules! fields {
    ($($k:tt => $v:expr),* $(,)?) => {{
        use std::collections::hash_map::HashMap;
        use rusoto_dynamodb::AttributeValue;
        use $crate::*;

        let results: HashMap<String, AttributeValue> = {
            let mut m = HashMap::new();
            $(
                m.insert(
                    i_field_key!($k),
                    to_attribute_value($v).expect("object! serialization failure")
                );
            )*
            m
        };
        results
    }};
}



/// ```
/// use dynamodb_data::*;
/// rusoto_dynamodb::PutItemInput {
///     expression_attribute_names: Some(names!{
///         ":id" => "id"
///     }),
///     ..Default::default()
/// };
/// ```
#[macro_export]
macro_rules! names {
    ($($k:expr => $v:expr),* $(,)?) => {{
        use std::collections::hash_map::HashMap;

        let results: HashMap<String, String> = {
            let mut m = HashMap::new();
            $(
                m.insert($k.to_owned(), $v.to_owned());
            )*
            m
        };
        results
    }};
}



///////////////////////////////////////////////////////////////////////////////
// INTERNAL - ATTRIBUTE-VALUE
///////////////////////////////////////////////////////////////////////////////

fn json_to_attribute_value(value: Value) -> AttributeValue {
    use std::iter::FromIterator;

    match value {
        Value::Array(xs) => {
            let xs: Vec<AttributeValue> = xs
                .into_iter()
                .map(|x| json_to_attribute_value(x))
                .collect();
            AttributeValue {
                l: Some(xs),
                ..Default::default()
            }
        },
        Value::Object(xs) => {
            let xs: Vec<(String, AttributeValue)> = xs
                .into_iter()
                .map(|(k, v)| (k, json_to_attribute_value(v)))
                .collect();
            let xs: HashMap<String, AttributeValue> = HashMap::from_iter(xs);
            AttributeValue {
                m: Some(xs),
                ..Default::default()
            }
        },
        Value::String(x) => {
            if x.is_empty() {
                AttributeValue {
                    s: Some(String::from("\0")),
                    ..Default::default()
                }
            } else {
                AttributeValue {
                    s: Some(x),
                    ..Default::default()
                }
            }
        },
        Value::Number(x) => AttributeValue {
            n: Some(format!("{}", x)),
            ..Default::default()
        },
        Value::Bool(x) => AttributeValue {
            bool: Some(x),
            ..Default::default()
        },
        Value::Null => AttributeValue {
            null: Some(true),
            ..Default::default()
        },
    }
}

fn attribute_value_to_json(value: AttributeValue) -> Value {
    use std::iter::FromIterator;

    if value.b.is_some() {
        match String::from_utf8(value.b.expect("dynamodb sdk error").to_vec()) {
            Ok(x) => Value::String(x),
            _ => panic!()
        }
    }
    else if value.bool.is_some() {
        Value::Bool(value.bool.expect("dynamodb sdk error"))
    }
    else if value.bs.is_some() {
        let xs = value.bs
            .expect("dynamodb sdk error")
            .into_iter()
            .map(|x| {
                match String::from_utf8(x.to_vec()) {
                    Ok(x) => Value::String(x),
                    _ => panic!()
                }
            })
            .collect();
        Value::Array(xs)
    }
    else if value.l.is_some() {
        let xs: Vec<Value> = value.l
            .expect("dynamodb sdk error")
            .into_iter()
            .map(|x| attribute_value_to_json(x))
            .collect();
        Value::Array(xs)
    }
    else if value.m.is_some() {
        let xs: Vec<(String, Value)> = value.m
            .expect("dynamodb sdk error")
            .into_iter()
            .map(|(k, v)| (k, attribute_value_to_json(v)))
            .collect();
        let xs: serde_json::Map<String, Value> = serde_json::Map::from_iter(xs);
        Value::Object(xs)
    }
    else if value.n.is_some() {
        serde_json::from_str(value.n.expect("dynamodb sdk error").as_str()).expect("dynamodb sdk error")
    }
    else if value.ns.is_some() {
        let xs: Vec<Value> = value.ns
            .expect("dynamodb sdk error")
            .into_iter()
            .map(|x| serde_json::from_str(x.as_str()).expect("dynamodb sdk error"))
            .collect();
        Value::Array(xs)
    }
    else if value.s.is_some() {
        let text = value.s.expect("dynamodb sdk error");
        if text == String::from("\0") {
            Value::String(String::new())
        } else {
            Value::String(text)
        }
    }
    else if value.null.is_some() {
        Value::Null
    }
    else if value.ss.is_some() {
        let xs: Vec<Value> = value.ns
            .expect("dynamodb sdk error")
            .into_iter()
            .map(|x| Value::String(x))
            .collect();
        Value::Array(xs)
    } else {
        panic!()
    }
}


///////////////////////////////////////////////////////////////////////////////
// INTERNAL - ATTRIBUTE-VALUE HASH-MAP
///////////////////////////////////////////////////////////////////////////////

fn json_to_attribute_value_hashmap(value: Value) -> HashMap<String, AttributeValue> {
    json_to_attribute_value(value).m.expect("dynamodb sdk error")
}

fn attribute_value_hashmap_to_json_map(value: HashMap<String, AttributeValue>) -> serde_json::Map<String, Value> {
    use std::iter::FromIterator;
    let value: AttributeValue = AttributeValue {
        m: Some(value),
        ..Default::default()
    };
    match attribute_value_to_json(value) {
        Value::Object(xs) => {
            xs
        },
        _ => panic!()
    }
}