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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Dynomite provides a set of interfaces built on top of
//! [rusoto_dynamodb](https://rusoto.github.io/rusoto/rusoto_dynamodb/index.html)
//! which make working with aws Dynamodb more productive in rust.
//!
//! [Dynamodb](https://aws.amazon.com/dynamodb/) is a nosql database aws offers
//! as a managed service. It's abstractions include a table comprised of a collection
//!  of items which are a composed of a collection of named attributes which
//! can be one of a finite set of types. You can learn more about its core components
//! [here](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html)
//!
//! [Rusoto](https://github.com/rusoto/rusoto) provides an excellent set of
//! interfaces for interacting with the dynamodb API. It's representation
//! of Items is essentially a `HashMap` of `String`
//! to [AttributeValue](https://rusoto.github.io/rusoto/rusoto_dynamodb/struct.AttributeValue.html)
//! types which fits dynamodb's nosql contract well.
//! AttributeValues are able to represent multiple types of values in a
//! single container type.
//!
//! However, when programming in rust we're afforded stricter, more concise typing
//! tools than HashMaps when working with data. Dynomite is intended to make those types
//! interface more transparently with rusoto item type apis.
//!
//! Dynomite provides a set of building blocks for making interactions with
//! dynamodb feel more natural for rust's native types.
//!
//! At a low level, [Attribute](dynomite/trait.Attribute.html) type implementations
//! provide conversion interfaces to and from native rust types which represent
//! dynamodb's notion of "attributes".
//!
//! At a higher level, [Item](dynomite/trait.Item.html) type implementations
//! provide converstion interfaces for complex types which represent
//! dynamodb's notion of "items".
//!
//! You can optionally opt into having `Item` types derived for you by using
//! the [dynomite-derive](../dynomite_derive/index.html) crate,
//! which utilizes a technique you may be familiar
//! with if you've ever worked with [serde](https://github.com/serde-rs/serde).
//!
//! # Errors
//!
//! Operations that may fail typically result in an
//! [AttributeError](error/enum.AttributeError.html). These errors were
//! designed to work well with the [failure](https://crates.io/crates/failure)
//! crate ecosystem.
//!
//! # Cargo Features
//!
//! This crate has one Cargo feature, "uuid",
//! which adds support for implementing `Attribute` for
//! the [uuid](https://crates.io/crates/uuid) crate type `Uuid`, a useful
//! type for producing and representing
//! unique identifiers for items. This feature is enabled by default.
//!

#[deny(missing_docs)]
#[macro_use]
extern crate failure;
extern crate rusoto_core;
extern crate rusoto_dynamodb;
#[cfg(feature = "uuid")]
extern crate uuid;

use std::collections::{HashMap, HashSet};

#[cfg(feature = "uuid")]
use uuid::Uuid;
use rusoto_dynamodb::AttributeValue;

pub mod error;

pub use error::AttributeError;

/// type alias for map of named attribute values
pub type Attributes = HashMap<String, AttributeValue>;

/// A type which can be represented as a set of string keys and
/// `AttributeValues` and may also be coersed from the same set of values
///
/// # Examples
///
/// ```
/// extern crate rusoto_dynamodb;
/// extern crate dynomite;
///
/// use std::collections::HashMap;
/// use dynomite::{AttributeError, Item, Attribute, FromAttributes, Attributes};
/// use rusoto_dynamodb::AttributeValue;
///
/// #[derive(PartialEq,Debug, Clone)]
/// struct Person {
///   id: String
/// }
///
/// impl Item for Person {
///   fn key(&self) -> Attributes {
///     let mut attrs = HashMap::new();
///     attrs.insert("id".into(), "123".to_string().into_attr());
///     attrs
///   }
/// }
///
/// impl FromAttributes for Person {
///    fn from_attrs(
///      attrs: Attributes
///    ) -> Result<Self, AttributeError> {
///      Ok(Self {
///        id: attrs.get("id")
///          .and_then(|val| val.s.clone())
///          .ok_or(AttributeError::MissingField { name: "id".into() })?
///      })
///    }
/// }
///
/// impl Into<Attributes> for Person {
///   fn into(self: Self) -> Attributes {
///     let mut attrs = HashMap::new();
///     attrs.insert("id".into(), "123".to_string().into_attr());
///     attrs
///   }
/// }
/// fn main() {
///   let person = Person { id: "123".into() };
///   let attrs: Attributes = person.clone().into();
///   assert_eq!(Ok(person), FromAttributes::from_attrs(attrs))
/// }
/// ```
pub trait Item: Into<Attributes> + FromAttributes {
    /// Returns the set of attributes which make up this item's primary key
    fn key(&self) -> Attributes;
}

/// A type capable of being converted into an attrbute value or converted from
/// an `AttributeValue`
///
/// Implementations of this are provided for each type of `AttributeValue` field
/// which maps to a native rustlang type
///
/// # Examples
///
/// ```
/// extern crate rusoto_dynamodb;
/// extern crate dynomite;
///
/// use dynomite::Attribute;
/// use rusoto_dynamodb::AttributeValue;
///
/// fn main() {
///   assert_eq!(
///     "test".to_string().into_attr().s,
///      AttributeValue {
///        s: Some("test".to_string()),
///        ..Default::default()
///      }.s
///    );
/// }
/// ```
pub trait Attribute: Sized {
    /// Returns a conversion into an `AttributeValue`
    fn into_attr(self: Self) -> AttributeValue;
    /// Returns a fallible conversion from an `AttributeValue`
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError>;
}

/// A type capable of being produced from
/// a set of string keys and `AttributeValues`
pub trait FromAttributes: Sized {
    fn from_attrs(attrs: Attributes) -> Result<Self, AttributeError>;
}

impl<T: Item> Attribute for T {
    fn into_attr(self: Self) -> AttributeValue {
        AttributeValue {
            m: Some(self.into()),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value
            .m
            .ok_or(AttributeError::InvalidType)
            .and_then(|attrs| T::from_attrs(attrs))
    }
}

#[cfg(feature = "uuid")]
impl Attribute for Uuid {
    fn into_attr(self: Self) -> AttributeValue {
        AttributeValue {
            s: Some(self.hyphenated().to_string()),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value
            .s
            .ok_or(AttributeError::InvalidType)
            .and_then(|s| Uuid::parse_str(s.as_str()).map_err(|_| AttributeError::InvalidFormat))
    }
}

impl Attribute for String {
    fn into_attr(self: Self) -> AttributeValue {
        AttributeValue {
            s: Some(self),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value.s.ok_or(AttributeError::InvalidType)
    }
}

impl Attribute for HashSet<String> {
    fn into_attr(mut self: Self) -> AttributeValue {
        AttributeValue {
            ss: Some(self.drain().collect()),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value
            .ss
            .ok_or(AttributeError::InvalidType)
            .map(|mut value| value.drain(..).collect())
    }
}

impl Attribute for HashSet<Vec<u8>> {
    fn into_attr(mut self: Self) -> AttributeValue {
        AttributeValue {
            bs: Some(self.drain().collect()),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value
            .bs
            .ok_or(AttributeError::InvalidType)
            .map(|mut value| value.drain(..).collect())
    }
}

impl Attribute for bool {
    fn into_attr(self: Self) -> AttributeValue {
        AttributeValue {
            bool: Some(self),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value.bool.ok_or(AttributeError::InvalidType)
    }
}

impl Attribute for Vec<u8> {
    fn into_attr(self: Self) -> AttributeValue {
        AttributeValue {
            b: Some(self),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value.b.ok_or(AttributeError::InvalidType)
    }
}

impl<T: Item> Attribute for Vec<T> {
    fn into_attr(mut self: Self) -> AttributeValue {
        AttributeValue {
            l: Some(self.drain(..).map(|s| s.into_attr()).collect()),
            ..Default::default()
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        value
            .l
            .ok_or(AttributeError::InvalidType)?
            .into_iter()
            .map(Attribute::from_attr)
            .collect()
    }
}

impl<T: Attribute> Attribute for Option<T> {
    fn into_attr(self: Self) -> AttributeValue {
        match self {
            Some(value) => value.into_attr(),
            _ => Default::default(),
        }
    }
    fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
        match Attribute::from_attr(value) {
            Ok(value) => Ok(Some(value)),
            Err(AttributeError::InvalidType) => Ok(None),
            Err(err) => Err(err),
        }
    }
}

macro_rules! numeric_attr {
    ($type:ty) => {
        impl Attribute for $type {
            fn into_attr(self) -> AttributeValue {
                AttributeValue {
                    n: Some(self.to_string()),
                    ..Default::default()
                }
            }
            fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
                value.n
                    .ok_or(AttributeError::InvalidType)
                    .and_then(|num| {
                        num.parse()
                            .map_err(|_| AttributeError::InvalidFormat)
                    })
            }
        }
    };
}

macro_rules! numeric_collection_attr {
    ($type:ty => $collection:ty) => {
        impl Attribute for $collection {
            fn into_attr(self) -> AttributeValue {
                AttributeValue {
                    ns: Some(self.iter().map(|item| item.to_string()).collect()),
                    ..Default::default()
                }
            }
            fn from_attr(value: AttributeValue) -> Result<Self, AttributeError> {
                let mut nums = value.ns
                    .ok_or(AttributeError::InvalidType)?;
                let mut results: Vec<Result<$type, AttributeError>> =
                    nums.drain(..).map(|ns| ns.parse().map_err(|_| AttributeError::InvalidFormat)).collect();
                let collected = results.drain(..).collect();
                collected
            }
        }
    };
}

// implement Attribute for numeric types
numeric_attr!(u16);
numeric_attr!(u32);
numeric_attr!(i32);
numeric_attr!(i64);
numeric_attr!(f32);
numeric_attr!(f64);

// implement Attribute for numeric collections
numeric_collection_attr!(u16 => HashSet<u16>);
numeric_collection_attr!(u16 => Vec<u16>);
numeric_collection_attr!(u32 => HashSet<u32>);
numeric_collection_attr!(u32 => Vec<u32>);
numeric_collection_attr!(i32 => HashSet<i32>);
numeric_collection_attr!(i32 => Vec<i32>);
numeric_collection_attr!(i64 => HashSet<i64>);
numeric_collection_attr!(i64 => Vec<i64>);
numeric_collection_attr!(f32 => Vec<f32>);
numeric_collection_attr!(f64 => Vec<f64>);

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn uuid_attr() {
        let value = Uuid::new_v4();
        assert_eq!(Ok(value), Uuid::from_attr(value.into_attr()));
    }

    #[test]
    fn uuid_invalid_attr() {
        assert_eq!(
            Err(AttributeError::InvalidType),
            Uuid::from_attr(AttributeValue {
                bool: Some(true),
                ..Default::default()
            })
        );
    }

    #[test]
    fn option_some_attr() {
        let value = Some(1);
        assert_eq!(Ok(value), Attribute::from_attr(value.into_attr()));
    }

    #[test]
    fn option_none_attr() {
        let value: Option<u32> = Default::default();
        assert_eq!(Ok(value), Attribute::from_attr(value.into_attr()));
    }

    #[test]
    fn option_invalid_attr() {
        assert_eq!(
            Ok(None),
            Option::<u32>::from_attr(AttributeValue {
                bool: Some(true),
                ..Default::default()
            })
        );
    }

    #[test]
    fn bool_attr() {
        let value = true;
        assert_eq!(Ok(value), bool::from_attr(value.into_attr()));
    }

    #[test]
    fn string_attr() {
        let value = "test".to_string();
        assert_eq!(
            Ok(value.clone()),
            String::from_attr(value.clone().into_attr())
        );
    }

    #[test]
    fn byte_vec_attr() {
        let value = "test".as_bytes().to_vec();
        assert_eq!(
            Ok(value.clone()),
            Vec::<u8>::from_attr(value.clone().into_attr())
        );
    }
}