deez/
deez.rs

1use aws_sdk_dynamodb::types::AttributeValue;
2
3#[derive(Debug)]
4pub struct IndexKeys {
5    pub hash: IndexKey,
6    pub range: IndexKey,
7}
8
9#[derive(Debug, Default)]
10pub struct IndexKey {
11    pub field: String,
12    pub composite: String,
13}
14
15impl IndexKey {
16    pub fn field(&self) -> String {
17        self.field.clone()
18    }
19    pub fn av(&self) -> AttributeValue {
20        AttributeValue::S(self.composite.clone())
21    }
22}
23
24#[derive(Eq, Hash, PartialEq, Debug)]
25pub enum Key {
26    Hash,
27    Range,
28}
29
30#[cfg(test)]
31mod tests {
32    use crate::mocks::mocks::*;
33    use aws_sdk_dynamodb::types::AttributeValue;
34    use std::collections::HashMap;
35
36    #[test]
37    fn all_types() {
38        let a = Buss {
39            string_opt: Some("yeah".to_string()),
40            ..Default::default()
41        };
42
43        let b: &HashMap<String, AttributeValue> = &a.into();
44        println!("{:#?}", b);
45
46        let c: Buss = b.into();
47        println!("{:#?}", c);
48
49        // todo: assert
50    }
51
52    #[test]
53    fn index_names() {
54        assert_eq!(Task::gsi1_name(), "task_gsi1");
55        assert_eq!(Task::gsi2_name(), "task_gsi2");
56    }
57
58    #[test]
59    fn to_from() {
60        {
61            let a = Task {
62                task_id: Some("1a2b3c4d".to_string()),
63                project: Some("foo_project".to_string()),
64                employee: Some("e42069".to_string()),
65                description: "nothin' but chillin' 20's".to_string(),
66                some_metadata: "baz".to_string(),
67            };
68
69            let b: &HashMap<String, AttributeValue> = &a.into();
70            println!("{:#?}", b);
71
72            assert_eq!(
73                b["pk"],
74                AttributeValue::S("$TaskService#Task#task_id_1a2b3c4d".to_string())
75            );
76            assert_eq!(
77                b["sk"],
78                AttributeValue::S("$Task#employee_e42069#project_foo_project".to_string())
79            );
80            assert_eq!(
81                b["gsi1pk"],
82                AttributeValue::S("$TaskService#Task#project_foo_project".to_string())
83            );
84            assert_eq!(
85                b["gsi1sk"],
86                AttributeValue::S("$Task#employee_e42069#task_id_1a2b3c4d".to_string())
87            );
88            assert_eq!(
89                b["gsi2pk"],
90                AttributeValue::S("$TaskService#Task#employee_e42069".to_string())
91            );
92            assert_eq!(
93                b["gsi2sk"],
94                AttributeValue::S("$Task#project_foo_project#task_id_1a2b3c4d".to_string())
95            );
96
97            let c: Task = b.into();
98            println!("{:#?}", c);
99
100            assert_eq!(c.task_id, Some("1a2b3c4d".to_string()));
101            assert_eq!(c.project, Some("foo_project".to_string()));
102            assert_eq!(c.employee, Some("e42069".to_string()));
103            assert_eq!(c.description, "nothin' but chillin' 20's".to_string());
104            assert_eq!(c.some_metadata, "it's true".to_string());
105        }
106    }
107
108    #[test]
109    fn partial_keys() {
110        let mut task = Task {
111            task_id: Some("1a2b3c4d".to_string()),
112            project: None,
113            employee: None,
114            description: "nothin' but chillin' 20's".to_string(),
115            some_metadata: "baz".to_string(),
116            ..Default::default()
117        };
118
119        {
120            let map: HashMap<String, AttributeValue> = task.clone().into();
121            // println!("{:#?}", map);
122            assert_eq!(map["sk"], AttributeValue::S("$Task".to_string()));
123        }
124
125        {
126            task.employee = Some("e42069".to_string());
127            let map: HashMap<String, AttributeValue> = task.clone().into();
128            assert_eq!(
129                map["sk"],
130                AttributeValue::S("$Task#employee_e42069".to_string())
131            );
132        }
133    }
134}