From d32bfe5019fcf45c7eda6156ce755bad8d7555b0 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Sun, 3 Nov 2024 13:14:33 +0100 Subject: [PATCH] Add struct_name override for cel_validated Signed-off-by: Danil-Grigorev --- examples/crd_derive_schema.rs | 4 ++-- kube-derive/src/custom_resource.rs | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/crd_derive_schema.rs b/examples/crd_derive_schema.rs index 186a2c7b9..ebaa53ffa 100644 --- a/examples/crd_derive_schema.rs +++ b/examples/crd_derive_schema.rs @@ -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}; @@ -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", diff --git a/kube-derive/src/custom_resource.rs b/kube-derive/src/custom_resource.rs index 238206302..c94ce95c9 100644 --- a/kube-derive/src/custom_resource.rs +++ b/kube-derive/src/custom_resource.rs @@ -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}; @@ -564,12 +564,27 @@ struct CELAttr { reason: Option, } -pub(crate) fn cel_validation(_: TokenStream, input: TokenStream) -> TokenStream { +#[derive(FromMeta)] +struct CELSettings { + struct_name: Option, +} + +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(), @@ -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 = vec![]; @@ -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, }