Skip to content

Commit

Permalink
Use same deserializer function for all new Timestamped fields (#6996)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 5, 2024
1 parent 4b0c39e commit 41522aa
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ fn init_instruction_counts_log() -> InstructionCountsLog {

#[derive(Serialize, Deserialize)]
struct Data {
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
is_public: Timestamped<bool>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
name: Timestamped<String>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
description: Timestamped<String>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
rules: Timestamped<AccessRulesInternal>,
#[serde(deserialize_with = "deserialize_optional_document")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
avatar: Timestamped<Option<Document>>,
#[serde(deserialize_with = "deserialize_optional_document")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
banner: Timestamped<Option<Document>>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
permissions: Timestamped<CommunityPermissions>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
gate_config: Timestamped<Option<AccessGateConfigInternal>>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
primary_language: Timestamped<String>,
user_index_canister_id: CanisterId,
local_user_index_canister_id: CanisterId,
Expand All @@ -355,9 +355,9 @@ struct Data {
channels: Channels,
events: CommunityEvents,
invited_users: InvitedUsers,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
invite_code: Timestamped<Option<u64>>,
#[serde(deserialize_with = "deserialize_maybe_timestamped")]
#[serde(deserialize_with = "deserialize_to_timestamped")]
invite_code_enabled: Timestamped<bool>,
frozen: Timestamped<Option<FrozenGroupInfo>>,
timer_jobs: TimerJobs<TimerJob>,
Expand Down Expand Up @@ -386,27 +386,6 @@ struct Data {
bot_permissions: BTreeMap<UserId, SlashCommandPermissions>,
}

fn deserialize_maybe_timestamped<'de, D: Deserializer<'de>, T: Deserialize<'de>>(d: D) -> Result<Timestamped<T>, D::Error> {
let maybe_timestamped = MaybeTimestamped::deserialize(d)?;
Ok(maybe_timestamped.into())
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(untagged)]
pub enum MaybeTimestamped<T> {
Timestamped(Timestamped<T>),
Value(T),
}

impl<T> From<MaybeTimestamped<T>> for Timestamped<T> {
fn from(value: MaybeTimestamped<T>) -> Self {
match value {
MaybeTimestamped::Timestamped(ts) => ts,
MaybeTimestamped::Value(value) => Timestamped::new(value, now_millis()),
}
}
}

impl Data {
#[allow(clippy::too_many_arguments)]
fn new(
Expand Down Expand Up @@ -966,7 +945,7 @@ pub struct AddUsersToChannelResult {
pub users_limit_reached: Vec<UserId>,
}

fn deserialize_optional_document<'de, D: Deserializer<'de>>(d: D) -> Result<Timestamped<Option<Document>>, D::Error> {
let document = Option::deserialize(d)?;
Ok(Timestamped::new(document, now_millis()))
fn deserialize_to_timestamped<'de, D: Deserializer<'de>, T: Deserialize<'de>>(d: D) -> Result<Timestamped<T>, D::Error> {
let value = T::deserialize(d)?;
Ok(Timestamped::new(value, now_millis()))
}

0 comments on commit 41522aa

Please sign in to comment.