Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Enum {
ver_ctx: VersionContext<'_>,
mod_gen_ctx: ModuleGenerationContext<'_>,
) -> Option<TokenStream> {
if ver_ctx.version.skip_from || self.common.options.skip_from {
if self.common.skip_from(ver_ctx, mod_gen_ctx) {
return None;
}

Expand Down
14 changes: 13 additions & 1 deletion crates/stackable-versioned-macros/src/codegen/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ impl Struct {
ver_ctx: VersionContext<'_>,
mod_gen_ctx: ModuleGenerationContext<'_>,
) -> Option<TokenStream> {
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;
}

Expand Down
4 changes: 3 additions & 1 deletion crates/stackable-versioned-macros/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub struct VersionDefinition {
/// Indicates that the container version is deprecated.
pub deprecated: Option<String>,

/// Indicates that the generation of `From<OLD> 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.
Expand Down
2 changes: 1 addition & 1 deletion crates/stackable-versioned-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<v1alpha1::Foo> for v1alpha2::Foo {
fn from(foo: v1alpha1::Foo) -> Self {
Self {
bar: 42,
baz: foo.baz.into(),
}
}
}

impl From<v1alpha2::Foo> for v1alpha1::Foo {
fn from(foo: v1alpha2::Foo) -> Self {
Self {
baz: foo.baz.into(),
}
}
}

impl From<v1alpha1::Baz> for v1alpha2::Baz {
fn from(baz: v1alpha1::Baz) -> Self {
match baz {
v1alpha1::Baz::PrevA => Self::A,
v1alpha1::Baz::B => Self::B,
}
}
}

impl From<v1alpha2::Baz> for v1alpha1::Baz {
fn from(baz: v1alpha2::Baz) -> Self {
match baz {
v1alpha2::Baz::A => Self::PrevA,
v1alpha2::Baz::B => Self::B,
}
}
}
Original file line number Diff line number Diff line change
@@ -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<v1alpha1::Foo> for v1alpha2::Foo {
fn from(foo: v1alpha1::Foo) -> Self {
Self { baz: foo.bar }
}
}

impl From<v1alpha2::Foo> for v1alpha1::Foo {
fn from(foo: v1alpha2::Foo) -> Self {
Self { bar: foo.baz }
}
}

impl From<v1alpha1::Mode> for v1alpha2::Mode {
fn from(mode: v1alpha1::Mode) -> Self {
match mode {
v1alpha1::Mode::PrevA => Self::A,
v1alpha1::Mode::B => Self::B,
}
}
}

impl From<v1alpha2::Mode> for v1alpha1::Mode {
fn from(mode: v1alpha2::Mode) -> Self {
match mode {
v1alpha2::Mode::A => Self::PrevA,
v1alpha2::Mode::B => Self::B,
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/stackable-versioned-macros/tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ mod inputs {
// mod scale;
// mod selectable;
// mod shortnames;
// mod skip_from_module;
// mod skip_from_version;
// mod submodule;
}

Expand Down
6 changes: 6 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading