From 7aaeef834ca914db06e20e40d5223d52ab150769 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 25 May 2021 18:27:44 +0800 Subject: [PATCH] Move to separate DeriveActiveModel, cargo fmt --- examples/sqlx-mysql/src/example_cake.rs | 2 +- .../sqlx-mysql/src/example_cake_filling.rs | 2 +- examples/sqlx-mysql/src/example_filling.rs | 2 +- examples/sqlx-mysql/src/example_fruit.rs | 2 +- sea-orm-macros/src/derives/active_model.rs | 75 +++++++++++++++++++ sea-orm-macros/src/derives/mod.rs | 2 + sea-orm-macros/src/derives/model.rs | 49 +----------- sea-orm-macros/src/lib.rs | 10 +++ src/connector/mod.rs | 2 +- src/database/statement.rs | 2 +- src/driver/sqlx_mysql.rs | 2 +- src/entity/active_model.rs | 9 ++- src/entity/prelude.rs | 7 +- src/lib.rs | 2 +- src/tests_cfg/cake.rs | 2 +- src/tests_cfg/cake_filling.rs | 2 +- src/tests_cfg/filling.rs | 2 +- src/tests_cfg/fruit.rs | 2 +- 18 files changed, 110 insertions(+), 66 deletions(-) create mode 100644 sea-orm-macros/src/derives/active_model.rs diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs index 755efd6a..e36d5c7d 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*; #[table = "cake"] pub struct Entity; -#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String, diff --git a/examples/sqlx-mysql/src/example_cake_filling.rs b/examples/sqlx-mysql/src/example_cake_filling.rs index 9812f57a..f7c4f7ee 100644 --- a/examples/sqlx-mysql/src/example_cake_filling.rs +++ b/examples/sqlx-mysql/src/example_cake_filling.rs @@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*; #[table = "cake_filling"] pub struct Entity; -#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub cake_id: i32, pub filling_id: i32, diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs index 45142363..1b872e98 100644 --- a/examples/sqlx-mysql/src/example_filling.rs +++ b/examples/sqlx-mysql/src/example_filling.rs @@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*; #[table = "filling"] pub struct Entity; -#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String, diff --git a/examples/sqlx-mysql/src/example_fruit.rs b/examples/sqlx-mysql/src/example_fruit.rs index d347f50e..8d0c188d 100644 --- a/examples/sqlx-mysql/src/example_fruit.rs +++ b/examples/sqlx-mysql/src/example_fruit.rs @@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*; #[table = "fruit"] pub struct Entity; -#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String, diff --git a/sea-orm-macros/src/derives/active_model.rs b/sea-orm-macros/src/derives/active_model.rs new file mode 100644 index 00000000..27953610 --- /dev/null +++ b/sea-orm-macros/src/derives/active_model.rs @@ -0,0 +1,75 @@ +use heck::CamelCase; +use proc_macro2::{Ident, TokenStream}; +use quote::{format_ident, quote, quote_spanned}; +use syn::{Data, DataStruct, Field, Fields, Type}; + +pub fn expend_derive_active_model(ident: Ident, data: Data) -> syn::Result { + let fields = match data { + Data::Struct(DataStruct { + fields: Fields::Named(named), + .. + }) => named.named, + _ => { + return Ok(quote_spanned! { + ident.span() => compile_error!("you can only derive DeriveModel on structs"); + }) + } + }; + + let field: Vec = fields + .clone() + .into_iter() + .map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string())) + .collect(); + + let name: Vec = fields + .clone() + .into_iter() + .map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case())) + .collect(); + + let ty: Vec = fields.into_iter().map(|Field { ty, .. }| ty).collect(); + + Ok(quote!( + #[derive(Clone, Debug)] + pub struct ActiveModel { + #(pub #field: sea_orm::Action<#ty>),* + } + + impl sea_orm::ActiveModelOf<#ident> for ActiveModel { + fn from_model(m: #ident) -> Self { + Self::from(m) + } + } + + impl From<#ident> for ActiveModel { + fn from(m: #ident) -> Self { + Self { + #(#field: sea_orm::Action::Set(m.#field)),* + } + } + } + + impl sea_orm::ActiveModelTrait for ActiveModel { + type Column = Column; + + fn get(&self, c: Self::Column) -> sea_orm::Action { + match c { + #(Self::Column::#name => self.#field.clone().into_action_value()),* + } + } + + fn set(&mut self, c: Self::Column, v: sea_orm::Value) { + match c { + #(Self::Column::#name => self.#field = sea_orm::Action::Set(v.unwrap())),* + } + } + + fn unset(&mut self, c: Self::Column) { + match c { + #(Self::Column::#name => self.#field = sea_orm::Action::Unset),* + } + } + } + )) +} diff --git a/sea-orm-macros/src/derives/mod.rs b/sea-orm-macros/src/derives/mod.rs index c94dba4e..380c3f9d 100644 --- a/sea-orm-macros/src/derives/mod.rs +++ b/sea-orm-macros/src/derives/mod.rs @@ -1,9 +1,11 @@ +mod active_model; mod column; mod entity; mod from_query_result; mod model; mod primary_key; +pub use active_model::*; pub use column::*; pub use entity::*; pub use from_query_result::*; diff --git a/sea-orm-macros/src/derives/model.rs b/sea-orm-macros/src/derives/model.rs index fbb3d22c..cb56b7ac 100644 --- a/sea-orm-macros/src/derives/model.rs +++ b/sea-orm-macros/src/derives/model.rs @@ -1,7 +1,7 @@ use heck::CamelCase; use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote, quote_spanned}; -use syn::{Data, DataStruct, Field, Fields, Type}; +use syn::{Data, DataStruct, Field, Fields}; pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result { let fields = match data { @@ -23,16 +23,10 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result .collect(); let name: Vec = fields - .clone() .into_iter() .map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case())) .collect(); - let ty: Vec = fields - .into_iter() - .map(|Field { ty, .. }| ty) - .collect(); - Ok(quote!( impl sea_orm::ModelTrait for #ident { type Column = Column; @@ -55,46 +49,5 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result }) } } - - #[derive(Clone, Debug)] - pub struct ActiveModel { - #(pub #field: sea_orm::Action<#ty>),* - } - - impl sea_orm::ActiveModelOf<#ident> for ActiveModel { - fn from_model(m: #ident) -> Self { - Self::from(m) - } - } - - impl From<#ident> for ActiveModel { - fn from(m: #ident) -> Self { - Self { - #(#field: sea_orm::Action::Set(m.#field)),* - } - } - } - - impl sea_orm::ActiveModelTrait for ActiveModel { - type Column = Column; - - fn get(&self, c: Self::Column) -> sea_orm::Action { - match c { - #(Self::Column::#name => self.#field.clone().into_action_value()),* - } - } - - fn set(&mut self, c: Self::Column, v: sea_orm::Value) { - match c { - #(Self::Column::#name => self.#field = sea_orm::Action::Set(v.unwrap())),* - } - } - - fn unset(&mut self, c: Self::Column) { - match c { - #(Self::Column::#name => self.#field = sea_orm::Action::Unset),* - } - } - } )) } diff --git a/sea-orm-macros/src/lib.rs b/sea-orm-macros/src/lib.rs index 2ffb7199..ce1a9867 100644 --- a/sea-orm-macros/src/lib.rs +++ b/sea-orm-macros/src/lib.rs @@ -45,6 +45,16 @@ pub fn derive_model(input: TokenStream) -> TokenStream { } } +#[proc_macro_derive(DeriveActiveModel)] +pub fn derive_active_model(input: TokenStream) -> TokenStream { + let DeriveInput { ident, data, .. } = parse_macro_input!(input); + + match derives::expend_derive_active_model(ident, data) { + Ok(ts) => ts.into(), + Err(e) => e.to_compile_error().into(), + } +} + #[proc_macro_derive(FromQueryResult)] pub fn derive_from_query_result(input: TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); diff --git a/src/connector/mod.rs b/src/connector/mod.rs index 5229fa8f..0f5c0e54 100644 --- a/src/connector/mod.rs +++ b/src/connector/mod.rs @@ -2,7 +2,7 @@ mod select; pub use select::*; -use crate::{Statement, DatabaseConnection, QueryResult, TypeErr}; +use crate::{DatabaseConnection, QueryResult, Statement, TypeErr}; use async_trait::async_trait; use std::{error::Error, fmt}; diff --git a/src/database/statement.rs b/src/database/statement.rs index 7954f69b..abe5066c 100644 --- a/src/database/statement.rs +++ b/src/database/statement.rs @@ -1,5 +1,5 @@ use sea_query::{inject_parameters, MySqlQueryBuilder, Values}; -use std::{fmt}; +use std::fmt; pub struct Statement { pub sql: String, diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index 310048db..e7e37e29 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -4,7 +4,7 @@ use sqlx::{mysql::MySqlRow, MySqlPool}; sea_query::sea_query_driver_mysql!(); use sea_query_driver_mysql::bind_query; -use crate::{connector::*, debug_print, query::*, Statement, DatabaseConnection}; +use crate::{connector::*, debug_print, query::*, DatabaseConnection, Statement}; pub struct SqlxMySqlConnector; diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 5f436568..369a185a 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -1,5 +1,5 @@ -use std::fmt::Debug; use crate::{ColumnTrait, ModelTrait, Value}; +use std::fmt::Debug; #[derive(Clone, Debug)] pub enum Action { @@ -7,7 +7,10 @@ pub enum Action { Unset, } -impl Action where V: Into { +impl Action +where + V: Into, +{ pub fn into_action_value(self) -> Action { match self { Self::Set(v) => Action::Set(v.into()), @@ -31,4 +34,4 @@ pub trait ActiveModelTrait: Clone + Debug { fn set(&mut self, c: Self::Column, v: Value); fn unset(&mut self, c: Self::Column); -} \ No newline at end of file +} diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 01e11c68..f131f167 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,5 +1,6 @@ pub use crate::{ - Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, - EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, - QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, + Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel, + DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, + Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, QueryResult, Related, + RelationDef, RelationTrait, Select, TypeErr, Value, }; diff --git a/src/lib.rs b/src/lib.rs index 4ab4f371..b44ee556 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ pub use entity::*; pub use query::*; pub use sea_orm_macros::{ - DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult, + DeriveActiveModel, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult, }; pub use sea_query; pub use sea_query::Iden; diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index d61c13ee..7c5b2b1d 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -5,7 +5,7 @@ use crate::entity::prelude::*; #[table = "cake"] pub struct Entity; -#[derive(Clone, Debug, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String, diff --git a/src/tests_cfg/cake_filling.rs b/src/tests_cfg/cake_filling.rs index b4c2f95e..93db9cd9 100644 --- a/src/tests_cfg/cake_filling.rs +++ b/src/tests_cfg/cake_filling.rs @@ -5,7 +5,7 @@ use crate::entity::prelude::*; #[table = "cake_filling"] pub struct Entity; -#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub cake_id: i32, pub filling_id: i32, diff --git a/src/tests_cfg/filling.rs b/src/tests_cfg/filling.rs index aecf23e0..673e62ee 100644 --- a/src/tests_cfg/filling.rs +++ b/src/tests_cfg/filling.rs @@ -5,7 +5,7 @@ use crate::entity::prelude::*; #[table = "filling"] pub struct Entity; -#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String, diff --git a/src/tests_cfg/fruit.rs b/src/tests_cfg/fruit.rs index c4067a28..b852d2f6 100644 --- a/src/tests_cfg/fruit.rs +++ b/src/tests_cfg/fruit.rs @@ -5,7 +5,7 @@ use crate::entity::prelude::*; #[table = "fruit"] pub struct Entity; -#[derive(Clone, Debug, PartialEq, DeriveModel)] +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String,