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
pub mod cas;
pub mod fixture;

use rusoto_dynamodb::{AttributeDefinition, AttributeValue, KeySchemaElement};

pub type TableName = String;

pub fn hash_key(attribute_name: &str) -> KeySchemaElement {
    KeySchemaElement {
        attribute_name: attribute_name.into(),
        key_type: "HASH".into(),
    }
}

pub fn string_attribute_definition(attribute_name: &str) -> AttributeDefinition {
    AttributeDefinition {
        attribute_name: attribute_name.into(),
        attribute_type: "S".into(),
    }
}

pub fn string_attribute_value(value: &str) -> AttributeValue {
    AttributeValue {
        s: Some(value.to_string()),
        ..Default::default()
    }
}

pub fn bool_attribute_value(value: bool) -> AttributeValue {
    AttributeValue {
        bool: Some(value),
        ..Default::default()
    }
}

pub fn blob_attribute_value(value: &Vec<u8>) -> AttributeValue {
    AttributeValue {
        b: Some(value.as_slice().into()),
        ..Default::default()
    }
}

pub fn number_attribute_value(value: u64) -> AttributeValue {
    AttributeValue {
        n: Some(value.to_string()),
        ..Default::default()
    }
}

pub fn string_set_attribute_value(value: Vec<String>) -> AttributeValue {
    AttributeValue {
        ss: Some(value),
        ..Default::default()
    }
}

pub fn list_attribute_value(value: Vec<AttributeValue>) -> AttributeValue {
    AttributeValue {
        l: Some(value),
        ..Default::default()
    }
}

#[cfg(test)]
pub mod test {

    use crate::dht::bbdht::dynamodb::schema::{
        fixture::attribute_name_fresh, hash_key, string_attribute_definition,
        string_attribute_value,
    };
    use rusoto_dynamodb::{AttributeDefinition, AttributeValue, KeySchemaElement};

    #[test]
    fn hash_key_test() {
        let attribute_name = attribute_name_fresh();

        let result = hash_key(&attribute_name);

        assert_eq!(
            KeySchemaElement {
                attribute_name: attribute_name.to_string(),
                key_type: String::from("HASH"),
            },
            result,
        );
    }

    #[test]
    fn string_attribute_definition_test() {
        let attribute_name = attribute_name_fresh();

        let result = string_attribute_definition(&attribute_name);

        assert_eq!(
            AttributeDefinition {
                attribute_name: attribute_name.into(),
                attribute_type: "S".into(),
            },
            result,
        );
    }

    #[test]
    fn string_attribute_value_test() {
        let value = String::from("foo");

        let result = string_attribute_value(&value);

        assert_eq!(
            AttributeValue {
                b: None,
                bool: None,
                bs: None,
                l: None,
                m: None,
                n: None,
                ns: None,
                null: None,
                s: Some(value.clone()),
                ss: None,
            },
            result,
        );
    }
}