Skip to content

Commit 1b8c400

Browse files
authored
fix(stackable-versioned): Emit right type for unchanged fields (#860)
* fix(stackable-versioned): Emit right type for unchanged fields Previously, the type used in fields marked as NoChange in a particular version was incorrect. It used the type of the definition container. The NoChange action now also tracks the field type and can thus generate the correct type. * chore: Remove superfluous 'added' action * chore: Update changelog
1 parent 3857d21 commit 1b8c400

File tree

5 files changed

+62
-25
lines changed

5 files changed

+62
-25
lines changed

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

+57-21
Original file line numberDiff line numberDiff line change
@@ -297,36 +297,69 @@ where
297297
ItemStatus::Addition { .. } => {
298298
chain.insert(version.inner, ItemStatus::NotPresent)
299299
}
300-
ItemStatus::Change { from_ident, .. } => {
301-
chain.insert(version.inner, ItemStatus::NoChange(from_ident.clone()))
302-
}
303-
ItemStatus::Deprecation { previous_ident, .. } => chain
304-
.insert(version.inner, ItemStatus::NoChange(previous_ident.clone())),
305-
ItemStatus::NoChange(ident) => {
306-
chain.insert(version.inner, ItemStatus::NoChange(ident.clone()))
307-
}
300+
ItemStatus::Change {
301+
from_ident,
302+
from_type,
303+
..
304+
} => chain.insert(
305+
version.inner,
306+
ItemStatus::NoChange {
307+
ident: from_ident.clone(),
308+
ty: from_type.clone(),
309+
},
310+
),
311+
ItemStatus::Deprecation { previous_ident, .. } => chain.insert(
312+
version.inner,
313+
ItemStatus::NoChange {
314+
ident: previous_ident.clone(),
315+
ty: self.inner.ty(),
316+
},
317+
),
318+
ItemStatus::NoChange { ident, ty } => chain.insert(
319+
version.inner,
320+
ItemStatus::NoChange {
321+
ident: ident.clone(),
322+
ty: ty.clone(),
323+
},
324+
),
308325
ItemStatus::NotPresent => unreachable!(),
309326
},
310327
(Some(status), None) => {
311-
let ident = match status {
312-
ItemStatus::Addition { ident, .. } => ident,
313-
ItemStatus::Change { to_ident, .. } => to_ident,
314-
ItemStatus::Deprecation { ident, .. } => ident,
315-
ItemStatus::NoChange(ident) => ident,
328+
let (ident, ty) = match status {
329+
ItemStatus::Addition { ident, ty, .. } => (ident, ty),
330+
ItemStatus::Change {
331+
to_ident, to_type, ..
332+
} => (to_ident, to_type),
333+
ItemStatus::Deprecation { ident, .. } => (ident, &self.inner.ty()),
334+
ItemStatus::NoChange { ident, ty } => (ident, ty),
316335
ItemStatus::NotPresent => unreachable!(),
317336
};
318337

319-
chain.insert(version.inner, ItemStatus::NoChange(ident.clone()))
338+
chain.insert(
339+
version.inner,
340+
ItemStatus::NoChange {
341+
ident: ident.clone(),
342+
ty: ty.clone(),
343+
},
344+
)
320345
}
321346
(Some(status), Some(_)) => {
322-
let ident = match status {
323-
ItemStatus::Addition { ident, .. } => ident,
324-
ItemStatus::Change { to_ident, .. } => to_ident,
325-
ItemStatus::NoChange(ident) => ident,
347+
let (ident, ty) = match status {
348+
ItemStatus::Addition { ident, ty, .. } => (ident, ty),
349+
ItemStatus::Change {
350+
to_ident, to_type, ..
351+
} => (to_ident, to_type),
352+
ItemStatus::NoChange { ident, ty, .. } => (ident, ty),
326353
_ => unreachable!(),
327354
};
328355

329-
chain.insert(version.inner, ItemStatus::NoChange(ident.clone()))
356+
chain.insert(
357+
version.inner,
358+
ItemStatus::NoChange {
359+
ident: ident.clone(),
360+
ty: ty.clone(),
361+
},
362+
)
330363
}
331364
_ => unreachable!(),
332365
};
@@ -365,7 +398,10 @@ pub(crate) enum ItemStatus {
365398
note: Option<String>,
366399
ident: Ident,
367400
},
368-
NoChange(Ident),
401+
NoChange {
402+
ident: Ident,
403+
ty: Type,
404+
},
369405
NotPresent,
370406
}
371407

@@ -375,7 +411,7 @@ impl ItemStatus {
375411
ItemStatus::Addition { ident, .. } => Some(ident),
376412
ItemStatus::Change { to_ident, .. } => Some(to_ident),
377413
ItemStatus::Deprecation { ident, .. } => Some(ident),
378-
ItemStatus::NoChange(ident) => Some(ident),
414+
ItemStatus::NoChange { ident, .. } => Some(ident),
379415
ItemStatus::NotPresent => None,
380416
}
381417
}

crates/stackable-versioned-macros/src/codegen/venum/variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl VersionedVariant {
142142
#ident,
143143
})
144144
}
145-
ItemStatus::NoChange(ident) => Some(quote! {
145+
ItemStatus::NoChange { ident, .. } => Some(quote! {
146146
#(#original_attributes)*
147147
#ident,
148148
}),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ impl VersionedField {
157157
})
158158
}
159159
ItemStatus::NotPresent => None,
160-
ItemStatus::NoChange(field_ident) => Some(quote! {
160+
ItemStatus::NoChange { ident, ty } => Some(quote! {
161161
#(#original_attributes)*
162-
pub #field_ident: #field_type,
162+
pub #ident: #ty,
163163
}),
164164
}
165165
}

crates/stackable-versioned-macros/tests/good/basic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use stackable_versioned_macros::versioned;
1313
)]
1414
pub(crate) struct Foo {
1515
#[versioned(
16-
added(since = "v1alpha1"),
1716
changed(since = "v1beta1", from_name = "jjj", from_type = "u8"),
1817
changed(since = "v1", from_type = "u16"),
1918
deprecated(since = "v2", note = "not empty")

crates/stackable-versioned/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ All notable changes to this project will be documented in this file.
2222

2323
- Report variant rename validation error at the correct span and trim underscores
2424
from variants not using PascalCase ([#842]).
25+
- Emit correct struct field types for fields with no changes (NoChange) ([#860]).
2526

2627
[#842]: https://github.com/stackabletech/operator-rs/pull/842
2728
[#844]: https://github.com/stackabletech/operator-rs/pull/844
2829
[#847]: https://github.com/stackabletech/operator-rs/pull/847
2930
[#850]: https://github.com/stackabletech/operator-rs/pull/850
3031
[#859]: https://github.com/stackabletech/operator-rs/pull/859
32+
[#860]: https://github.com/stackabletech/operator-rs/pull/860
3133

3234
## [0.1.1] - 2024-07-10
3335

0 commit comments

Comments
 (0)