Skip to content

feat(stackable-versioned): Add support for generics #969

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 5 commits into from
Feb 27, 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
@@ -0,0 +1,9 @@
#[versioned(version(name = "v1alpha1"), version(name = "v1"))]
// ---
pub enum Foo<T>
where
T: Default,
{
Bar(T),
Baz,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[versioned(
version(name = "v1alpha1"),
version(name = "v1"),
options(preserve_module)
)]
// ---
pub mod versioned {
struct Foo<T>
where
T: Default,
{
bar: T,
baz: u8,
}

enum Boom<T>
where
T: Default,
{
Big(T),
Shaq,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[versioned(version(name = "v1alpha1"), version(name = "v1"))]
// ---
pub struct Foo<T>
where
T: Default,
{
bar: T,
baz: u8,
}

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

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

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

22 changes: 18 additions & 4 deletions crates/stackable-versioned-macros/src/codegen/container/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Not;
use darling::{util::IdentString, FromAttributes, Result};
use proc_macro2::TokenStream;
use quote::quote;
use syn::ItemEnum;
use syn::{Generics, ItemEnum};

use crate::{
attrs::container::NestedContainerAttributes,
Expand Down Expand Up @@ -46,6 +46,7 @@ impl Container {
};

Ok(Self::Enum(Enum {
generics: item_enum.generics,
variants: versioned_variants,
common,
}))
Expand Down Expand Up @@ -82,6 +83,7 @@ impl Container {
};

Ok(Self::Enum(Enum {
generics: item_enum.generics,
variants: versioned_variants,
common,
}))
Expand All @@ -96,12 +98,16 @@ pub(crate) struct Enum {

/// Common container data which is shared between enums and structs.
pub(crate) common: CommonContainerData,

/// Generic types of the enum
pub generics: Generics,
}

// Common token generation
impl Enum {
/// Generates code for the enum definition.
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
let (_, type_generics, where_clause) = self.generics.split_for_impl();
let original_attributes = &self.common.original_attributes;
let ident = &self.common.idents.original;
let version_docs = &version.docs;
Expand All @@ -114,7 +120,7 @@ impl Enum {
quote! {
#(#[doc = #version_docs])*
#(#original_attributes)*
pub enum #ident {
pub enum #ident #type_generics #where_clause {
#variants
}
}
Expand All @@ -133,6 +139,12 @@ impl Enum {

match next_version {
Some(next_version) => {
// TODO (@Techassi): Support generic types which have been removed in newer versions,
// but need to exist for older versions How do we represent that? Because the
// defined struct always represents the latest version. I guess we could generally
// advise against using generic types, but if you have to, avoid removing it in
// later versions.
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let enum_ident = &self.common.idents.original;
let from_ident = &self.common.idents.from;

Expand All @@ -158,8 +170,10 @@ impl Enum {
Some(quote! {
#automatically_derived
#allow_attribute
impl ::std::convert::From<#version_ident::#enum_ident> for #next_version_ident::#enum_ident {
fn from(#from_ident: #version_ident::#enum_ident) -> Self {
impl #impl_generics ::std::convert::From<#version_ident::#enum_ident #type_generics> for #next_version_ident::#enum_ident #type_generics
#where_clause
{
fn from(#from_ident: #version_ident::#enum_ident #type_generics) -> Self {
match #from_ident {
#variants
}
Expand Down
26 changes: 20 additions & 6 deletions crates/stackable-versioned-macros/src/codegen/container/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Not;
use darling::{util::IdentString, Error, FromAttributes, Result};
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_quote, ItemStruct, Path, Visibility};
use syn::{parse_quote, Generics, ItemStruct, Path, Visibility};

use crate::{
attrs::container::NestedContainerAttributes,
Expand Down Expand Up @@ -58,6 +58,7 @@ impl Container {
};

Ok(Self::Struct(Struct {
generics: item_struct.generics,
fields: versioned_fields,
common,
}))
Expand Down Expand Up @@ -113,6 +114,7 @@ impl Container {
};

Ok(Self::Struct(Struct {
generics: item_struct.generics,
fields: versioned_fields,
common,
}))
Expand All @@ -123,16 +125,20 @@ impl Container {
pub(crate) struct Struct {
/// List of fields defined in the original struct. How, and if, an item
/// should generate code, is decided by the currently generated version.
pub(crate) fields: Vec<VersionedField>,
pub fields: Vec<VersionedField>,

/// Common container data which is shared between structs and enums.
pub(crate) common: CommonContainerData,
pub common: CommonContainerData,

/// Generic types of the struct
pub generics: Generics,
}

// Common token generation
impl Struct {
/// Generates code for the struct definition.
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
let (_, type_generics, where_clause) = self.generics.split_for_impl();
let original_attributes = &self.common.original_attributes;
let ident = &self.common.idents.original;
let version_docs = &version.docs;
Expand All @@ -149,7 +155,7 @@ impl Struct {
#(#[doc = #version_docs])*
#(#original_attributes)*
#kube_attribute
pub struct #ident {
pub struct #ident #type_generics #where_clause {
#fields
}
}
Expand All @@ -168,6 +174,12 @@ impl Struct {

match next_version {
Some(next_version) => {
// TODO (@Techassi): Support generic types which have been removed in newer versions,
// but need to exist for older versions How do we represent that? Because the
// defined struct always represents the latest version. I guess we could generally
// advise against using generic types, but if you have to, avoid removing it in
// later versions.
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let struct_ident = &self.common.idents.original;
let from_struct_ident = &self.common.idents.from;

Expand All @@ -194,8 +206,10 @@ impl Struct {
Some(quote! {
#automatically_derived
#allow_attribute
impl ::std::convert::From<#from_module_ident::#struct_ident> for #for_module_ident::#struct_ident {
fn from(#from_struct_ident: #from_module_ident::#struct_ident) -> Self {
impl #impl_generics ::std::convert::From<#from_module_ident::#struct_ident #type_generics> for #for_module_ident::#struct_ident #type_generics
#where_clause
{
fn from(#from_struct_ident: #from_module_ident::#struct_ident #type_generics) -> Self {
Self {
#fields
}
Expand Down
5 changes: 5 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- Add basic support for generic types in struct and enum definitions ([#969]).

### Changed

- BREAKING: Move `preserve_module` option into `options` to unify option interface ([#961]).

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

## [0.5.1] - 2025-02-14

Expand Down