Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F33068596
D6855.1768440960.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D6855.1768440960.diff
View Options
diff --git a/services/comm-services-lib/src/database.rs b/services/comm-services-lib/src/database.rs
--- a/services/comm-services-lib/src/database.rs
+++ b/services/comm-services-lib/src/database.rs
@@ -1,6 +1,8 @@
use aws_sdk_dynamodb::{model::AttributeValue, Error as DynamoDBError};
use chrono::{DateTime, Utc};
+use std::collections::HashMap;
use std::fmt::{Display, Formatter};
+use std::num::ParseIntError;
#[derive(
Debug, derive_more::Display, derive_more::From, derive_more::Error,
@@ -12,10 +14,16 @@
Attribute(DBItemError),
}
+#[derive(Debug)]
+pub enum Value {
+ AttributeValue(Option<AttributeValue>),
+ String(String),
+}
+
#[derive(Debug, derive_more::Error, derive_more::Constructor)]
pub struct DBItemError {
attribute_name: &'static str,
- attribute_value: Option<AttributeValue>,
+ attribute_value: Value,
attribute_error: DBItemAttributeError,
}
@@ -47,6 +55,8 @@
IncorrectType,
#[display(...)]
InvalidTimestamp(chrono::ParseError),
+ #[display(...)]
+ InvalidNumberFormat(ParseIntError),
}
pub fn parse_string_attribute(
@@ -57,12 +67,12 @@
Some(AttributeValue::S(value)) => Ok(value),
Some(_) => Err(DBItemError::new(
attribute_name,
- attribute_value,
+ Value::AttributeValue(attribute_value),
DBItemAttributeError::IncorrectType,
)),
None => Err(DBItemError::new(
attribute_name,
- attribute_value,
+ Value::AttributeValue(attribute_value),
DBItemAttributeError::Missing,
)),
}
@@ -76,12 +86,12 @@
Some(AttributeValue::Bool(value)) => Ok(value),
Some(_) => Err(DBItemError::new(
attribute_name,
- attribute_value,
+ Value::AttributeValue(attribute_value),
DBItemAttributeError::IncorrectType,
)),
None => Err(DBItemError::new(
attribute_name,
- attribute_value,
+ Value::AttributeValue(attribute_value),
DBItemAttributeError::Missing,
)),
}
@@ -96,15 +106,47 @@
datetime.parse().map_err(|e| {
DBItemError::new(
attribute_name,
- attribute_value,
+ Value::AttributeValue(attribute_value),
DBItemAttributeError::InvalidTimestamp(e),
)
})
} else {
Err(DBItemError::new(
attribute_name,
- attribute_value,
+ Value::AttributeValue(attribute_value),
DBItemAttributeError::Missing,
))
}
}
+
+pub fn parse_map_attribute(
+ attribute_name: &'static str,
+ attribute_value: Option<AttributeValue>,
+) -> Result<HashMap<String, AttributeValue>, DBItemError> {
+ match attribute_value {
+ Some(AttributeValue::M(map)) => Ok(map),
+ Some(_) => Err(DBItemError::new(
+ attribute_name,
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::IncorrectType,
+ )),
+ None => Err(DBItemError::new(
+ attribute_name,
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::Missing,
+ )),
+ }
+}
+
+pub fn parse_number(
+ attribute_name: &'static str,
+ attribute_value: &str,
+) -> Result<i32, DBItemError> {
+ attribute_value.parse::<i32>().map_err(|e| {
+ DBItemError::new(
+ attribute_name,
+ Value::String(attribute_value.to_string()),
+ DBItemAttributeError::InvalidNumberFormat(e),
+ )
+ })
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Jan 15, 1:36 AM (8 h, 57 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5935150
Default Alt Text
D6855.1768440960.diff (3 KB)
Attached To
Mode
D6855: [services][feature-flags] Introduce map and i32 parsing
Attached
Detach File
Event Timeline
Log In to Comment