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

Reorganize kvp so that KeyValuePairs is an alias for BTreeMap instead of newtyping it #889

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

Conversation

nightkr
Copy link
Member

@nightkr nightkr commented Oct 14, 2024

Description

This is effectively an extended version of #888 (and supersedes it).

Definition of Done Checklist

  • Not all of these items are applicable to all PRs, the author should update this template to only leave the boxes in that are relevant
  • Please make sure all these things are done and tick the boxes

Author

Preview Give feedback

Reviewer

Preview Give feedback

Acceptance

Preview Give feedback

@nightkr nightkr requested a review from Techassi October 14, 2024 09:27
@nightkr nightkr linked an issue Oct 14, 2024 that may be closed by this pull request
Copy link
Member

@Techassi Techassi left a comment

Choose a reason for hiding this comment

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

Very nice job, I like it!

Left a bunch of small comments, nothing major.


/// Constructs a `secrets.stackable.tech/class` annotation.
pub fn secret_class(secret_class: &str) -> Result<Annotation, AnnotationError> {
Annotation::try_from(("secrets.stackable.tech/class", secret_class))
Copy link
Member

Choose a reason for hiding this comment

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

note: I think it might be useful to move these well-known keys into (non pub) constants in this module. This groups these keys in a single place.

Copy link
Member

Choose a reason for hiding this comment

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

You can take a look at how it is done in the label module.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd argue the opposite, moving towards dropping the consts module entirely since you should be using the typed interface instead. It feels weird to have both a typed and stringly interface for the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Especially when the label values are all hardcoded in the doccomments anyway.

Comment on lines 112 to 116
impl From<&Key> for String {
fn from(value: &Key) -> Self {
value.to_string()
}
}
Copy link
Member

Choose a reason for hiding this comment

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

question: I'm curious why this is needed? Shouldn't we be able to call to_string() on &Key anyway?

Copy link
Member Author

Choose a reason for hiding this comment

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

It makes using it in a SNAFU error slightly nicer (.context(BlahSnafu { key }) instead of .with_context(|_| BlahSnafu { key: key.to_string() })), but.. arguably the SNAFU error should just take a Key instead to begin with.

Copy link
Member Author

Choose a reason for hiding this comment

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

pub mod consts;
mod key;
mod label;
pub mod label;
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Let's group public modules with each other. To be more specific, let's move this module declaration to the annotation one above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +24 to +27
#[cfg(doc)]
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
#[cfg(doc)]
use std::ops::Deref;
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Move these imports to the std library and external crate imports at the top of the file.

Copy link
Member Author

@nightkr nightkr Oct 16, 2024

Choose a reason for hiding this comment

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

I think it makes sense to call out doc-only "fake" imports as different from regular one (just like publics), though we definitely haven't done a great job at consistency around that in general.

crates/stackable-operator/src/kvp/mod.rs Outdated Show resolved Hide resolved
crates/stackable-operator/src/kvp/mod.rs Outdated Show resolved Hide resolved
crates/stackable-operator/src/kvp/mod.rs Outdated Show resolved Hide resolved
- `KeyValuePairs::insert(&mut self, kvp)`: use `::extend(&mut self, [kvp])` instead.
- `KeyValuePairs::try_from`: you may need to use `::try_from_iter` instead.
- `KeyValuePairs::contains_key`: unvalidated keys will need to use `::contains_str_key` instead.
- `Into<BTreeMap<String, String>>`: use `::to_unvalidated` instead.
Copy link
Member

Choose a reason for hiding this comment

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

note: I can see why this name is chosen, but can maybe be a little misleading. The source is obviously valid, the result is also valid (if not mutated).

Let's discuss some alternate names, like to_btree_map for example.

Copy link
Member Author

Choose a reason for hiding this comment

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

to_btree_map is confusing since it is already one. I could see to_strings or to_string_map, but I think those are too much about the "mechanical what" rather than the "what is the effect?".

crates/stackable-operator/CHANGELOG.md Outdated Show resolved Hide resolved
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.

KeyValuePairs::extend implements a nonsense merge
2 participants