diff --git a/crates/stackable-versioned-macros/src/codegen/container/enum.rs b/crates/stackable-versioned-macros/src/codegen/container/enum.rs index 0b254cde4..f8b4ecc88 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/enum.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/enum.rs @@ -143,7 +143,7 @@ impl Enum { ver_ctx: VersionContext<'_>, mod_gen_ctx: ModuleGenerationContext<'_>, ) -> Option { - if ver_ctx.version.skip_from || self.common.options.skip_from { + if self.common.skip_from(ver_ctx, mod_gen_ctx) { return None; } diff --git a/crates/stackable-versioned-macros/src/codegen/container/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/mod.rs index b7ef4327c..f64b1f2cf 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/mod.rs @@ -9,7 +9,7 @@ use syn::{Attribute, Ident}; use crate::{ attrs::container::StructCrdArguments, codegen::{ - VersionDefinition, + VersionContext, VersionDefinition, container::{r#enum::Enum, r#struct::Struct}, module::ModuleGenerationContext, }, @@ -32,6 +32,18 @@ pub struct CommonContainerData { pub idents: ContainerIdents, } +impl CommonContainerData { + /// Returns whether the `From` impls between `ver_ctx.version` and the next version should be + /// skipped, based on the module-, version- and container-level `skip(from)` flags. + pub fn skip_from( + &self, + ver_ctx: VersionContext<'_>, + mod_gen_ctx: ModuleGenerationContext<'_>, + ) -> bool { + mod_gen_ctx.skip.from.is_present() || ver_ctx.version.skip_from || self.options.skip_from + } +} + /// Supported types of containers, structs and enums. /// /// Abstracting away with kind of container is generated makes it possible to create a list of diff --git a/crates/stackable-versioned-macros/src/codegen/container/struct/mod.rs b/crates/stackable-versioned-macros/src/codegen/container/struct/mod.rs index f10fe243d..142cda893 100644 --- a/crates/stackable-versioned-macros/src/codegen/container/struct/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/container/struct/mod.rs @@ -465,7 +465,7 @@ impl Struct { ver_ctx: VersionContext<'_>, mod_gen_ctx: ModuleGenerationContext<'_>, ) -> Option { - if mod_gen_ctx.skip.from.is_present() || self.common.options.skip_from { + if self.common.skip_from(ver_ctx, mod_gen_ctx) { return None; } diff --git a/crates/stackable-versioned-macros/src/codegen/mod.rs b/crates/stackable-versioned-macros/src/codegen/mod.rs index 1e1117876..410c8b254 100644 --- a/crates/stackable-versioned-macros/src/codegen/mod.rs +++ b/crates/stackable-versioned-macros/src/codegen/mod.rs @@ -16,7 +16,9 @@ pub struct VersionDefinition { /// Indicates that the container version is deprecated. pub deprecated: Option, - /// Indicates that the generation of `From for NEW` should be skipped. + /// Indicates that the generation of the `From` impls (and `TrackingFrom` impls if conversion + /// tracking is enabled) between this and the next version should be skipped. This affects both + /// directions (upgrade and downgrade) of all containers in the module. pub skip_from: bool, /// A validated Kubernetes API version. diff --git a/crates/stackable-versioned-macros/src/lib.rs b/crates/stackable-versioned-macros/src/lib.rs index 358f63b03..500c0c8e0 100644 --- a/crates/stackable-versioned-macros/src/lib.rs +++ b/crates/stackable-versioned-macros/src/lib.rs @@ -1223,7 +1223,7 @@ mod snapshots { #[test] fn pass() { - // TODO (@Techassi): Re-add skip tests + // TODO (@Techassi): Re-add skip tests for object_from, merged_crd and try_convert let _settings_guard = test_utils::set_snapshot_path().bind_to_scope(); glob!("../tests/inputs/pass", "*.rs", |path| { diff --git a/crates/stackable-versioned-macros/tests/inputs/pass/skip_from_module.rs b/crates/stackable-versioned-macros/tests/inputs/pass/skip_from_module.rs new file mode 100644 index 000000000..130719a44 --- /dev/null +++ b/crates/stackable-versioned-macros/tests/inputs/pass/skip_from_module.rs @@ -0,0 +1,56 @@ +use stackable_versioned::versioned; +// --- +#[versioned(version(name = "v1alpha1"), version(name = "v1alpha2"), skip(from))] +// --- +mod versioned { + struct Foo { + #[versioned(added(in = "v1alpha2"))] + bar: usize, + + baz: Baz, + } + + enum Baz { + #[versioned(changed(in = "v1alpha2", previous_name = "PrevA"))] + A, + B, + } +} +// --- +fn main() {} + +// The From impls of all containers are skipped and implemented manually instead. +impl From for v1alpha2::Foo { + fn from(foo: v1alpha1::Foo) -> Self { + Self { + bar: 42, + baz: foo.baz.into(), + } + } +} + +impl From for v1alpha1::Foo { + fn from(foo: v1alpha2::Foo) -> Self { + Self { + baz: foo.baz.into(), + } + } +} + +impl From for v1alpha2::Baz { + fn from(baz: v1alpha1::Baz) -> Self { + match baz { + v1alpha1::Baz::PrevA => Self::A, + v1alpha1::Baz::B => Self::B, + } + } +} + +impl From for v1alpha1::Baz { + fn from(baz: v1alpha2::Baz) -> Self { + match baz { + v1alpha2::Baz::A => Self::PrevA, + v1alpha2::Baz::B => Self::B, + } + } +} diff --git a/crates/stackable-versioned-macros/tests/inputs/pass/skip_from_version.rs b/crates/stackable-versioned-macros/tests/inputs/pass/skip_from_version.rs new file mode 100644 index 000000000..4eb1bc78b --- /dev/null +++ b/crates/stackable-versioned-macros/tests/inputs/pass/skip_from_version.rs @@ -0,0 +1,60 @@ +use stackable_versioned::versioned; +// --- +#[versioned( + version(name = "v1alpha1", skip(from)), + version(name = "v1alpha2"), + version(name = "v1beta1") +)] +// --- +mod versioned { + struct Foo { + #[versioned(removed(in = "v1alpha2"))] + bar: usize, + + #[versioned(added(in = "v1alpha2"))] + baz: usize, + + #[versioned(added(in = "v1beta1"))] + qux: String, + } + + enum Mode { + #[versioned(changed(in = "v1alpha2", previous_name = "PrevA"))] + A, + B, + } +} +// --- +fn main() {} + +// The From impls of all containers between v1alpha1 and v1alpha2 are skipped and implemented +// manually instead. +impl From for v1alpha2::Foo { + fn from(foo: v1alpha1::Foo) -> Self { + Self { baz: foo.bar } + } +} + +impl From for v1alpha1::Foo { + fn from(foo: v1alpha2::Foo) -> Self { + Self { bar: foo.baz } + } +} + +impl From for v1alpha2::Mode { + fn from(mode: v1alpha1::Mode) -> Self { + match mode { + v1alpha1::Mode::PrevA => Self::A, + v1alpha1::Mode::B => Self::B, + } + } +} + +impl From for v1alpha1::Mode { + fn from(mode: v1alpha2::Mode) -> Self { + match mode { + v1alpha2::Mode::A => Self::PrevA, + v1alpha2::Mode::B => Self::B, + } + } +} diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@skip_from_module.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@skip_from_module.rs.snap new file mode 100644 index 000000000..0f75096ce --- /dev/null +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@skip_from_module.rs.snap @@ -0,0 +1,28 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/tests/inputs/pass/skip_from_module.rs +--- +#[automatically_derived] +mod v1alpha1 { + use super::*; + pub struct Foo { + pub baz: Baz, + } + pub enum Baz { + PrevA, + B, + } +} +#[automatically_derived] +mod v1alpha2 { + use super::*; + pub struct Foo { + pub bar: usize, + pub baz: Baz, + } + pub enum Baz { + A, + B, + } +} diff --git a/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@skip_from_version.rs.snap b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@skip_from_version.rs.snap new file mode 100644 index 000000000..af72ef275 --- /dev/null +++ b/crates/stackable-versioned-macros/tests/snapshots/stackable_versioned_macros__snapshots__pass@skip_from_version.rs.snap @@ -0,0 +1,72 @@ +--- +source: crates/stackable-versioned-macros/src/lib.rs +expression: formatted +input_file: crates/stackable-versioned-macros/tests/inputs/pass/skip_from_version.rs +--- +#[automatically_derived] +mod v1alpha1 { + use super::*; + pub struct Foo { + pub bar: usize, + } + pub enum Mode { + PrevA, + B, + } +} +#[automatically_derived] +mod v1alpha2 { + use super::*; + pub struct Foo { + pub baz: usize, + } + pub enum Mode { + A, + B, + } +} +#[automatically_derived] +impl ::core::convert::From for v1beta1::Foo { + fn from(__sv_foo: v1alpha2::Foo) -> Self { + Self { + baz: __sv_foo.baz.into(), + qux: ::std::default::Default::default(), + } + } +} +#[automatically_derived] +impl ::core::convert::From for v1alpha2::Foo { + fn from(__sv_foo: v1beta1::Foo) -> Self { + Self { baz: __sv_foo.baz.into() } + } +} +#[automatically_derived] +impl ::core::convert::From for v1beta1::Mode { + fn from(__sv_mode: v1alpha2::Mode) -> Self { + match __sv_mode { + v1alpha2::Mode::A => v1beta1::Mode::A, + v1alpha2::Mode::B => v1beta1::Mode::B, + } + } +} +#[automatically_derived] +impl ::core::convert::From for v1alpha2::Mode { + fn from(__sv_mode: v1beta1::Mode) -> Self { + match __sv_mode { + v1beta1::Mode::A => v1alpha2::Mode::A, + v1beta1::Mode::B => v1alpha2::Mode::B, + } + } +} +#[automatically_derived] +mod v1beta1 { + use super::*; + pub struct Foo { + pub baz: usize, + pub qux: String, + } + pub enum Mode { + A, + B, + } +} diff --git a/crates/stackable-versioned-macros/tests/trybuild.rs b/crates/stackable-versioned-macros/tests/trybuild.rs index 9621685bb..8bc02d02b 100644 --- a/crates/stackable-versioned-macros/tests/trybuild.rs +++ b/crates/stackable-versioned-macros/tests/trybuild.rs @@ -42,6 +42,8 @@ mod inputs { // mod scale; // mod selectable; // mod shortnames; + // mod skip_from_module; + // mod skip_from_version; // mod submodule; } diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 368282c1e..4b5690e64 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -24,10 +24,16 @@ All notable changes to this project will be documented in this file. strings ([#1326]). - The generated tracking status now respects the `schemars` and `serde` crate overrides. Previously, it failed to compile if these crates were not direct dependencies ([#1326]). +- BREAKING: Structs now respect the version-level `skip(from)` flag, e.g. `version(name = "v1alpha1", skip(from))`. + Previously, only enums respected it, so the `From` impls between the flagged and the next version + were still generated for structs, which conflicted with manual implementations. Likewise, enums now + respect the module-level `skip(from)` flag. Code which relied on the previously generated impls now + needs to implement them manually ([#1344]). [#1324]: https://github.com/stackabletech/operator-rs/pull/1324 [#1325]: https://github.com/stackabletech/operator-rs/pull/1325 [#1326]: https://github.com/stackabletech/operator-rs/pull/1326 +[#1344]: https://github.com/stackabletech/operator-rs/pull/1344 ## [0.14.0] - 2026-11-03