Skip to content

Commit

Permalink
[services-lib] Add BlobInfo utility type
Browse files Browse the repository at this point in the history
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
barthap committed Aug 16, 2023
1 parent fc8dbbf commit 57ec708
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions services/comm-services-lib/src/blob/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[cfg(feature = "blob-client")]
pub mod client;
pub mod types;
54 changes: 54 additions & 0 deletions services/comm-services-lib/src/blob/types.rs
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 })
}
}

0 comments on commit 57ec708

Please sign in to comment.