-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[services-lib] Add BlobInfo utility type
Summary: Adds a type that represents the information of an owned blob. This is useful for storing the blob reference in DDB Depends on D8777, D8790 Test Plan: Tested later with real usage. Reviewers: michal, jon Reviewed By: michal Subscribers: ashoat, tomek Differential Revision: https://phab.comm.dev/D8791
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#[cfg(feature = "blob-client")] | ||
pub mod client; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use aws_sdk_dynamodb::types::AttributeValue; | ||
use derive_more::Constructor; | ||
use std::collections::HashMap; | ||
|
||
use crate::database::{AttributeTryInto, DBItemError, TryFromAttribute}; | ||
|
||
const BLOB_HASH_DDB_MAP_KEY: &str = "blob_hash"; | ||
const HOLDER_DDB_MAP_KEY: &str = "holder"; | ||
|
||
/// Blob owning information - stores both blob_hash and holder | ||
#[derive(Clone, Debug, Constructor)] | ||
pub struct BlobInfo { | ||
pub blob_hash: String, | ||
pub holder: String, | ||
} | ||
|
||
impl From<BlobInfo> for AttributeValue { | ||
fn from(value: BlobInfo) -> Self { | ||
let map = HashMap::from([ | ||
( | ||
BLOB_HASH_DDB_MAP_KEY.to_string(), | ||
AttributeValue::S(value.blob_hash), | ||
), | ||
( | ||
HOLDER_DDB_MAP_KEY.to_string(), | ||
AttributeValue::S(value.holder), | ||
), | ||
]); | ||
AttributeValue::M(map) | ||
} | ||
} | ||
impl From<&BlobInfo> for AttributeValue { | ||
fn from(value: &BlobInfo) -> Self { | ||
AttributeValue::from(value.to_owned()) | ||
} | ||
} | ||
|
||
impl TryFromAttribute for BlobInfo { | ||
fn try_from_attr( | ||
attribute_name: impl Into<String>, | ||
attribute: Option<AttributeValue>, | ||
) -> Result<Self, DBItemError> { | ||
let attr_name: String = attribute_name.into(); | ||
let mut inner_map: HashMap<String, AttributeValue> = | ||
attribute.attr_try_into(&attr_name)?; | ||
let blob_hash = inner_map | ||
.remove("blob_hash") | ||
.attr_try_into(format!("{attr_name}.blob_hash"))?; | ||
let holder = inner_map | ||
.remove("holder") | ||
.attr_try_into(format!("{attr_name}.holder"))?; | ||
Ok(BlobInfo { blob_hash, holder }) | ||
} | ||
} |