Skip to content

Commit

Permalink
Add struct_name override for cel_validated
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Nov 3, 2024
1 parent 024cd08 commit d32bfe5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/crd_derive_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use kube::{
Api, ApiResource, DeleteParams, DynamicObject, GroupVersionKind, Patch, PatchParams, PostParams,
WatchEvent, WatchParams,
},
cel_validation,
runtime::wait::{await_condition, conditions},
Client, CustomResource, CustomResourceExt,
};
use kube::cel_validation;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand All @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize};
// - https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#defaulting
// - https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#defaulting-and-nullable

#[cel_validation]
#[cel_validation(struct_name = "FooSpecValidation")]
#[derive(CustomResource, Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone, JsonSchema)]
#[kube(
group = "clux.dev",
Expand Down
24 changes: 20 additions & 4 deletions kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by darling macros, out of our control
#![allow(clippy::manual_unwrap_or_default)]

use darling::{FromAttributes, FromDeriveInput, FromMeta};
use darling::{ast::NestedMeta, FromAttributes, FromDeriveInput, FromMeta};
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::ToTokens;
use syn::{parse::Parser, parse_quote, Attribute, Data, DeriveInput, Path, Visibility};
Expand Down Expand Up @@ -564,12 +564,27 @@ struct CELAttr {
reason: Option<String>,
}

pub(crate) fn cel_validation(_: TokenStream, input: TokenStream) -> TokenStream {
#[derive(FromMeta)]
struct CELSettings {
struct_name: Option<String>,
}

pub(crate) fn cel_validation(args: TokenStream, input: TokenStream) -> TokenStream {
let args = match NestedMeta::parse_meta_list(args) {
Err(err) => return err.to_compile_error(),
Ok(args) => args,
};

let mut ast: DeriveInput = match syn::parse2(input) {
Err(err) => return err.to_compile_error(),
Ok(di) => di,
};

let args = match CELSettings::from_list(&args) {
Err(err) => return err.write_errors(),
Ok(attrs) => attrs,
};

if !ast.attrs.iter().any(|attr| attr.path().is_ident("derive")) {
return syn::Error::new(
ast.ident.span(),
Expand All @@ -579,7 +594,7 @@ pub(crate) fn cel_validation(_: TokenStream, input: TokenStream) -> TokenStream
}

// Create a struct name for added validation rules, following the original struct name + "Validation"
let struct_name = ast.ident.to_string() + "Validation";
let struct_name = args.struct_name.unwrap_or(ast.ident.to_string() + "Validation");
let validation_struct = Ident::new(&struct_name, Span::call_site());

let mut validations: Vec<TokenStream> = vec![];
Expand Down Expand Up @@ -617,7 +632,8 @@ pub(crate) fn cel_validation(_: TokenStream, input: TokenStream) -> TokenStream
return syn::Error::new_spanned(
attr,
r#"Either message or message_expression should be specified at once"#,
).to_compile_error()
)
.to_compile_error();
}
let message = if let Some(message) = message {
quote! { "message": #message, }
Expand Down

0 comments on commit d32bfe5

Please sign in to comment.