feat: add macro to generate ToEncryptable trait (#6313)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com>
This commit is contained in:
Kartikeya Hegde
2024-11-04 11:24:13 +05:30
committed by GitHub
parent adc5262f13
commit 19cf0f7437
19 changed files with 949 additions and 738 deletions

View File

@ -1,12 +1,5 @@
use common_utils::{
crypto::{self, Encryptable},
encryption::Encryption,
pii::EmailStrategy,
types::keymanager::ToEncryptable,
};
use common_utils::{crypto, encryption::Encryption};
use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
use masking::{Secret, SwitchStrategy};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;
@ -74,48 +67,6 @@ pub struct EncryptableAddress {
pub email: crypto::OptionalEncryptableEmail,
}
impl ToEncryptable<EncryptableAddress, Secret<String>, Encryption> for Address {
fn to_encryptable(self) -> FxHashMap<String, Encryption> {
let mut map = FxHashMap::with_capacity_and_hasher(9, Default::default());
self.line1.map(|x| map.insert("line1".to_string(), x));
self.line2.map(|x| map.insert("line2".to_string(), x));
self.line3.map(|x| map.insert("line3".to_string(), x));
self.zip.map(|x| map.insert("zip".to_string(), x));
self.state.map(|x| map.insert("state".to_string(), x));
self.first_name
.map(|x| map.insert("first_name".to_string(), x));
self.last_name
.map(|x| map.insert("last_name".to_string(), x));
self.phone_number
.map(|x| map.insert("phone_number".to_string(), x));
self.email.map(|x| map.insert("email".to_string(), x));
map
}
fn from_encryptable(
mut hashmap: FxHashMap<String, Encryptable<Secret<String>>>,
) -> common_utils::errors::CustomResult<EncryptableAddress, common_utils::errors::ParsingError>
{
Ok(EncryptableAddress {
line1: hashmap.remove("line1"),
line2: hashmap.remove("line2"),
line3: hashmap.remove("line3"),
zip: hashmap.remove("zip"),
state: hashmap.remove("state"),
first_name: hashmap.remove("first_name"),
last_name: hashmap.remove("last_name"),
phone_number: hashmap.remove("phone_number"),
email: hashmap.remove("email").map(|email| {
let encryptable: Encryptable<Secret<String, EmailStrategy>> = Encryptable::new(
email.clone().into_inner().switch_strategy(),
email.into_encrypted(),
);
encryptable
}),
})
}
}
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay, Serialize, Deserialize)]
#[diesel(table_name = address)]
pub struct AddressUpdateInternal {

View File

@ -1,11 +1,7 @@
use common_utils::{
crypto::OptionalEncryptableSecretString, custom_serde, encryption::Encryption,
types::keymanager::ToEncryptable,
};
use common_utils::{custom_serde, encryption::Encryption};
use diesel::{
expression::AsExpression, AsChangeset, Identifiable, Insertable, Queryable, Selectable,
};
use masking::Secret;
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;
@ -63,38 +59,6 @@ pub struct Event {
pub metadata: Option<EventMetadata>,
}
pub struct EventWithEncryption {
pub request: Option<Encryption>,
pub response: Option<Encryption>,
}
pub struct EncryptableEvent {
pub request: OptionalEncryptableSecretString,
pub response: OptionalEncryptableSecretString,
}
impl ToEncryptable<EncryptableEvent, Secret<String>, Encryption> for EventWithEncryption {
fn to_encryptable(self) -> rustc_hash::FxHashMap<String, Encryption> {
let mut map = rustc_hash::FxHashMap::default();
self.request.map(|x| map.insert("request".to_string(), x));
self.response.map(|x| map.insert("response".to_string(), x));
map
}
fn from_encryptable(
mut hashmap: rustc_hash::FxHashMap<
String,
common_utils::crypto::Encryptable<Secret<String>>,
>,
) -> common_utils::errors::CustomResult<EncryptableEvent, common_utils::errors::ParsingError>
{
Ok(EncryptableEvent {
request: hashmap.remove("request"),
response: hashmap.remove("response"),
})
}
}
#[derive(Clone, Debug, Deserialize, Serialize, AsExpression, diesel::FromSqlRow)]
#[diesel(sql_type = diesel::sql_types::Jsonb)]
pub enum EventMetadata {