Skip to content

feat(stackable-versioned): Add from_name parameter validation #865

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

Merged
merged 2 commits into from
Sep 17, 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
29 changes: 29 additions & 0 deletions crates/stackable-versioned-macros/src/attrs/common/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions crates/stackable-versioned-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
# }
```
"#
)]
Expand Down
16 changes: 16 additions & 0 deletions crates/stackable-versioned-macros/tests/default/fail/changed.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
Original file line number Diff line number Diff line change
@@ -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")
| ^^^^^^^^^^^^^^^^
2 changes: 2 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]).
Expand Down Expand Up @@ -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

Expand Down