Skip to content

Commit b095b42

Browse files
Techassisbernauer
andauthored
fix(stackable-versioned): Handle fields added in later versions (#1031)
* fix(stackable-versioned): Handle fields added in later versions * test(stackable-versioned): Update snapshot test * test(stackable-versioned): Add compile pass test * chore: Rename default function * chore: Apply suggestion Co-authored-by: Sebastian Bernauer <[email protected]> * chore: Add changelog entry --------- Co-authored-by: Sebastian Bernauer <[email protected]>
1 parent dddd78f commit b095b42

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

crates/stackable-versioned-macros/fixtures/inputs/default/basic_struct.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
)]
88
// ---
99
pub(crate) struct Foo {
10+
#[versioned(added(since = "v1"))]
11+
foo: String,
12+
1013
#[versioned(
1114
changed(since = "v1beta1", from_name = "jjj", from_type = "u8"),
1215
changed(since = "v1", from_type = "u16"),

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

Lines changed: 6 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ impl ChangesetExt for BTreeMap<Version, ItemStatus> {
172172
ty,
173173
..
174174
} => (ident, ty, *previously_deprecated),
175+
ItemStatus::NotPresent => {
176+
self.insert(version.inner, ItemStatus::NotPresent);
177+
continue;
178+
}
175179
// TODO (@NickLarsenNZ): Explain why it is unreachable, as it can be reached during testing.
176180
// To reproduce, use an invalid version, eg: #[versioned(deprecated(since = "v99"))]
177181
_ => unreachable!(),

crates/stackable-versioned-macros/src/codegen/item/field.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ impl VersionedField {
143143
}
144144
}
145145

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

157158
match (change, next_change) {
159+
// If both this status and the next one is NotPresent, which means
160+
// a field was introduced after a bunch of versions, we don't
161+
// need to generate any code for the From impl.
162+
(ItemStatus::NotPresent, ItemStatus::NotPresent) => {
163+
quote! {}
164+
}
158165
(
159166
_,
160167
ItemStatus::Addition {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use stackable_versioned_macros::versioned;
2+
3+
fn main() {
4+
#[versioned(
5+
version(name = "v1alpha1"),
6+
version(name = "v1alpha2"),
7+
version(name = "v1beta1"),
8+
version(name = "v1")
9+
)]
10+
struct Foo {
11+
username: String,
12+
13+
#[versioned(added(since = "v1alpha2", default = default_first_name))]
14+
first_name: String,
15+
16+
#[versioned(added(since = "v1beta1"))]
17+
last_name: String,
18+
}
19+
}
20+
21+
fn default_first_name() -> String {
22+
"foo".into()
23+
}

crates/stackable-versioned-macros/tests/trybuild.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod default {
2727
fn default_macros() {
2828
let t = trybuild::TestCases::new();
2929
t.compile_fail("tests/default/fail/*.rs");
30+
t.pass("tests/default/pass/*.rs");
3031
}
3132

3233
#[cfg(feature = "k8s")]

crates/stackable-versioned/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Fixed
8+
9+
- Correctly handle fields added in later versions ([#1031]).
10+
11+
[#1031]: https://github.com/stackabletech/operator-rs/pull/1031
12+
713
## [0.7.1] - 2025-04-02
814

915
### Fixed

0 commit comments

Comments
 (0)