[][src]Trait dynomite::Item

pub trait Item: Into<Attributes> + FromAttributes {
    fn key(&self) -> Attributes;
}

A type which can be converted to and from a set of String keys and AttributeValues.

Examples

Below is an example of doing this manually for demonstration. You can also do this automatically using #[derive(Item)] on your structs (the recommended approach).

use std::collections::HashMap;
use dynomite::{AttributeError, Item, Attribute, FromAttributes, Attributes};
use dynomite::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))
}

Required methods

fn key(&self) -> Attributes

Returns the set of attributes which make up this item's primary key

Loading content...

Implementors

Loading content...