Refactor EntityName

This commit is contained in:
Chris Tsang 2021-06-04 00:33:59 +08:00
parent 6a26b8cc17
commit 93daf55cda
6 changed files with 38 additions and 14 deletions

View File

@ -17,7 +17,7 @@ fn get_entity_attr(attrs: &[Attribute]) -> Option<syn::Lit> {
} }
pub fn expand_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<TokenStream> { pub fn expand_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<TokenStream> {
let entity_name = match get_entity_attr(&attrs) { let _entity_name = match get_entity_attr(&attrs) {
Some(lit) => quote! { #lit }, Some(lit) => quote! { #lit },
None => { None => {
let normalized = ident.to_string().to_snake_case(); let normalized = ident.to_string().to_snake_case();
@ -26,20 +26,18 @@ pub fn expand_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<
}; };
Ok(quote!( Ok(quote!(
impl sea_orm::EntityName for #ident {}
impl sea_orm::IdenStatic for #ident {
fn as_str(&self) -> &str {
#entity_name
}
}
impl sea_orm::Iden for #ident { impl sea_orm::Iden for #ident {
fn unquoted(&self, s: &mut dyn std::fmt::Write) { fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap(); write!(s, "{}", self.as_str()).unwrap();
} }
} }
impl sea_orm::IdenStatic for #ident {
fn as_str(&self) -> &str {
<Self as sea_orm::EntityName>::table_name(self)
}
}
impl EntityTrait for #ident { impl EntityTrait for #ident {
type Model = Model; type Model = Model;

View File

@ -11,7 +11,13 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static {
fn as_str(&self) -> &str; fn as_str(&self) -> &str;
} }
pub trait EntityName: IdenStatic + Default {} pub trait EntityName: IdenStatic + Default {
fn table_name(&self) -> &str;
fn module_name(&self) -> &str {
Self::table_name(self)
}
}
pub trait EntityTrait: EntityName { pub trait EntityTrait: EntityName {
type Model: ModelTrait<Entity = Self> + FromQueryResult; type Model: ModelTrait<Entity = Self> + FromQueryResult;

View File

@ -2,9 +2,14 @@ use crate as sea_orm;
use crate::entity::prelude::*; use crate::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"cake"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,

View File

@ -2,9 +2,14 @@ use crate as sea_orm;
use crate::entity::prelude::*; use crate::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake_filling"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"cake_filling"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub cake_id: i32, pub cake_id: i32,

View File

@ -2,9 +2,14 @@ use crate as sea_orm;
use crate::entity::prelude::*; use crate::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "filling"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"filling"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,

View File

@ -2,9 +2,14 @@ use crate as sea_orm;
use crate::entity::prelude::*; use crate::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "fruit"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"fruit"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,