Move to separate DeriveActiveModel, cargo fmt

This commit is contained in:
Billy Chan 2021-05-25 18:27:44 +08:00
parent bbe80fa873
commit 7aaeef834c
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
18 changed files with 110 additions and 66 deletions

View File

@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
#[table = "cake"] #[table = "cake"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,

View File

@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
#[table = "cake_filling"] #[table = "cake_filling"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub cake_id: i32, pub cake_id: i32,
pub filling_id: i32, pub filling_id: i32,

View File

@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
#[table = "filling"] #[table = "filling"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,

View File

@ -4,7 +4,7 @@ use sea_orm::entity::prelude::*;
#[table = "fruit"] #[table = "fruit"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,

View File

@ -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<TokenStream> {
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<Ident> = fields
.clone()
.into_iter()
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string()))
.collect();
let name: Vec<Ident> = fields
.clone()
.into_iter()
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case()))
.collect();
let ty: Vec<Type> = 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<sea_orm::Value> {
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),*
}
}
}
))
}

View File

@ -1,9 +1,11 @@
mod active_model;
mod column; mod column;
mod entity; mod entity;
mod from_query_result; mod from_query_result;
mod model; mod model;
mod primary_key; mod primary_key;
pub use active_model::*;
pub use column::*; pub use column::*;
pub use entity::*; pub use entity::*;
pub use from_query_result::*; pub use from_query_result::*;

View File

@ -1,7 +1,7 @@
use heck::CamelCase; use heck::CamelCase;
use proc_macro2::{Ident, TokenStream}; use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned}; 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<TokenStream> { pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream> {
let fields = match data { let fields = match data {
@ -23,16 +23,10 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
.collect(); .collect();
let name: Vec<Ident> = fields let name: Vec<Ident> = fields
.clone()
.into_iter() .into_iter()
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case())) .map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case()))
.collect(); .collect();
let ty: Vec<Type> = fields
.into_iter()
.map(|Field { ty, .. }| ty)
.collect();
Ok(quote!( Ok(quote!(
impl sea_orm::ModelTrait for #ident { impl sea_orm::ModelTrait for #ident {
type Column = Column; type Column = Column;
@ -55,46 +49,5 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
}) })
} }
} }
#[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<sea_orm::Value> {
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),*
}
}
}
)) ))
} }

View File

@ -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)] #[proc_macro_derive(FromQueryResult)]
pub fn derive_from_query_result(input: TokenStream) -> TokenStream { pub fn derive_from_query_result(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input); let DeriveInput { ident, data, .. } = parse_macro_input!(input);

View File

@ -2,7 +2,7 @@ mod select;
pub use select::*; pub use select::*;
use crate::{Statement, DatabaseConnection, QueryResult, TypeErr}; use crate::{DatabaseConnection, QueryResult, Statement, TypeErr};
use async_trait::async_trait; use async_trait::async_trait;
use std::{error::Error, fmt}; use std::{error::Error, fmt};

View File

@ -1,5 +1,5 @@
use sea_query::{inject_parameters, MySqlQueryBuilder, Values}; use sea_query::{inject_parameters, MySqlQueryBuilder, Values};
use std::{fmt}; use std::fmt;
pub struct Statement { pub struct Statement {
pub sql: String, pub sql: String,

View File

@ -4,7 +4,7 @@ use sqlx::{mysql::MySqlRow, MySqlPool};
sea_query::sea_query_driver_mysql!(); sea_query::sea_query_driver_mysql!();
use sea_query_driver_mysql::bind_query; 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; pub struct SqlxMySqlConnector;

View File

@ -1,5 +1,5 @@
use std::fmt::Debug;
use crate::{ColumnTrait, ModelTrait, Value}; use crate::{ColumnTrait, ModelTrait, Value};
use std::fmt::Debug;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Action<V> { pub enum Action<V> {
@ -7,7 +7,10 @@ pub enum Action<V> {
Unset, Unset,
} }
impl<V> Action<V> where V: Into<Value> { impl<V> Action<V>
where
V: Into<Value>,
{
pub fn into_action_value(self) -> Action<Value> { pub fn into_action_value(self) -> Action<Value> {
match self { match self {
Self::Set(v) => Action::Set(v.into()), Self::Set(v) => Action::Set(v.into()),

View File

@ -1,5 +1,6 @@
pub use crate::{ pub use crate::{
Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, Action, ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel,
EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter,
QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, Iden, IdenStatic, ModelTrait, PrimaryKeyOfModel, PrimaryKeyTrait, QueryResult, Related,
RelationDef, RelationTrait, Select, TypeErr, Value,
}; };

View File

@ -13,7 +13,7 @@ pub use entity::*;
pub use query::*; pub use query::*;
pub use sea_orm_macros::{ pub use sea_orm_macros::{
DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult, DeriveActiveModel, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, FromQueryResult,
}; };
pub use sea_query; pub use sea_query;
pub use sea_query::Iden; pub use sea_query::Iden;

View File

@ -5,7 +5,7 @@ use crate::entity::prelude::*;
#[table = "cake"] #[table = "cake"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,

View File

@ -5,7 +5,7 @@ use crate::entity::prelude::*;
#[table = "cake_filling"] #[table = "cake_filling"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub cake_id: i32, pub cake_id: i32,
pub filling_id: i32, pub filling_id: i32,

View File

@ -5,7 +5,7 @@ use crate::entity::prelude::*;
#[table = "filling"] #[table = "filling"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,

View File

@ -5,7 +5,7 @@ use crate::entity::prelude::*;
#[table = "fruit"] #[table = "fruit"]
pub struct Entity; pub struct Entity;
#[derive(Clone, Debug, PartialEq, DeriveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,