add struct table column gen macro
@@ -4,7 +4,7 @@ use proc_macro2::{Ident, Span};
use quote::quote;
use quote::ToTokens;
use syn;
-use syn::{AttributeArgs, FnArg, ItemFn, parse_macro_input, ReturnType};
+use syn::{AttributeArgs, Data, FnArg, ItemFn, parse_macro_input, ReturnType};
use crate::proc_macro::TokenStream;
@@ -53,19 +53,49 @@ fn find_id_type_ident(arg: &syn::Data) -> Ident {
fn impl_macro(ast: &syn::DeriveInput) -> TokenStream {
+ println!("............proc_macro_struct sql start............");
let name = &ast.ident;
let id_type = find_id_type_ident(&ast.data);
+ /// gen fields token
+ let mut fields = quote! { String::new() };
+ match &ast.data {
+ syn::Data::Struct(s) => {
+ let mut index = 0;
+ for field in &s.fields {
+ let field_name = &field.ident.to_token_stream().to_string();
+ if index == 0 {
+ fields = quote! {
+ #fields+#field_name
+ };
+ } else {
+ fields = quote! {
+ #fields+","+#field_name
+ };
+ }
+ index += 1;
+ }
+ }
+ _ => {}
+ }
+
let gen = quote! {
impl CRUDEnable for #name {
type IdType = #id_type;
+
fn table_name() -> String {
let mut name = stringify!(#name).to_string();
let names: Vec<&str> = name.split("::").collect();
name = names.get(names.len() - 1).unwrap().to_string();
return rbatis::utils::string_util::to_snake_name(&name);
}
+
+ fn table_fields() -> String{
+ #fields
+ }
}
};
+ println!("............gen rust code:\n {}", format!("{}", gen));
+ println!("............proc_macro_struct sql end............");
gen.into()
}
