Skip to content

fix(stackable-versioned): Handle fields added in later versions #1031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2025
Merged
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 @@ -7,6 +7,9 @@
)]
// ---
pub(crate) struct Foo {
#[versioned(added(since = "v1"))]
foo: String,

#[versioned(
changed(since = "v1beta1", from_name = "jjj", from_type = "u8"),
changed(since = "v1", from_type = "u16"),
Expand Down

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

4 changes: 4 additions & 0 deletions crates/stackable-versioned-macros/src/codegen/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ impl ChangesetExt for BTreeMap<Version, ItemStatus> {
ty,
..
} => (ident, ty, *previously_deprecated),
ItemStatus::NotPresent => {
self.insert(version.inner, ItemStatus::NotPresent);
continue;
}
// 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!(),
Expand Down
7 changes: 7 additions & 0 deletions crates/stackable-versioned-macros/src/codegen/item/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl VersionedField {
}
}

// TODO (@Techassi): This should probably return an optional token stream.
pub(crate) fn generate_for_from_impl(
&self,
version: &VersionDefinition,
Expand All @@ -155,6 +156,12 @@ impl VersionedField {
let change = changes.get_expect(&version.inner);

match (change, next_change) {
// If both this status and the next one is NotPresent, which means
// a field was introduced after a bunch of versions, we don't
// need to generate any code for the From impl.
(ItemStatus::NotPresent, ItemStatus::NotPresent) => {
quote! {}
}
(
_,
ItemStatus::Addition {
Expand Down
23 changes: 23 additions & 0 deletions crates/stackable-versioned-macros/tests/default/pass/added.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use stackable_versioned_macros::versioned;

fn main() {
#[versioned(
version(name = "v1alpha1"),
version(name = "v1alpha2"),
version(name = "v1beta1"),
version(name = "v1")
)]
struct Foo {
username: String,

#[versioned(added(since = "v1alpha2", default = default_first_name))]
first_name: String,

#[versioned(added(since = "v1beta1"))]
last_name: String,
}
}

fn default_first_name() -> String {
"foo".into()
}
1 change: 1 addition & 0 deletions crates/stackable-versioned-macros/tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod default {
fn default_macros() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/default/fail/*.rs");
t.pass("tests/default/pass/*.rs");
}

#[cfg(feature = "k8s")]
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 @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed

- Correctly handle fields added in later versions ([#1031]).

[#1031]: https://github.com/stackabletech/operator-rs/pull/1031

## [0.7.1] - 2025-04-02

### Fixed
Expand Down