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

[reconfigurator] Introduce IdMap and use it in BlueprintZonesConfig #7327

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

jgallagher
Copy link
Contributor

@andrewjstone mentioned being bothered by our BTreeMap<Id, ValueWithAnId> maps on #7315:

The key to the map must always match the value in BlueprintZoneConfig. It's a shame a mismatch is now representable, and we can go about trying to change that if necessary. We can also just make it part of blippy for now. FWIW, this matches the pattern in BlueprintDatasetsConfig.

This has bugged me too, and adding a blippy check crossed my mind. But I think it would be better to make this just not possible, so this PR introduces a newtype wrapper around BTreeMap<T::Id, T> that enforces "key must be value's ID" via a mix of compile time API constraints (e.g., insert only takes the value and generates the key on its own) and runtime checks (e.g., when mutating a value stored in the map, it will panic if that mutation changes the ID of the value).

The new code is all in id_map.rs, and the rest of the changes are replacing the BlueprintZonesConfig map with it. If this looks reasonable to folks, followup PRs to change the other similar BTreeMaps to use IdMap should be straightforward.

{
"$ref": "#/components/schemas/IdMapBlueprintZoneConfig"
}
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this changed, exactly. Does JsonSchema do something special for BTreeMap that it doesn't know how to do for IdMap?

I think this is semantically equivalent, though; allOf says to apply all of the inner subschemas, and since this subschema is the same as what the prior schema was, this is basically undoing an inlining?

Copy link
Contributor

@andrewjstone andrewjstone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks for all the work here @jgallagher.

I have no idea about the JsonSchema question. Maybe @ahl does.

@@ -794,7 +795,7 @@ impl DataStore {
e.to_string()
))
})?;
sled_zones.zones.insert(zone.id, zone);
sled_zones.zones.insert(zone);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Self { original_id: borrowed.id(), borrowed: Some(borrowed) }
}

pub fn into_ref(mut self) -> &'a T {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice closing of this loophole too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants