Skip to content

Commit

Permalink
Remove all nulls from the output path tree
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Aug 21, 2024
1 parent 8cc2b90 commit fe868ce
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/cli/cmd/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ impl CommandExecute for PathsSubcommand {
async fn execute(self) -> color_eyre::Result<ExitCode> {
let release_ref = parse_release_ref(&self.release_ref)?;

let paths = FlakeHubClient::paths(self.api_addr.as_ref(), &release_ref).await?;
let mut paths = FlakeHubClient::paths(self.api_addr.as_ref(), &release_ref).await?;
clear_nulls(&mut paths);

tracing::debug!(
r#ref = release_ref.to_string(),
Expand Down Expand Up @@ -59,3 +60,25 @@ impl Serialize for PathNode {
}
}
}

// Recursively removes any nulls from the output path tree
fn clear_nulls(map: &mut HashMap<String, PathNode>) {
let keys_to_remove: Vec<String> = map
.iter_mut()
.filter_map(|(key, value)| match value {
PathNode::PathMap(ref mut inner_map) => {
clear_nulls(inner_map);
if inner_map.is_empty() {
Some(key.clone())
} else {
None
}
}
_ => None,
})
.collect();

for key in keys_to_remove {
map.remove(&key);
}
}

0 comments on commit fe868ce

Please sign in to comment.