Skip to content

Commit

Permalink
Fix parsing on the new fields of deployment audit log. (#31569)
Browse files Browse the repository at this point in the history
Whoops - make this field backward compatible!

GitOrigin-RevId: d600142992bc92ba472c78cbdf98323e1228a7ca
  • Loading branch information
nipunn1313 authored and Convex, Inc. committed Nov 14, 2024
1 parent bf9a0a5 commit 07f93ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/model/src/deployment_audit_log/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use value::{
codegen_convex_serialization,
obj,
remove_int64,
remove_nullable_int64,
remove_nullable_object,
remove_nullable_vec_of_strings,
remove_object,
remove_string,
remove_vec,
Expand Down Expand Up @@ -401,7 +403,8 @@ impl TryFrom<ConvexObject> for DeploymentAuditLogEvent {
.map(|s| TableName::from_str(s))
.try_collect()?;
let table_names_deleted =
remove_vec_of_strings(&mut fields, "table_names_deleted")?
remove_nullable_vec_of_strings(&mut fields, "table_names_deleted")?
.unwrap_or_default()
.iter()
.map(|s| TableName::from_str(s))
.try_collect()?;
Expand All @@ -413,7 +416,8 @@ impl TryFrom<ConvexObject> for DeploymentAuditLogEvent {
requestor: remove_nullable_object(&mut fields, "requestor")?
.unwrap_or(ImportRequestor::SnapshotImport),
table_names_deleted,
table_count_deleted: remove_int64(&mut fields, "table_count_deleted")? as u64,
table_count_deleted: remove_nullable_int64(&mut fields, "table_count_deleted")?
.unwrap_or(0) as u64,
}
},
_ => anyhow::bail!("action {action} unrecognized"),
Expand Down
3 changes: 3 additions & 0 deletions crates/value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ pub use crate::{
object::{
remove_boolean,
remove_int64,
remove_nullable_int64,
remove_nullable_object,
remove_nullable_string,
remove_nullable_vec,
remove_nullable_vec_of_strings,
remove_object,
remove_string,
remove_vec,
Expand Down
39 changes: 39 additions & 0 deletions crates/value/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ pub fn remove_int64(
}
}

pub fn remove_nullable_int64(
fields: &mut BTreeMap<FieldName, ConvexValue>,
field: &str,
) -> anyhow::Result<Option<i64>> {
match fields.remove(field) {
Some(ConvexValue::Int64(i)) => Ok(Some(i)),
None => Ok(None),
v => anyhow::bail!("expected int for {field}, got {v:?}"),
}
}

pub fn remove_object<E, T: TryFrom<ConvexObject, Error = E>>(
fields: &mut BTreeMap<FieldName, ConvexValue>,
field: &str,
Expand Down Expand Up @@ -288,6 +299,34 @@ pub fn remove_vec_of_strings(
.try_collect()
}

pub fn remove_nullable_vec(
fields: &mut BTreeMap<FieldName, ConvexValue>,
field: &str,
) -> anyhow::Result<Option<Vec<ConvexValue>>> {
match fields.remove(field) {
Some(ConvexValue::Array(a)) => Ok(Some(a.into())),
None => Ok(None),
v => anyhow::bail!("expected array for {field}, got {v:?}"),
}
}
pub fn remove_nullable_vec_of_strings(
fields: &mut BTreeMap<FieldName, ConvexValue>,
field: &str,
) -> anyhow::Result<Option<Vec<String>>> {
let Some(values) = remove_nullable_vec(fields, field)? else {
return Ok(None);
};
Ok(Some(
values
.into_iter()
.map(|value| match value {
ConvexValue::String(s) => anyhow::Ok(s.into()),
v => anyhow::bail!("expected string in array at {field}, got {v:?}"),
})
.try_collect()?,
))
}

impl Size for ConvexObject {
fn size(&self) -> usize {
self.size
Expand Down

0 comments on commit 07f93ce

Please sign in to comment.