-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
157 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Derives the [`Entity`](zino_core::orm::Entity) trait. | ||
|
||
# Attributes on struct fields | ||
|
||
- **`#[schema(primary_key)]`**: The `primary_key` annotation is used to | ||
mark a column as the primary key. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use super::parser; | ||
use convert_case::{Case, Casing}; | ||
use proc_macro2::TokenStream; | ||
use quote::{format_ident, quote}; | ||
use syn::DeriveInput; | ||
|
||
/// Parses the token stream for the `Entity` trait derivation. | ||
pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream { | ||
// Model name | ||
let name = input.ident; | ||
|
||
let mut primary_key_name = String::from("id"); | ||
let mut model_column_variants = Vec::new(); | ||
let mut model_column_mappings = Vec::new(); | ||
for field in parser::parse_struct_fields(input.data) { | ||
if let Some(ident) = field.ident { | ||
let name = ident.to_string(); | ||
let variant = format_ident!("{}", name.to_case(Case::Pascal)); | ||
'inner: for attr in field.attrs.iter() { | ||
let arguments = parser::parse_schema_attr(attr); | ||
for (key, _value) in arguments.into_iter() { | ||
match key.as_str() { | ||
"ignore" => break 'inner, | ||
"primary_key" => { | ||
primary_key_name.clone_from(&name); | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
model_column_variants.push(quote! { | ||
#variant, | ||
}); | ||
model_column_mappings.push(quote! { | ||
#variant => #name, | ||
}); | ||
} | ||
} | ||
|
||
let model_column_type = format_ident!("{}Column", name); | ||
let primary_key_variant = format_ident!("{}", primary_key_name.to_case(Case::Pascal)); | ||
quote! { | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] | ||
pub enum #model_column_type { | ||
#(#model_column_variants)* | ||
} | ||
|
||
impl AsRef<str> for #model_column_type { | ||
#[inline] | ||
fn as_ref(&self) -> &str { | ||
use #model_column_type::*; | ||
match self { | ||
#(#model_column_mappings)* | ||
} | ||
} | ||
} | ||
|
||
impl zino_core::orm::Entity for #name { | ||
type Column = #model_column_type; | ||
const PRIMARY_KEY: Self::Column = <#model_column_type>::#primary_key_variant; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters