diff --git a/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_field.rs b/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_for_version.rs similarity index 100% rename from crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_field.rs rename to crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_for_version.rs diff --git a/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module.rs b/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module.rs new file mode 100644 index 000000000..4ba3d1a87 --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module.rs @@ -0,0 +1,14 @@ +#[versioned( + version(name = "v1alpha1"), + version(name = "v1beta1"), + options(skip(from)) +)] +// --- +pub(crate) mod versioned { + pub struct Foo { + bar: usize, + + #[versioned(added(since = "v1beta1"))] + baz: bool, + } +} diff --git a/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module_for_version.rs b/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module_for_version.rs new file mode 100644 index 000000000..b50a410a6 --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module_for_version.rs @@ -0,0 +1,21 @@ +#[versioned( + version(name = "v1alpha1"), + version(name = "v1beta1", skip(from)), + version(name = "v1") +)] +// --- +pub(crate) mod versioned { + pub struct Foo { + bar: usize, + + #[versioned(added(since = "v1beta1"))] + baz: bool, + } + + pub struct Bar { + foo: usize, + + #[versioned(added(since = "v1beta1"))] + faz: bool, + } +} diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_field.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_for_version.rs.snap similarity index 96% rename from crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_field.rs.snap rename to crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_for_version.rs.snap index ee11bac68..1dd6c5a6a 100644 --- a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_field.rs.snap +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_for_version.rs.snap @@ -1,7 +1,7 @@ --- source: crates/stackable-versioned-macros/src/lib.rs expression: formatted -input_file: crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_field.rs +input_file: crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_for_version.rs --- #[automatically_derived] pub mod v1alpha1 { diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module.rs.snap new file mode 100644 index 000000000..33f8fdeee --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module.rs.snap @@ -0,0 +1,20 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module.rs +--- +#[automatically_derived] +pub(crate) mod v1alpha1 { + use super::*; + pub struct Foo { + pub bar: usize, + } +} +#[automatically_derived] +pub(crate) mod v1beta1 { + use super::*; + pub struct Foo { + pub bar: usize, + pub baz: bool, + } +} diff --git a/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module_for_version.rs.snap b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module_for_version.rs.snap new file mode 100644 index 000000000..8db99d15c --- /dev/null +++ b/crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@skip_from_module_for_version.rs.snap @@ -0,0 +1,57 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/fixtures/inputs/default/skip_from_module_for_version.rs +--- +#[automatically_derived] +pub(crate) mod v1alpha1 { + use super::*; + pub struct Foo { + pub bar: usize, + } + pub struct Bar { + pub foo: usize, + } +} +#[automatically_derived] +impl ::std::convert::From for v1beta1::Foo { + fn from(__sv_foo: v1alpha1::Foo) -> Self { + Self { + bar: __sv_foo.bar.into(), + baz: ::std::default::Default::default(), + } + } +} +#[automatically_derived] +impl ::std::convert::From for v1beta1::Bar { + fn from(__sv_bar: v1alpha1::Bar) -> Self { + Self { + foo: __sv_bar.foo.into(), + faz: ::std::default::Default::default(), + } + } +} +#[automatically_derived] +pub(crate) mod v1beta1 { + use super::*; + pub struct Foo { + pub bar: usize, + pub baz: bool, + } + pub struct Bar { + pub foo: usize, + pub faz: bool, + } +} +#[automatically_derived] +pub(crate) mod v1 { + use super::*; + pub struct Foo { + pub bar: usize, + pub baz: bool, + } + pub struct Bar { + pub foo: usize, + pub faz: bool, + } +} diff --git a/crates/stackable-versioned-macros/src/codegen/changes.rs b/crates/stackable-versioned-macros/src/codegen/changes.rs index df52a38ca..8aa0e8858 100644 --- a/crates/stackable-versioned-macros/src/codegen/changes.rs +++ b/crates/stackable-versioned-macros/src/codegen/changes.rs @@ -182,6 +182,8 @@ impl ChangesetExt for BTreeMap { ty, .. } => (ident, ty, *previously_deprecated), + // TODO (@NickLarsenNZ): Explain why it is unreachable, as it can be reached during testing. + // To reproduce, use an invalid version, eg: #[versioned(deprecated(since = "v99"))] _ => unreachable!(), }; diff --git a/crates/stackable-versioned-macros/src/codegen/module.rs b/crates/stackable-versioned-macros/src/codegen/module.rs index ab8889737..4ae950c1b 100644 --- a/crates/stackable-versioned-macros/src/codegen/module.rs +++ b/crates/stackable-versioned-macros/src/codegen/module.rs @@ -22,6 +22,7 @@ pub(crate) struct Module { versions: Vec, containers: Vec, preserve_module: bool, + skip_from: bool, ident: IdentString, vis: Visibility, } @@ -31,6 +32,7 @@ impl Module { pub(crate) fn new( ModuleInput { ident, vis, .. }: ModuleInput, preserve_module: bool, + skip_from: bool, versions: Vec, containers: Vec, ) -> Self { @@ -38,6 +40,7 @@ impl Module { ident: ident.into(), preserve_module, containers, + skip_from, versions, vis, } @@ -77,11 +80,13 @@ impl Module { for container in &self.containers { container_definitions.extend(container.generate_definition(version)); - from_impls.extend(container.generate_from_impl( - version, - versions.peek().copied(), - self.preserve_module, - )); + if !self.skip_from { + from_impls.extend(container.generate_from_impl( + version, + versions.peek().copied(), + self.preserve_module, + )); + } // Generate Kubernetes specific code which is placed outside of the container // definition. diff --git a/crates/stackable-versioned-macros/src/lib.rs b/crates/stackable-versioned-macros/src/lib.rs index 0ea0c3ea7..0725fcbe0 100644 --- a/crates/stackable-versioned-macros/src/lib.rs +++ b/crates/stackable-versioned-macros/src/lib.rs @@ -507,6 +507,12 @@ fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2:: let versions: Vec = (&module_attributes).into(); let preserve_modules = module_attributes.preserve_module.is_present(); + let skip_from = module_attributes + .common_root_arguments + .options + .skip + .as_ref() + .map_or(false, |opts| opts.from.is_present()); let module_span = item_mod.span(); let module_input = ModuleInput { @@ -541,7 +547,14 @@ fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2:: containers.push(container); } - Module::new(module_input, preserve_modules, versions, containers).generate_tokens() + Module::new( + module_input, + preserve_modules, + skip_from, + versions, + containers, + ) + .generate_tokens() } Item::Enum(item_enum) => { let container_attributes: StandaloneContainerAttributes = diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index dc5c4f29d..27e745d23 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -31,6 +31,7 @@ All notable changes to this project will be documented in this file. - Correctly emit Kubernetes code when macro is used on modules ([#912]). - Use `.into()` on all field conversions ([#925]). - Remove invalid type comparison on field conversion because the semantics are unknown ([#925]). +- Check whether to skip all from impls when versioning a module ([#926]). [#891]: https://github.com/stackabletech/operator-rs/pull/891 [#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. [#923]: https://github.com/stackabletech/operator-rs/pull/923 [#924]: https://github.com/stackabletech/operator-rs/pull/924 [#925]: https://github.com/stackabletech/operator-rs/pull/925 +[#926]: https://github.com/stackabletech/operator-rs/pull/926 ## [0.4.1] - 2024-10-23