Skip to content

Commit 1487096

Browse files
authored
feat(stackable-versioned): Add from_name parameter validation (#865)
* feat(stackable-versioned): Add from_name parameter validation * chore: Update changelog
1 parent 4c67dbd commit 1487096

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

Diff for: crates/stackable-versioned-macros/src/attrs/common/item.rs

+29
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl ItemAttributes {
147147
errors.handle(self.validate_action_combinations(item_ident, item_type));
148148
errors.handle(self.validate_action_order(item_ident, item_type));
149149
errors.handle(self.validate_item_name(item_ident, item_type));
150+
errors.handle(self.validate_changed_item_name(item_type));
150151
errors.handle(self.validate_item_attributes(item_attrs));
151152

152153
// TODO (@Techassi): Add hint if a field or variant is added in the
@@ -294,6 +295,34 @@ impl ItemAttributes {
294295
}
295296
Ok(())
296297
}
298+
299+
/// This associated function is called by the top-level validation function
300+
/// and validates that parameters provided to the `changed` actions are
301+
/// valid.
302+
fn validate_changed_item_name(&self, item_type: &ItemType) -> Result<(), Error> {
303+
let prefix = match item_type {
304+
ItemType::Field => DEPRECATED_FIELD_PREFIX,
305+
ItemType::Variant => DEPRECATED_VARIANT_PREFIX,
306+
};
307+
308+
let mut errors = Error::accumulator();
309+
310+
// This ensures that `from_name` doesn't include the deprecation prefix.
311+
for change in &self.changes {
312+
if let Some(from_name) = change.from_name.as_ref() {
313+
if from_name.starts_with(prefix) {
314+
errors.push(
315+
Error::custom(format!(
316+
"the previous {item_type} name must not start with the deprecation prefix"
317+
))
318+
.with_span(&from_name.span()),
319+
);
320+
}
321+
}
322+
}
323+
324+
errors.finish()
325+
}
297326
}
298327

299328
// TODO (@Techassi): Add validation for when default_fn is "" (empty path).

Diff for: crates/stackable-versioned-macros/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,11 @@ pub struct FooSpec {
454454
bar: usize,
455455
baz: bool,
456456
}
457+
458+
# fn main() {
457459
let merged_crd = Foo::merged_crd("v1").unwrap();
458460
println!("{}", serde_yaml::to_string(&merged_crd).unwrap());
461+
# }
459462
```
460463
"#
461464
)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use stackable_versioned_macros::versioned;
2+
3+
fn main() {
4+
#[versioned(
5+
version(name = "v1alpha1"),
6+
version(name = "v1beta1"),
7+
version(name = "v1")
8+
)]
9+
struct Foo {
10+
#[versioned(
11+
changed(since = "v1beta1", from_name = "deprecated_bar"),
12+
changed(since = "v1", from_name = "deprecated_baz")
13+
)]
14+
bar: usize,
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: the previous field name must not start with the deprecation prefix
2+
--> tests/default/fail/changed.rs:11:52
3+
|
4+
11 | changed(since = "v1beta1", from_name = "deprecated_bar"),
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: the previous field name must not start with the deprecation prefix
8+
--> tests/default/fail/changed.rs:12:47
9+
|
10+
12 | changed(since = "v1", from_name = "deprecated_baz")
11+
| ^^^^^^^^^^^^^^^^

Diff for: crates/stackable-versioned/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9+
- Add `from_name` parameter validation ([#865]).
910
- Add new `from_type` parameter to `changed()` action ([#844]).
1011
- Pass through container and item attributes (including doc-comments). Add
1112
attribute for version specific docs ([#847]).
@@ -33,6 +34,7 @@ All notable changes to this project will be documented in this file.
3334
[#857]: https://github.com/stackabletech/operator-rs/pull/857
3435
[#859]: https://github.com/stackabletech/operator-rs/pull/859
3536
[#860]: https://github.com/stackabletech/operator-rs/pull/860
37+
[#865]: https://github.com/stackabletech/operator-rs/pull/865
3638

3739
## [0.1.1] - 2024-07-10
3840

0 commit comments

Comments
 (0)