cargo fmt
This commit is contained in:
parent
e203237f4d
commit
022bee0627
@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
use proc_macro2::{Ident, Span, TokenStream};
|
use proc_macro2::{Ident, Span, TokenStream};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{Attribute, Data, Fields, Lit, Meta, parse::Error, punctuated::Punctuated, spanned::Spanned, token::Comma};
|
use syn::{
|
||||||
|
parse::Error, punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Data, Fields,
|
||||||
|
Lit, Meta,
|
||||||
|
};
|
||||||
|
|
||||||
use convert_case::{Case, Casing};
|
use convert_case::{Case, Casing};
|
||||||
|
|
||||||
@ -20,8 +22,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
if let Some(ident) = nv.path.get_ident() {
|
if let Some(ident) = nv.path.get_ident() {
|
||||||
if ident == "table_name" {
|
if ident == "table_name" {
|
||||||
table_name = Some(nv.lit.clone());
|
table_name = Some(nv.lit.clone());
|
||||||
}
|
} else if ident == "schema_name" {
|
||||||
else if ident == "schema_name" {
|
|
||||||
let name = &nv.lit;
|
let name = &nv.lit;
|
||||||
schema_name = quote! { Some(#name) };
|
schema_name = quote! { Some(#name) };
|
||||||
}
|
}
|
||||||
@ -30,7 +31,9 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let entity_def = table_name.map(|table_name| quote! {
|
let entity_def = table_name
|
||||||
|
.map(|table_name| {
|
||||||
|
quote! {
|
||||||
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
|
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
|
||||||
pub struct Entity;
|
pub struct Entity;
|
||||||
|
|
||||||
@ -43,7 +46,9 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
#table_name
|
#table_name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).unwrap_or_default();
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
// generate Column enum and it's ColumnTrait impl
|
// generate Column enum and it's ColumnTrait impl
|
||||||
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
|
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
|
||||||
@ -55,7 +60,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
if let Fields::Named(fields) = item_struct.fields {
|
if let Fields::Named(fields) = item_struct.fields {
|
||||||
for field in fields.named {
|
for field in fields.named {
|
||||||
if let Some(ident) = &field.ident {
|
if let Some(ident) = &field.ident {
|
||||||
let field_name = Ident::new(&ident.to_string().to_case(Case::Pascal), Span::call_site());
|
let field_name =
|
||||||
|
Ident::new(&ident.to_string().to_case(Case::Pascal), Span::call_site());
|
||||||
columns_enum.push(quote! { #field_name });
|
columns_enum.push(quote! { #field_name });
|
||||||
|
|
||||||
let mut nullable = false;
|
let mut nullable = false;
|
||||||
@ -70,64 +76,74 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
if ident != "sea_orm" {
|
if ident != "sea_orm" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// single param
|
// single param
|
||||||
if let Ok(list) = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated) {
|
if let Ok(list) =
|
||||||
|
attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated)
|
||||||
|
{
|
||||||
for meta in list.iter() {
|
for meta in list.iter() {
|
||||||
match meta {
|
match meta {
|
||||||
Meta::NameValue(nv) => {
|
Meta::NameValue(nv) => {
|
||||||
if let Some(name) = nv.path.get_ident() {
|
if let Some(name) = nv.path.get_ident() {
|
||||||
if name == "column_type" {
|
if name == "column_type" {
|
||||||
if let Lit::Str(litstr) = &nv.lit {
|
if let Lit::Str(litstr) = &nv.lit {
|
||||||
let ty: TokenStream = syn::parse_str(&litstr.value())?;
|
let ty: TokenStream =
|
||||||
|
syn::parse_str(&litstr.value())?;
|
||||||
sql_type = Some(ty);
|
sql_type = Some(ty);
|
||||||
|
} else {
|
||||||
|
return Err(Error::new(
|
||||||
|
field.span(),
|
||||||
|
format!("Invalid column_type {:?}", nv.lit),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
else {
|
} else if name == "auto_increment" {
|
||||||
return Err(Error::new(field.span(), format!("Invalid column_type {:?}", nv.lit)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if name == "auto_increment" {
|
|
||||||
if let Lit::Str(litstr) = &nv.lit {
|
if let Lit::Str(litstr) = &nv.lit {
|
||||||
auto_increment = match litstr.value().as_str() {
|
auto_increment =
|
||||||
|
match litstr.value().as_str() {
|
||||||
"true" => true,
|
"true" => true,
|
||||||
"false" => false,
|
"false" => false,
|
||||||
_ => return Err(Error::new(field.span(), format!("Invalid auto_increment = {}", litstr.value()))),
|
_ => return Err(Error::new(
|
||||||
|
field.span(),
|
||||||
|
format!(
|
||||||
|
"Invalid auto_increment = {}",
|
||||||
|
litstr.value()
|
||||||
|
),
|
||||||
|
)),
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
return Err(Error::new(
|
||||||
|
field.span(),
|
||||||
|
format!(
|
||||||
|
"Invalid auto_increment = {:?}",
|
||||||
|
nv.lit
|
||||||
|
),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
else {
|
} else if name == "default_value" {
|
||||||
return Err(Error::new(field.span(), format!("Invalid auto_increment = {:?}", nv.lit)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if name == "default_value" {
|
|
||||||
default_value = Some(nv.lit.to_owned());
|
default_value = Some(nv.lit.to_owned());
|
||||||
}
|
} else if name == "default_expr" {
|
||||||
else if name == "default_expr" {
|
|
||||||
default_expr = Some(nv.lit.to_owned());
|
default_expr = Some(nv.lit.to_owned());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Meta::Path(p) => {
|
Meta::Path(p) => {
|
||||||
if let Some(name) = p.get_ident() {
|
if let Some(name) = p.get_ident() {
|
||||||
if name == "primary_key" {
|
if name == "primary_key" {
|
||||||
primary_keys.push(quote! { #field_name });
|
primary_keys.push(quote! { #field_name });
|
||||||
primary_key_types.push(field.ty.clone());
|
primary_key_types.push(field.ty.clone());
|
||||||
}
|
} else if name == "nullable" {
|
||||||
else if name == "nullable" {
|
|
||||||
nullable = true;
|
nullable = true;
|
||||||
}
|
} else if name == "indexed" {
|
||||||
else if name == "indexed" {
|
|
||||||
indexed = true;
|
indexed = true;
|
||||||
}
|
} else if name == "unique" {
|
||||||
else if name == "unique" {
|
|
||||||
unique = true;
|
unique = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => {},
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,13 +154,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
None => {
|
None => {
|
||||||
let field_type = &field.ty;
|
let field_type = &field.ty;
|
||||||
let temp = quote! { #field_type }
|
let temp = quote! { #field_type }
|
||||||
.to_string()//E.g.: "Option < String >"
|
.to_string() //E.g.: "Option < String >"
|
||||||
.replace(" ", "");
|
.replace(" ", "");
|
||||||
let temp = if temp.starts_with("Option<") {
|
let temp = if temp.starts_with("Option<") {
|
||||||
nullable = true;
|
nullable = true;
|
||||||
&temp[7..(temp.len() - 1)]
|
&temp[7..(temp.len() - 1)]
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
temp.as_str()
|
temp.as_str()
|
||||||
};
|
};
|
||||||
match temp {
|
match temp {
|
||||||
@ -162,7 +177,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
"NaiveDateTime" => quote! { DateTime },
|
"NaiveDateTime" => quote! { DateTime },
|
||||||
"Uuid" => quote! { Uuid },
|
"Uuid" => quote! { Uuid },
|
||||||
"Decimal" => quote! { BigInteger },
|
"Decimal" => quote! { BigInteger },
|
||||||
_ => return Err(Error::new(field.span(), format!("unrecognized type {}", temp))),
|
_ => {
|
||||||
|
return Err(Error::new(
|
||||||
|
field.span(),
|
||||||
|
format!("unrecognized type {}", temp),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -189,38 +209,39 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let primary_key = (!primary_keys.is_empty()).then(|| {
|
let primary_key = (!primary_keys.is_empty())
|
||||||
|
.then(|| {
|
||||||
let auto_increment = auto_increment && primary_keys.len() == 1;
|
let auto_increment = auto_increment && primary_keys.len() == 1;
|
||||||
let primary_key_types = if primary_key_types.len() == 1 {
|
let primary_key_types = if primary_key_types.len() == 1 {
|
||||||
let first = primary_key_types.first();
|
let first = primary_key_types.first();
|
||||||
quote! { #first }
|
quote! { #first }
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
quote! { (#primary_key_types) }
|
quote! { (#primary_key_types) }
|
||||||
};
|
};
|
||||||
quote! {
|
quote! {
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||||
pub enum PrimaryKey {
|
pub enum PrimaryKey {
|
||||||
#primary_keys
|
#primary_keys
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrimaryKeyTrait for PrimaryKey {
|
impl PrimaryKeyTrait for PrimaryKey {
|
||||||
type ValueType = #primary_key_types;
|
type ValueType = #primary_key_types;
|
||||||
|
|
||||||
fn auto_increment() -> bool {
|
fn auto_increment() -> bool {
|
||||||
#auto_increment
|
#auto_increment
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}).unwrap_or_default();
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
return Ok(quote! {
|
return Ok(quote! {
|
||||||
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]
|
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]
|
||||||
pub enum Column {
|
pub enum Column {
|
||||||
#columns_enum
|
#columns_enum
|
||||||
}
|
}
|
||||||
|
|
||||||
impl sea_orm::prelude::ColumnTrait for Column {
|
impl sea_orm::prelude::ColumnTrait for Column {
|
||||||
type EntityName = Entity;
|
type EntityName = Entity;
|
||||||
|
|
||||||
fn def(&self) -> sea_orm::prelude::ColumnDef {
|
fn def(&self) -> sea_orm::prelude::ColumnDef {
|
||||||
@ -228,10 +249,10 @@ impl sea_orm::prelude::ColumnTrait for Column {
|
|||||||
#columns_trait
|
#columns_trait
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#entity_def
|
#entity_def
|
||||||
|
|
||||||
#primary_key
|
#primary_key
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,16 @@ mod active_model;
|
|||||||
mod active_model_behavior;
|
mod active_model_behavior;
|
||||||
mod column;
|
mod column;
|
||||||
mod entity;
|
mod entity;
|
||||||
|
mod entity_model;
|
||||||
mod from_query_result;
|
mod from_query_result;
|
||||||
mod model;
|
mod model;
|
||||||
mod primary_key;
|
mod primary_key;
|
||||||
mod entity_model;
|
|
||||||
|
|
||||||
pub use active_model::*;
|
pub use active_model::*;
|
||||||
pub use active_model_behavior::*;
|
pub use active_model_behavior::*;
|
||||||
pub use column::*;
|
pub use column::*;
|
||||||
pub use entity::*;
|
pub use entity::*;
|
||||||
|
pub use entity_model::*;
|
||||||
pub use from_query_result::*;
|
pub use from_query_result::*;
|
||||||
pub use model::*;
|
pub use model::*;
|
||||||
pub use primary_key::*;
|
pub use primary_key::*;
|
||||||
pub use entity_model::*;
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use syn::{DeriveInput, parse_macro_input};
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
|
|
||||||
mod derives;
|
mod derives;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user