Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3353555
D4015.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D4015.diff
View Options
diff --git a/services/identity/src/database.rs b/services/identity/src/database.rs
--- a/services/identity/src/database.rs
+++ b/services/identity/src/database.rs
@@ -1,5 +1,7 @@
+use std::collections::HashMap;
+
use rusoto_core::Region;
-use rusoto_dynamodb::DynamoDbClient;
+use rusoto_dynamodb::{AttributeValue, DynamoDbClient};
pub struct DatabaseClient {
client: DynamoDbClient,
@@ -12,3 +14,90 @@
}
}
}
+
+type AttributeName = String;
+
+fn create_simple_primary_key(
+ partition_key: (AttributeName, String),
+) -> HashMap<AttributeName, AttributeValue> {
+ HashMap::from([(
+ partition_key.0,
+ AttributeValue {
+ s: Some(partition_key.1),
+ ..Default::default()
+ },
+ )])
+}
+
+fn create_composite_primary_key(
+ partition_key: (AttributeName, String),
+ sort_key: (AttributeName, String),
+) -> HashMap<AttributeName, AttributeValue> {
+ let mut primary_key = create_simple_primary_key(partition_key);
+ primary_key.insert(
+ sort_key.0,
+ AttributeValue {
+ s: Some(sort_key.1),
+ ..Default::default()
+ },
+ );
+ primary_key
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_create_simple_primary_key() {
+ let partition_key_name = "userID".to_string();
+ let partition_key_value = "12345".to_string();
+ let partition_key =
+ (partition_key_name.clone(), partition_key_value.clone());
+ let primary_key = create_simple_primary_key(partition_key);
+ assert_eq!(primary_key.len(), 1);
+ let attribute = primary_key.get(&partition_key_name);
+ assert!(attribute.is_some());
+ assert_eq!(
+ attribute,
+ Some(AttributeValue {
+ s: Some(partition_key_value),
+ ..Default::default()
+ })
+ .as_ref()
+ );
+ }
+
+ #[test]
+ fn test_create_composite_primary_key() {
+ let partition_key_name = "userID".to_string();
+ let partition_key_value = "12345".to_string();
+ let partition_key =
+ (partition_key_name.clone(), partition_key_value.clone());
+ let sort_key_name = "deviceID".to_string();
+ let sort_key_value = "54321".to_string();
+ let sort_key = (sort_key_name.clone(), sort_key_value.clone());
+ let primary_key = create_composite_primary_key(partition_key, sort_key);
+ assert_eq!(primary_key.len(), 2);
+ let partition_key_attribute = primary_key.get(&partition_key_name);
+ assert!(partition_key_attribute.is_some());
+ assert_eq!(
+ partition_key_attribute,
+ Some(AttributeValue {
+ s: Some(partition_key_value),
+ ..Default::default()
+ })
+ .as_ref()
+ );
+ let sort_key_attribute = primary_key.get(&sort_key_name);
+ assert!(sort_key_attribute.is_some());
+ assert_eq!(
+ sort_key_attribute,
+ Some(AttributeValue {
+ s: Some(sort_key_value),
+ ..Default::default()
+ })
+ .as_ref()
+ )
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 24, 10:17 AM (16 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2574986
Default Alt Text
D4015.diff (2 KB)
Attached To
Mode
D4015: [services][identity] helper function to create primary key for DynamoDB
Attached
Detach File
Event Timeline
Log In to Comment