Skip to content

Commit 11c4092

Browse files
authored
fix(stackable-versioned): Emit correct enum based on kube kind argument (#920)
* fix(stackable-versioned): Emit correct enum based on kube kind argument * chore(stackable-versioned): Update changelog
1 parent 59cb9d5 commit 11c4092

File tree

6 files changed

+221
-8
lines changed

6 files changed

+221
-8
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1beta1"),
4+
version(name = "v1"),
5+
k8s(
6+
group = "stackable.tech",
7+
kind = "FooBar",
8+
singular = "foo",
9+
plural = "foos",
10+
namespaced,
11+
)
12+
)]
13+
// ---
14+
#[derive(
15+
Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, kube::CustomResource,
16+
)]
17+
pub struct FooSpec {
18+
#[versioned(
19+
added(since = "v1beta1"),
20+
changed(since = "v1", from_name = "bah", from_type = "u16")
21+
)]
22+
bar: usize,
23+
baz: bool,
24+
}

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__k8s_snapshots@renamed_kind.rs.snap

Lines changed: 177 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/container/enum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Container {
3737
.map_or(false, |s| s.from.is_present()),
3838
};
3939

40-
let idents: ContainerIdents = item_enum.ident.into();
40+
let idents = ContainerIdents::from(item_enum.ident, None);
4141

4242
let common = CommonContainerData {
4343
original_attributes: item_enum.attrs,
@@ -73,7 +73,7 @@ impl Container {
7373
.map_or(false, |s| s.from.is_present()),
7474
};
7575

76-
let idents: ContainerIdents = item_enum.ident.into();
76+
let idents = ContainerIdents::from(item_enum.ident, None);
7777

7878
let common = CommonContainerData {
7979
original_attributes: item_enum.attrs,

crates/stackable-versioned-macros/src/codegen/container/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::Deref;
22

33
use darling::{util::IdentString, Result};
4-
use proc_macro2::TokenStream;
4+
use proc_macro2::{Span, TokenStream};
55
use quote::{quote, ToTokens};
66
use syn::{parse_quote, Attribute, Ident, ItemEnum, ItemStruct, Path, Visibility};
77

@@ -237,12 +237,22 @@ pub(crate) struct ContainerIdents {
237237
pub(crate) from: IdentString,
238238
}
239239

240-
impl From<Ident> for ContainerIdents {
241-
fn from(ident: Ident) -> Self {
240+
impl ContainerIdents {
241+
pub(crate) fn from(ident: Ident, kubernetes_options: Option<&KubernetesOptions>) -> Self {
242+
let kubernetes = kubernetes_options.map_or_else(
243+
|| ident.as_cleaned_kubernetes_ident(),
244+
|options| {
245+
options.kind.as_ref().map_or_else(
246+
|| ident.as_cleaned_kubernetes_ident(),
247+
|kind| IdentString::from(Ident::new(kind, Span::call_site())),
248+
)
249+
},
250+
);
251+
242252
Self {
243-
kubernetes: ident.as_cleaned_kubernetes_ident(),
244253
from: ident.as_from_impl_ident(),
245254
original: ident.into(),
255+
kubernetes,
246256
}
247257
}
248258
}

crates/stackable-versioned-macros/src/codegen/container/struct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Container {
3232
}
3333

3434
let kubernetes_options = attributes.kubernetes_arguments.map(Into::into);
35-
let idents: ContainerIdents = item_struct.ident.into();
35+
let idents = ContainerIdents::from(item_struct.ident, kubernetes_options.as_ref());
3636

3737
// Validate K8s specific requirements
3838
// Ensure that the struct name includes the 'Spec' suffix.
@@ -78,7 +78,7 @@ impl Container {
7878
}
7979

8080
let kubernetes_options = attributes.kubernetes_arguments.map(Into::into);
81-
let idents: ContainerIdents = item_struct.ident.into();
81+
let idents = ContainerIdents::from(item_struct.ident, kubernetes_options.as_ref());
8282

8383
// Validate K8s specific requirements
8484
// Ensure that the struct name includes the 'Spec' suffix.

crates/stackable-versioned/CHANGELOG.md

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

2424
### Fixed
2525

26+
- Emit correct enum ident based on kube/k8s kind argument ([#920]).
2627
- Generate Kubernetes code independent of container order ([#913]).
2728
- Correctly emit Kubernetes code when macro is used on modules ([#912]).
2829

@@ -31,6 +32,7 @@ All notable changes to this project will be documented in this file.
3132
[#913]: https://github.com/stackabletech/operator-rs/pull/913
3233
[#914]: https://github.com/stackabletech/operator-rs/pull/914
3334
[#919]: https://github.com/stackabletech/operator-rs/pull/919
35+
[#920]: https://github.com/stackabletech/operator-rs/pull/920
3436

3537
## [0.4.1] - 2024-10-23
3638

0 commit comments

Comments
 (0)