From 761e8e079aa82282e99c26ec7786354b5c4560d0 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 16 Sep 2024 16:46:04 +0200 Subject: [PATCH 1/2] feat(stackable-versioned): Add from_name parameter validation --- .../src/attrs/common/item.rs | 29 +++++++++++++++++++ crates/stackable-versioned-macros/src/lib.rs | 3 ++ .../tests/default/fail/changed.rs | 16 ++++++++++ .../tests/default/fail/changed.stderr | 11 +++++++ 4 files changed, 59 insertions(+) create mode 100644 crates/stackable-versioned-macros/tests/default/fail/changed.rs create mode 100644 crates/stackable-versioned-macros/tests/default/fail/changed.stderr diff --git a/crates/stackable-versioned-macros/src/attrs/common/item.rs b/crates/stackable-versioned-macros/src/attrs/common/item.rs index 839ae3ec6..42b6ac953 100644 --- a/crates/stackable-versioned-macros/src/attrs/common/item.rs +++ b/crates/stackable-versioned-macros/src/attrs/common/item.rs @@ -147,6 +147,7 @@ impl ItemAttributes { errors.handle(self.validate_action_combinations(item_ident, item_type)); errors.handle(self.validate_action_order(item_ident, item_type)); errors.handle(self.validate_item_name(item_ident, item_type)); + errors.handle(self.validate_changed_item_name(item_type)); errors.handle(self.validate_item_attributes(item_attrs)); // TODO (@Techassi): Add hint if a field or variant is added in the @@ -294,6 +295,34 @@ impl ItemAttributes { } Ok(()) } + + /// This associated function is called by the top-level validation function + /// and validates that parameters provided to the `changed` actions are + /// valid. + fn validate_changed_item_name(&self, item_type: &ItemType) -> Result<(), Error> { + let prefix = match item_type { + ItemType::Field => DEPRECATED_FIELD_PREFIX, + ItemType::Variant => DEPRECATED_VARIANT_PREFIX, + }; + + let mut errors = Error::accumulator(); + + // This ensures that `from_name` doesn't include the deprecation prefix. + for change in &self.changes { + if let Some(from_name) = change.from_name.as_ref() { + if from_name.starts_with(prefix) { + errors.push( + Error::custom(format!( + "the previous {item_type} name must not start with the deprecation prefix" + )) + .with_span(&from_name.span()), + ); + } + } + } + + errors.finish() + } } // TODO (@Techassi): Add validation for when default_fn is "" (empty path). diff --git a/crates/stackable-versioned-macros/src/lib.rs b/crates/stackable-versioned-macros/src/lib.rs index ca18d81d5..9aa688f3f 100644 --- a/crates/stackable-versioned-macros/src/lib.rs +++ b/crates/stackable-versioned-macros/src/lib.rs @@ -454,8 +454,11 @@ pub struct FooSpec { bar: usize, baz: bool, } + +# fn main() { let merged_crd = Foo::merged_crd("v1").unwrap(); println!("{}", serde_yaml::to_string(&merged_crd).unwrap()); +# } ``` "# )] diff --git a/crates/stackable-versioned-macros/tests/default/fail/changed.rs b/crates/stackable-versioned-macros/tests/default/fail/changed.rs new file mode 100644 index 000000000..2d17aa768 --- /dev/null +++ b/crates/stackable-versioned-macros/tests/default/fail/changed.rs @@ -0,0 +1,16 @@ +use stackable_versioned_macros::versioned; + +fn main() { + #[versioned( + version(name = "v1alpha1"), + version(name = "v1beta1"), + version(name = "v1") + )] + struct Foo { + #[versioned( + changed(since = "v1beta1", from_name = "deprecated_bar"), + changed(since = "v1", from_name = "deprecated_baz") + )] + bar: usize, + } +} diff --git a/crates/stackable-versioned-macros/tests/default/fail/changed.stderr b/crates/stackable-versioned-macros/tests/default/fail/changed.stderr new file mode 100644 index 000000000..a6d0d4070 --- /dev/null +++ b/crates/stackable-versioned-macros/tests/default/fail/changed.stderr @@ -0,0 +1,11 @@ +error: the previous field name must not start with the deprecation prefix + --> tests/default/fail/changed.rs:11:52 + | +11 | changed(since = "v1beta1", from_name = "deprecated_bar"), + | ^^^^^^^^^^^^^^^^ + +error: the previous field name must not start with the deprecation prefix + --> tests/default/fail/changed.rs:12:47 + | +12 | changed(since = "v1", from_name = "deprecated_baz") + | ^^^^^^^^^^^^^^^^ From efea381228dbd32237908018b51795a6f639c576 Mon Sep 17 00:00:00 2001 From: Techassi Date: Mon, 16 Sep 2024 17:26:59 +0200 Subject: [PATCH 2/2] chore: Update changelog --- crates/stackable-versioned/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 3161f9512..3b82c52d9 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added +- Add `from_name` parameter validation ([#865]). - Add new `from_type` parameter to `changed()` action ([#844]). - Pass through container and item attributes (including doc-comments). Add attribute for version specific docs ([#847]). @@ -33,6 +34,7 @@ All notable changes to this project will be documented in this file. [#857]: https://github.com/stackabletech/operator-rs/pull/857 [#859]: https://github.com/stackabletech/operator-rs/pull/859 [#860]: https://github.com/stackabletech/operator-rs/pull/860 +[#865]: https://github.com/stackabletech/operator-rs/pull/865 ## [0.1.1] - 2024-07-10