Skip to content

Commit 7bddc97

Browse files
authored
fix(stackable-versioned): Check whether to skip all from impls when versioning a module (#926)
* fix(stackable-versioned): Check whether to skip all from impls when versioning a module * test(stackable-versioned): Ensure from impls are skipped when versioning a module with applicable skip options * chore(stackable-versioned): Correctly name a test * chore(stackable-versioned): Update changelog * chore(stackable-versioned): Remove space from test filename
1 parent 3f9c4cd commit 7bddc97

10 files changed

+141
-7
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1beta1"),
4+
options(skip(from))
5+
)]
6+
// ---
7+
pub(crate) mod versioned {
8+
pub struct Foo {
9+
bar: usize,
10+
11+
#[versioned(added(since = "v1beta1"))]
12+
baz: bool,
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1beta1", skip(from)),
4+
version(name = "v1")
5+
)]
6+
// ---
7+
pub(crate) mod versioned {
8+
pub struct Foo {
9+
bar: usize,
10+
11+
#[versioned(added(since = "v1beta1"))]
12+
baz: bool,
13+
}
14+
15+
pub struct Bar {
16+
foo: usize,
17+
18+
#[versioned(added(since = "v1beta1"))]
19+
faz: bool,
20+
}
21+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module.rs.snap

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module_for_version.rs.snap

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/stackable-versioned-macros/src/codegen/changes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ impl ChangesetExt for BTreeMap<Version, ItemStatus> {
182182
ty,
183183
..
184184
} => (ident, ty, *previously_deprecated),
185+
// TODO (@NickLarsenNZ): Explain why it is unreachable, as it can be reached during testing.
186+
// To reproduce, use an invalid version, eg: #[versioned(deprecated(since = "v99"))]
185187
_ => unreachable!(),
186188
};
187189

crates/stackable-versioned-macros/src/codegen/module.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(crate) struct Module {
2222
versions: Vec<VersionDefinition>,
2323
containers: Vec<Container>,
2424
preserve_module: bool,
25+
skip_from: bool,
2526
ident: IdentString,
2627
vis: Visibility,
2728
}
@@ -31,13 +32,15 @@ impl Module {
3132
pub(crate) fn new(
3233
ModuleInput { ident, vis, .. }: ModuleInput,
3334
preserve_module: bool,
35+
skip_from: bool,
3436
versions: Vec<VersionDefinition>,
3537
containers: Vec<Container>,
3638
) -> Self {
3739
Self {
3840
ident: ident.into(),
3941
preserve_module,
4042
containers,
43+
skip_from,
4144
versions,
4245
vis,
4346
}
@@ -77,11 +80,13 @@ impl Module {
7780
for container in &self.containers {
7881
container_definitions.extend(container.generate_definition(version));
7982

80-
from_impls.extend(container.generate_from_impl(
81-
version,
82-
versions.peek().copied(),
83-
self.preserve_module,
84-
));
83+
if !self.skip_from {
84+
from_impls.extend(container.generate_from_impl(
85+
version,
86+
versions.peek().copied(),
87+
self.preserve_module,
88+
));
89+
}
8590

8691
// Generate Kubernetes specific code which is placed outside of the container
8792
// definition.

crates/stackable-versioned-macros/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2::
507507

508508
let versions: Vec<VersionDefinition> = (&module_attributes).into();
509509
let preserve_modules = module_attributes.preserve_module.is_present();
510+
let skip_from = module_attributes
511+
.common_root_arguments
512+
.options
513+
.skip
514+
.as_ref()
515+
.map_or(false, |opts| opts.from.is_present());
510516

511517
let module_span = item_mod.span();
512518
let module_input = ModuleInput {
@@ -541,7 +547,14 @@ fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2::
541547
containers.push(container);
542548
}
543549

544-
Module::new(module_input, preserve_modules, versions, containers).generate_tokens()
550+
Module::new(
551+
module_input,
552+
preserve_modules,
553+
skip_from,
554+
versions,
555+
containers,
556+
)
557+
.generate_tokens()
545558
}
546559
Item::Enum(item_enum) => {
547560
let container_attributes: StandaloneContainerAttributes =

crates/stackable-versioned/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ All notable changes to this project will be documented in this file.
3131
- Correctly emit Kubernetes code when macro is used on modules ([#912]).
3232
- Use `.into()` on all field conversions ([#925]).
3333
- Remove invalid type comparison on field conversion because the semantics are unknown ([#925]).
34+
- Check whether to skip all from impls when versioning a module ([#926]).
3435

3536
[#891]: https://github.com/stackabletech/operator-rs/pull/891
3637
[#912]: https://github.com/stackabletech/operator-rs/pull/912
@@ -42,6 +43,7 @@ All notable changes to this project will be documented in this file.
4243
[#923]: https://github.com/stackabletech/operator-rs/pull/923
4344
[#924]: https://github.com/stackabletech/operator-rs/pull/924
4445
[#925]: https://github.com/stackabletech/operator-rs/pull/925
46+
[#926]: https://github.com/stackabletech/operator-rs/pull/926
4547

4648
## [0.4.1] - 2024-10-23
4749

0 commit comments

Comments
 (0)