From 8137d218b2c4277b349891059f338a3e22905e49 Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 24 May 2024 09:58:44 +0200 Subject: [PATCH 1/3] Update changelog --- crates/stackable-versioned/CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index c026f6e0a..fa8660b6c 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -4,11 +4,13 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +- Adjust generated module and container names ([#CHANGEME]). - Change from derive macro to attribute macro to be able to generate code - _in place_ instead of _appending_ new code ([#CHANGEME]). + _in place_ instead of _appending_ new code ([#793]). - Improve action chain generation ([#784]). [#784](https://github.com/stackabletech/operator-rs/pull/784) +[#793](https://github.com/stackabletech/operator-rs/pull/793) [#CHANGEME](https://github.com/stackabletech/operator-rs/pull/CHANGEME) ## [0.1.0] - 2024-05-08 From 61e74a086861b1c7e8d8864d8dae1e84500310f0 Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 24 May 2024 11:12:15 +0200 Subject: [PATCH 2/3] Adjust module and container name generation --- Cargo.lock | 16 ++++++ Cargo.toml | 1 + crates/stackable-versioned/Cargo.toml | 1 + crates/stackable-versioned/src/gen/vstruct.rs | 55 ++++++++++++------- crates/stackable-versioned/tests/basic.rs | 8 +-- 5 files changed, 56 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42e557581..c9c01ad7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,6 +548,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -2914,6 +2923,7 @@ dependencies = [ name = "stackable-versioned" version = "0.1.0" dependencies = [ + "convert_case", "darling", "k8s-version", "proc-macro2", @@ -3404,6 +3414,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" + [[package]] name = "unicode-xid" version = "0.2.4" diff --git a/Cargo.toml b/Cargo.toml index 73bcf56e4..1444b8695 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ chrono = { version = "0.4.38", default-features = false } clap = { version = "4.5.4", features = ["derive", "cargo", "env"] } const_format = "0.2.32" const-oid = "0.9.6" +convert_case = "0.6.0" darling = "0.20.9" delegate = "0.12.0" derivative = "2.2.0" diff --git a/crates/stackable-versioned/Cargo.toml b/crates/stackable-versioned/Cargo.toml index d2ff81b73..3d40c7802 100644 --- a/crates/stackable-versioned/Cargo.toml +++ b/crates/stackable-versioned/Cargo.toml @@ -12,6 +12,7 @@ proc-macro = true [dependencies] k8s-version = { path = "../k8s-version", features = ["darling"] } +convert_case.workspace = true darling.workspace = true proc-macro2.workspace = true syn.workspace = true diff --git a/crates/stackable-versioned/src/gen/vstruct.rs b/crates/stackable-versioned/src/gen/vstruct.rs index 379d2ed3b..eaab573db 100644 --- a/crates/stackable-versioned/src/gen/vstruct.rs +++ b/crates/stackable-versioned/src/gen/vstruct.rs @@ -1,3 +1,4 @@ +use convert_case::{Case, Casing}; use darling::FromField; use proc_macro2::TokenStream; use quote::{format_ident, quote, ToTokens}; @@ -29,41 +30,53 @@ pub(crate) struct VersionedStruct { impl ToTokens for VersionedStruct { fn to_tokens(&self, tokens: &mut TokenStream) { let versions = self.versions.iter().peekable(); - let struct_name = &self.ident; + + let module_name = self.ident.to_string().to_case(Case::Snake); + let module_name = format_ident!("{module_name}"); + let alias_name = &self.ident; + + let mut struct_tokens = TokenStream::new(); for version in versions { - let mut fields = TokenStream::new(); + let mut field_tokens = TokenStream::new(); for field in &self.fields { - fields.extend(field.to_tokens_for_version(version)); + field_tokens.extend(field.to_tokens_for_version(version)); } - // TODO (@Techassi): Make the generation of the module optional to - // enable the attribute macro to be applied to a module which - // generates versioned versions of all contained containers. - let deprecated_attr = version.deprecated.then_some(quote! {#[deprecated]}); - let module_name = format_ident!("{version}", version = version.inner.to_string()); - tokens.extend(quote! { - #[automatically_derived] - #deprecated_attr - pub mod #module_name { + let struct_name = version.inner.to_string().to_case(Case::Pascal); + let struct_name = format_ident!("{struct_name}"); - pub struct #struct_name { - #fields - } + struct_tokens.extend(quote! { + #deprecated_attr + pub struct #struct_name { + #field_tokens } - }); + }) } + // Generate module with contents + tokens.extend(quote! { + #[automatically_derived] + pub mod #module_name { + #struct_tokens + } + }); + // Special handling for the last (and thus latest) version - let module_name = format_ident!( - "{version}", - version = self.versions.last().unwrap().inner.to_string() - ); + let struct_name = self + .versions + .last() + .unwrap() + .inner + .to_string() + .to_case(Case::Pascal); + let struct_name = format_ident!("{struct_name}"); + tokens.extend(quote! { - pub type #struct_name = #module_name::#struct_name; + pub type #alias_name = #module_name::#struct_name; }) } } diff --git a/crates/stackable-versioned/tests/basic.rs b/crates/stackable-versioned/tests/basic.rs index b3dd86db8..524d08fa9 100644 --- a/crates/stackable-versioned/tests/basic.rs +++ b/crates/stackable-versioned/tests/basic.rs @@ -24,12 +24,12 @@ struct Foo { #[test] fn basic() { - let _ = v1alpha1::Foo { jjj: 0, baz: false }; - let _ = v1beta1::Foo { bar: 0, baz: false }; - let _ = v1::Foo { bar: 0, baz: false }; + let _ = foo::V1Alpha1 { jjj: 0, baz: false }; + let _ = foo::V1Beta1 { bar: 0, baz: false }; + let _ = foo::V1 { bar: 0, baz: false }; #[allow(deprecated)] - let _ = v2::Foo { + let _ = foo::V2 { deprecated_bar: 0, baz: false, }; From e1012a57454da3d585cc696d4ce5781fe8c8c61f Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 24 May 2024 14:30:06 +0200 Subject: [PATCH 3/3] Apply suggestions Co-authored-by: Nick --- crates/stackable-versioned/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index fa8660b6c..320004c3a 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -4,14 +4,14 @@ All notable changes to this project will be documented in this file. ## [Unreleased] -- Adjust generated module and container names ([#CHANGEME]). +- Adjust generated module and container names ([#797]). - Change from derive macro to attribute macro to be able to generate code _in place_ instead of _appending_ new code ([#793]). - Improve action chain generation ([#784]). [#784](https://github.com/stackabletech/operator-rs/pull/784) [#793](https://github.com/stackabletech/operator-rs/pull/793) -[#CHANGEME](https://github.com/stackabletech/operator-rs/pull/CHANGEME) +[#797](https://github.com/stackabletech/operator-rs/pull/797) ## [0.1.0] - 2024-05-08