-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Add stackable-versioned
and k8s-version
crates for CRD versioning
#764
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
Changes from 2 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
31df5b1
chore: Add skeleton code
Techassi c93cdee
feat: Add basic container and attribute validation
Techassi 109e38f
Start field attribute validation
Techassi 40df3af
Move code generation into structs
Techassi cff3fe9
Adjust field actions which require generation in multiple versions
Techassi aaaedaa
Add basic support for added and always present fields
Techassi be20ad8
feat(k8s-version): Add Kubernetes version crate
Techassi f46d370
feat(k8s-version): Add support for FromMeta
Techassi 50f8076
chore(k8s-version): Add changelog
Techassi b09ad5f
test(k8s-version): Add more unit tests
Techassi 015ce8d
docs(k8s-version): Add README
Techassi 9ba774f
chore: Switch work machine
Techassi 03c32e7
Add basic support for renamed fields
Techassi 5448375
Enfore version sorting, add option to opt out
Techassi cc2190a
Add basic support for deprecated fields
Techassi 2eb23d7
Remove unused dependency
Techassi 2586e77
Fix k8s-version unit tests
Techassi 9f6c682
chore: Merge branch 'main' into feat/crd-versioning
Techassi 2506fc4
Add basic support for multiple field actions on one field
Techassi 957dd91
Restructure field validation code
Techassi cba9153
Generate chain of statuses
Techassi 2269675
Merge branch 'main' into feat/crd-versioning
Techassi f3515e2
Add Ord impl for Level and Version
Techassi e324d30
Add Part(Ord) unit tests for Level and Version
Techassi 3fa20f4
Add FromMeta unit test for Level
Techassi bd935d6
Generate code for multiple field actions
Techassi a9eeafd
Improve field attribute validation
Techassi 0916a93
Improve error handling, add doc comments
Techassi 727fbdf
k8s-version: Add validated Group
Techassi 92fafcf
k8s-version: Add library doc comments
Techassi 1952db8
k8s-version: Add doc comments for error enums
Techassi 6148a09
Add more (doc) comments
Techassi 3252c55
Add changelog for stackable-versioned
Techassi 2393ae0
Apply suggestions
Techassi 9e6fdef
Clean-up suggestions
Techassi 8a38f47
Rename API_VERSION_REGEX to API_GROUP_REGEX
Techassi 1aa7fcf
Use expect instead of unwrap for regular expressions
Techassi 36279ff
Include duplicate version name in error message
Techassi cbf7c8c
Bump json-patch to 1.4.0 because 1.3.0 was yanked
Techassi 0fdd492
Adjust level format
Techassi 005c203
Improve derive macro test
Techassi 4944a7f
Add how to use the ApiVersion::new() function
Techassi 3a2e052
Add doc comment for FieldAttributes::validate_versions
Techassi 232f883
Fix doc comment
Techassi ba3fc19
Fix doc tests
Techassi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "stackable-versioned" | ||
version = "0.1.0" | ||
authors.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
convert_case.workspace = true | ||
darling.workspace = true | ||
proc-macro2.workspace = true | ||
syn.workspace = true | ||
quote.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use darling::{ | ||
util::{Flag, SpannedValue}, | ||
Error, FromDeriveInput, FromMeta, | ||
}; | ||
|
||
#[derive(Debug, FromDeriveInput)] | ||
#[darling( | ||
attributes(versioned), | ||
supports(struct_named), | ||
forward_attrs(allow, doc, cfg, serde), | ||
and_then = ContainerAttributes::validate | ||
)] | ||
pub(crate) struct ContainerAttributes { | ||
#[darling(multiple)] | ||
pub(crate) version: SpannedValue<Vec<VersionAttributes>>, | ||
} | ||
|
||
impl ContainerAttributes { | ||
fn validate(mut self) -> darling::Result<Self> { | ||
if self.version.is_empty() { | ||
return Err(Error::custom( | ||
"attribute `#[versioned()]` must contain at least one `version`", | ||
) | ||
.with_span(&self.version.span())); | ||
} | ||
|
||
for version in &mut *self.version { | ||
if version.name.is_empty() { | ||
return Err(Error::custom("field `name` of `version` must not be empty") | ||
.with_span(&version.name.span())); | ||
} | ||
|
||
if !version | ||
.name | ||
.chars() | ||
.all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-') | ||
{ | ||
return Err(Error::custom( | ||
"field `name` of `version` must only contain alphanumeric ASCII characters (a-z, A-Z, 0-9, '.', '-')", | ||
) | ||
.with_span(&version.name.span())); | ||
} | ||
|
||
// TODO (@Techassi): Use Diagnostics API when stablizized to throw | ||
// a warning when the input mismatches the generated module name. | ||
// See https://github.com/rust-lang/rust/issues/54140 | ||
let module_name = version.name.to_lowercase(); | ||
if module_name != *version.name { | ||
println!("the generated module name differs from the provided version name which might cause confusion around what the code seems to suggest") | ||
} | ||
version.module_name = module_name | ||
} | ||
|
||
Ok(self) | ||
} | ||
} | ||
|
||
#[derive(Debug, FromMeta)] | ||
pub struct VersionAttributes { | ||
pub(crate) name: SpannedValue<String>, | ||
|
||
pub(crate) deprecated: Flag, | ||
|
||
#[darling(skip)] | ||
pub(crate) module_name: String, | ||
// #[darling(default = default_visibility)] | ||
// pub(crate) visibility: Visibility, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod container; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use darling::FromDeriveInput; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::{spanned::Spanned, Data, DataEnum, DataStruct, DeriveInput, Error, Ident, Result}; | ||
|
||
use crate::attrs::container::ContainerAttributes; | ||
|
||
pub(crate) mod version; | ||
|
||
pub(crate) fn expand(input: DeriveInput) -> Result<TokenStream> { | ||
// Extract container attributes | ||
let attributes = ContainerAttributes::from_derive_input(&input)?; | ||
|
||
// Validate container shape | ||
let expanded = match input.data { | ||
Data::Struct(data) => expand_struct(input.ident, data, attributes)?, | ||
Data::Enum(data) => expand_enum(input.ident, data, attributes)?, | ||
Data::Union(_) => { | ||
return Err(Error::new( | ||
input.span(), | ||
"derive macro `Versioned` only supports structs and enums", | ||
)) | ||
} | ||
}; | ||
|
||
Ok(quote! { | ||
#expanded | ||
}) | ||
} | ||
|
||
pub(crate) fn expand_struct( | ||
ident: Ident, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: DataStruct, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attributes: ContainerAttributes, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Result<TokenStream> { | ||
Ok(quote!()) | ||
} | ||
|
||
pub(crate) fn expand_enum( | ||
ident: Ident, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: DataEnum, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attributes: ContainerAttributes, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Result<TokenStream> { | ||
Ok(quote!()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use syn::{Field, Ident}; | ||
|
||
pub(crate) struct Version { | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
struct_ident: Ident, | ||
version: String, | ||
|
||
deprecated: Vec<Field>, | ||
renamed: Vec<Field>, | ||
added: Vec<Field>, | ||
|
||
fields: Vec<Field>, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use proc_macro::TokenStream; | ||
use syn::{DeriveInput, Error}; | ||
|
||
mod attrs; | ||
mod gen; | ||
|
||
#[proc_macro_derive(Versioned, attributes(versioned))] | ||
pub fn versioned_macro_derive(input: TokenStream) -> TokenStream { | ||
let input = syn::parse_macro_input!(input as DeriveInput); | ||
|
||
gen::expand(input) | ||
.unwrap_or_else(Error::into_compile_error) | ||
.into() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use stackable_versioned::Versioned; | ||
|
||
#[test] | ||
fn basic() { | ||
#[derive(Versioned)] | ||
#[versioned(version(name = "1.2.3", deprecated))] | ||
struct Foo { | ||
bar: usize, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.