Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3366876
D8790.id29833.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D8790.id29833.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
@@ -97,42 +97,144 @@
}
}
+impl TryFromAttribute for String {
+ fn try_from_attr(
+ attribute_name: impl Into<String>,
+ attribute_value: Option<AttributeValue>,
+ ) -> Result<Self, DBItemError> {
+ match attribute_value {
+ Some(AttributeValue::S(value)) => Ok(value),
+ Some(_) => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::IncorrectType,
+ )),
+ None => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::Missing,
+ )),
+ }
+ }
+}
+
+impl TryFromAttribute for bool {
+ fn try_from_attr(
+ attribute_name: impl Into<String>,
+ attribute_value: Option<AttributeValue>,
+ ) -> Result<Self, DBItemError> {
+ match attribute_value {
+ Some(AttributeValue::Bool(value)) => Ok(value),
+ Some(_) => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::IncorrectType,
+ )),
+ None => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::Missing,
+ )),
+ }
+ }
+}
+
+impl TryFromAttribute for DateTime<Utc> {
+ fn try_from_attr(
+ attribute_name: impl Into<String>,
+ attribute: Option<AttributeValue>,
+ ) -> Result<Self, DBItemError> {
+ if let Some(AttributeValue::S(datetime)) = &attribute {
+ // parse() accepts a relaxed RFC3339 string
+ datetime.parse().map_err(|e| {
+ DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute),
+ DBItemAttributeError::InvalidTimestamp(e),
+ )
+ })
+ } else {
+ Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute),
+ DBItemAttributeError::Missing,
+ ))
+ }
+ }
+}
+
+impl TryFromAttribute for HashMap<String, AttributeValue> {
+ fn try_from_attr(
+ attribute_name: impl Into<String>,
+ attribute_value: Option<AttributeValue>,
+ ) -> Result<Self, DBItemError> {
+ match attribute_value {
+ Some(AttributeValue::M(map)) => Ok(map),
+ Some(_) => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::IncorrectType,
+ )),
+ None => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::Missing,
+ )),
+ }
+ }
+}
+
+impl TryFromAttribute for Vec<u8> {
+ fn try_from_attr(
+ attribute_name: impl Into<String>,
+ attribute_value: Option<AttributeValue>,
+ ) -> Result<Self, DBItemError> {
+ match attribute_value {
+ Some(AttributeValue::B(data)) => Ok(data.into_inner()),
+ Some(_) => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::IncorrectType,
+ )),
+ None => Err(DBItemError::new(
+ attribute_name.into(),
+ Value::AttributeValue(attribute_value),
+ DBItemAttributeError::Missing,
+ )),
+ }
+ }
+}
+
+#[deprecated = "Use `String::try_from_attr()` instead"]
pub fn parse_string_attribute(
attribute_name: impl Into<String>,
attribute_value: Option<AttributeValue>,
) -> Result<String, DBItemError> {
- match attribute_value {
- Some(AttributeValue::S(value)) => Ok(value),
- Some(_) => Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::IncorrectType,
- )),
- None => Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::Missing,
- )),
- }
+ String::try_from_attr(attribute_name, attribute_value)
}
+#[deprecated = "Use `bool::try_from_attr()` instead"]
pub fn parse_bool_attribute(
attribute_name: impl Into<String>,
attribute_value: Option<AttributeValue>,
) -> Result<bool, DBItemError> {
- match attribute_value {
- Some(AttributeValue::Bool(value)) => Ok(value),
- Some(_) => Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::IncorrectType,
- )),
- None => Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::Missing,
- )),
- }
+ bool::try_from_attr(attribute_name, attribute_value)
+}
+
+#[deprecated = "Use `DateTime::<Utc>::try_from_attr()` instead"]
+pub fn parse_datetime_attribute(
+ attribute_name: impl Into<String>,
+ attribute_value: Option<AttributeValue>,
+) -> Result<DateTime<Utc>, DBItemError> {
+ DateTime::<Utc>::try_from_attr(attribute_name, attribute_value)
+}
+
+#[deprecated = "Use `HashMap::<String, AttributeValue>::try_from_attr()` instead"]
+pub fn parse_map_attribute(
+ attribute_name: impl Into<String>,
+ attribute_value: Option<AttributeValue>,
+) -> Result<HashMap<String, AttributeValue>, DBItemError> {
+ attribute_value.attr_try_into(attribute_name)
}
pub fn parse_int_attribute<T>(
@@ -159,28 +261,6 @@
}
}
-pub fn parse_datetime_attribute(
- attribute_name: impl Into<String>,
- attribute_value: Option<AttributeValue>,
-) -> Result<DateTime<Utc>, DBItemError> {
- if let Some(AttributeValue::S(datetime)) = &attribute_value {
- // parse() accepts a relaxed RFC3339 string
- datetime.parse().map_err(|e| {
- DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::InvalidTimestamp(e),
- )
- })
- } else {
- Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::Missing,
- ))
- }
-}
-
/// Parses the UTC timestamp in milliseconds from a DynamoDB numeric attribute
pub fn parse_timestamp_attribute(
attribute_name: impl Into<String>,
@@ -202,25 +282,6 @@
Ok(DateTime::from_utc(naive_datetime, Utc))
}
-pub fn parse_map_attribute(
- attribute_name: impl Into<String>,
- attribute_value: Option<AttributeValue>,
-) -> Result<HashMap<String, AttributeValue>, DBItemError> {
- match attribute_value {
- Some(AttributeValue::M(map)) => Ok(map),
- Some(_) => Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::IncorrectType,
- )),
- None => Err(DBItemError::new(
- attribute_name.into(),
- Value::AttributeValue(attribute_value),
- DBItemAttributeError::Missing,
- )),
- }
-}
-
pub fn parse_integer<T>(
attribute_name: impl Into<String>,
attribute_value: &str,
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 26, 12:53 PM (15 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2584770
Default Alt Text
D8790.id29833.diff (6 KB)
Attached To
Mode
D8790: [services-lib] Implement DDB Attr conversions, deprecate old methods
Attached
Detach File
Event Timeline
Log In to Comment