ActiveModel derive macro
This commit is contained in:
parent
9c42ab2552
commit
bbe80fa873
@ -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};
|
||||
use syn::{Data, DataStruct, Field, Fields, Type};
|
||||
|
||||
pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream> {
|
||||
let fields = match data {
|
||||
@ -23,10 +23,16 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
|
||||
.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!(
|
||||
impl sea_orm::ModelTrait for #ident {
|
||||
type Column = Column;
|
||||
@ -49,5 +55,46 @@ 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),*
|
||||
}
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
||||
|
@ -11,13 +11,6 @@ pub struct Model {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
// can we generate this?
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ActiveModel {
|
||||
pub id: Action<i32>,
|
||||
pub name: Action<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
Id,
|
||||
@ -81,46 +74,3 @@ impl Model {
|
||||
Entity::find_related().belongs_to::<Entity>(self)
|
||||
}
|
||||
}
|
||||
|
||||
// can we generate this?
|
||||
impl ActiveModelOf<Model> for ActiveModel {
|
||||
fn from_model(m: Model) -> Self {
|
||||
Self::from(m)
|
||||
}
|
||||
}
|
||||
|
||||
// can we generate this?
|
||||
impl From<Model> for ActiveModel {
|
||||
fn from(m: Model) -> Self {
|
||||
Self {
|
||||
id: Action::Set(m.id),
|
||||
name: Action::Set(m.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// can we generate this?
|
||||
impl ActiveModelTrait for ActiveModel {
|
||||
type Column = Column;
|
||||
|
||||
fn get(&self, c: Self::Column) -> Action<Value> {
|
||||
match c {
|
||||
Column::Id => self.id.clone().into_action_value(),
|
||||
Column::Name => self.name.clone().into_action_value(),
|
||||
}
|
||||
}
|
||||
|
||||
fn set(&mut self, c: Self::Column, v: Value) {
|
||||
match c {
|
||||
Column::Id => self.id = Action::Set(v.unwrap()),
|
||||
Column::Name => self.name = Action::Set(v.unwrap()),
|
||||
}
|
||||
}
|
||||
|
||||
fn unset(&mut self, c: Self::Column) {
|
||||
match c {
|
||||
Column::Id => self.id = Action::Unset,
|
||||
Column::Name => self.name = Action::Unset,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user