Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate the blueprint schema when we try to activate a blueprint sent from SDK #6283

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,11 @@ impl App {
);
let app_id = info.application_id.clone();
if cmd.make_default {
store_hub.set_default_blueprint_for_app(&app_id, store_id);
store_hub
.set_default_blueprint_for_app(&app_id, store_id)
.unwrap_or_else(|err| {
re_log::warn!("Failed to make blueprint default: {err}");
});
}
if cmd.make_active {
store_hub
Expand Down Expand Up @@ -1266,6 +1270,7 @@ fn blueprint_loader() -> BlueprintPersistence {
BlueprintPersistence {
loader: None,
saver: None,
validator: Some(Box::new(crate::blueprint::is_valid_blueprint)),
}
}

Expand Down Expand Up @@ -1318,6 +1323,7 @@ fn blueprint_loader() -> BlueprintPersistence {
BlueprintPersistence {
loader: Some(Box::new(load_blueprint_from_disk)),
saver: Some(Box::new(save_blueprint_to_disk)),
validator: Some(Box::new(crate::blueprint::is_valid_blueprint)),
}
}

Expand Down
28 changes: 27 additions & 1 deletion crates/re_viewer_context/src/store_hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ pub type BlueprintLoader =
/// Save a blueprint to persisted storage, e.g. disk.
pub type BlueprintSaver = dyn Fn(&ApplicationId, &EntityDb) -> anyhow::Result<()> + Send + Sync;

/// Validate a blueprint against the current blueprint schema requirements.
pub type BlueprintValidator = dyn Fn(&EntityDb) -> bool + Send + Sync;

/// How to save and load blueprints
pub struct BlueprintPersistence {
pub loader: Option<Box<BlueprintLoader>>,
pub saver: Option<Box<BlueprintSaver>>,
pub validator: Option<Box<BlueprintValidator>>,
}

/// Convenient information used for `MemoryPanel`
Expand Down Expand Up @@ -94,6 +98,7 @@ impl StoreHub {
BlueprintPersistence {
loader: None,
saver: None,
validator: None,
},
&|_| {},
)
Expand Down Expand Up @@ -416,10 +421,24 @@ impl StoreHub {
&mut self,
app_id: &ApplicationId,
blueprint_id: &StoreId,
) {
) -> anyhow::Result<()> {
let blueprint = self
.store_bundle
.get(blueprint_id)
.context("missing blueprint")?;

// TODO(#6282): Improve this error message.
if let Some(validator) = &self.persistence.validator {
if !(validator)(blueprint) {
anyhow::bail!("Blueprint failed validation");
}
}

re_log::debug!("Switching default blueprint for {app_id} to {blueprint_id}");
self.default_blueprint_by_app_id
.insert(app_id.clone(), blueprint_id.clone());

Ok(())
}

/// Clear the current default blueprint
Expand Down Expand Up @@ -469,6 +488,13 @@ impl StoreHub {
.get(blueprint_id)
.context("missing blueprint")?;

// TODO(#6282): Improve this error message.
if let Some(validator) = &self.persistence.validator {
if !(validator)(blueprint) {
anyhow::bail!("Blueprint failed validation");
}
}

let new_blueprint = blueprint.clone_with_new_id(new_id.clone())?;

self.store_bundle.insert(new_blueprint);
Expand Down
Loading