-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for converting Map keys to case
- Loading branch information
Showing
9 changed files
with
79 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
Derives the [`ModelHooks`](zino_core::model::ModelHooks) trait. | ||
|
||
# Attributes on struct fields | ||
|
||
- **`#[schema(rename_all = "...")]`**: The `rename_all` attribute is used to | ||
rename all the fields according to the specific case when decoding the model as a `Map`. | ||
Supported values: `lowercase` | `UPPERCASE` | `PascalCase` | `camelCase` | `snake_case` | ||
| `SCREAMING_SNAKE_CASE` | `kebab-case` | `SCREAMING-KEBAB-CASE`. |
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 |
---|---|---|
@@ -1,16 +1,58 @@ | ||
use super::parser; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use quote::{format_ident, quote}; | ||
use syn::DeriveInput; | ||
|
||
/// Parses the token stream for the `ModelHooks` trait derivation. | ||
pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream { | ||
// Model name | ||
let name = input.ident; | ||
|
||
// Parsing struct attributes | ||
let mut model_hooks = Vec::new(); | ||
let mut field_case = None; | ||
for attr in input.attrs.iter() { | ||
for (key, value) in parser::parse_schema_attr(attr).into_iter() { | ||
if let Some(value) = value { | ||
match key.as_str() { | ||
"rename_all" => { | ||
field_case = match value.as_str() { | ||
"lowercase" => Some("Lower"), | ||
"UPPERCASE" => Some("Upper"), | ||
"PascalCase" => Some("Pascal"), | ||
"camelCase" => Some("Camel"), | ||
"snake_case" => Some("Snake"), | ||
"SCREAMING_SNAKE_CASE" => Some("Constant"), | ||
"kebab-case" => Some("Kebab"), | ||
"SCREAMING-KEBAB-CASE" => Some("Cobol"), | ||
_ => None, | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
} | ||
if let Some(case) = field_case { | ||
let case_variant = format_ident!("{}", case); | ||
model_hooks.push(quote! { | ||
#[inline] | ||
async fn after_decode(model: &mut zino_core::Map) -> Result<(), zino_core::error::Error> { | ||
use convert_case::Case; | ||
use zino_core::extension::JsonObjectExt; | ||
|
||
model.rename_keys(Case::#case_variant); | ||
Ok(()) | ||
} | ||
}); | ||
} | ||
|
||
quote! { | ||
impl zino_core::model::ModelHooks for #name { | ||
type Data = (); | ||
type Extension = (); | ||
|
||
#(#model_hooks)* | ||
} | ||
} | ||
} |
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