Refactor Trait bounds

This commit is contained in:
Chris Tsang 2021-06-02 19:09:00 +08:00
parent 2fc4520ee8
commit c65cb9687b
10 changed files with 37 additions and 37 deletions

View File

@ -47,29 +47,29 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
} }
impl sea_orm::ActiveModelTrait for ActiveModel { impl sea_orm::ActiveModelTrait for ActiveModel {
type Column = Column; type Entity = Entity;
fn take(&mut self, c: Self::Column) -> sea_orm::ActiveValue<sea_orm::Value> { fn take(&mut self, c: <Self::Entity as EntityTrait>::Column) -> sea_orm::ActiveValue<sea_orm::Value> {
match c { match c {
#(Self::Column::#name => std::mem::take(&mut self.#field).into_wrapped_value()),* #(<Self::Entity as EntityTrait>::Column::#name => std::mem::take(&mut self.#field).into_wrapped_value()),*
} }
} }
fn get(&self, c: Self::Column) -> sea_orm::ActiveValue<sea_orm::Value> { fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> sea_orm::ActiveValue<sea_orm::Value> {
match c { match c {
#(Self::Column::#name => self.#field.clone().into_wrapped_value()),* #(<Self::Entity as EntityTrait>::Column::#name => self.#field.clone().into_wrapped_value()),*
} }
} }
fn set(&mut self, c: Self::Column, v: sea_orm::Value) { fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: sea_orm::Value) {
match c { match c {
#(Self::Column::#name => self.#field = sea_orm::ActiveValue::set(v.unwrap())),* #(<Self::Entity as EntityTrait>::Column::#name => self.#field = sea_orm::ActiveValue::set(v.unwrap())),*
} }
} }
fn unset(&mut self, c: Self::Column) { fn unset(&mut self, c: <Self::Entity as EntityTrait>::Column) {
match c { match c {
#(Self::Column::#name => self.#field = sea_orm::ActiveValue::unset()),* #(<Self::Entity as EntityTrait>::Column::#name => self.#field = sea_orm::ActiveValue::unset()),*
} }
} }
} }

View File

@ -29,23 +29,23 @@ pub fn expand_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
Ok(quote!( Ok(quote!(
impl sea_orm::ModelTrait for #ident { impl sea_orm::ModelTrait for #ident {
type Column = Column; type Entity = Entity;
fn get(&self, c: Self::Column) -> sea_orm::Value { fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> sea_orm::Value {
match c { match c {
#(Self::Column::#name => self.#field.clone().into()),* #(<Self::Entity as EntityTrait>::Column::#name => self.#field.clone().into()),*
} }
} }
fn set(&mut self, c: Self::Column, v: sea_orm::Value) { fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: sea_orm::Value) {
match c { match c {
#(Self::Column::#name => self.#field = v.unwrap()),* #(<Self::Entity as EntityTrait>::Column::#name => self.#field = v.unwrap()),*
} }
} }
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::TypeErr> { fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::TypeErr> {
Ok(Self { Ok(Self {
#(#field: row.try_get(pre, Self::Column::#name.as_str().into())?),* #(#field: row.try_get(pre, <Self::Entity as EntityTrait>::Column::#name.as_str().into())?),*
}) })
} }
} }

View File

@ -47,8 +47,8 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
impl sea_orm::PrimaryKeyTrait for #ident {} impl sea_orm::PrimaryKeyTrait for #ident {}
impl sea_orm::PrimaryKeyOfModel<Model> for #ident { impl sea_orm::PrimaryKeyToColumn<Entity> for #ident {
fn into_column(self) -> <Model as ModelTrait>::Column { fn into_column(self) -> <Entity as EntityTrait>::Column {
match self { match self {
#(Self::#variant => Column::#variant),* #(Self::#variant => Column::#variant),*
} }

View File

@ -1,4 +1,4 @@
use crate::{ColumnTrait, EntityTrait, Value}; use crate::{EntityTrait, Value};
use std::fmt::Debug; use std::fmt::Debug;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -51,15 +51,15 @@ where
} }
pub trait ActiveModelTrait: Clone + Debug + Default { pub trait ActiveModelTrait: Clone + Debug + Default {
type Column: ColumnTrait; type Entity: EntityTrait;
fn take(&mut self, c: Self::Column) -> ActiveValue<Value>; fn take(&mut self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>;
fn get(&self, c: Self::Column) -> ActiveValue<Value>; fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>;
fn set(&mut self, c: Self::Column, v: Value); fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
fn unset(&mut self, c: Self::Column); fn unset(&mut self, c: <Self::Entity as EntityTrait>::Column);
} }
impl<V> ActiveValue<V> impl<V> ActiveValue<V>

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
ActiveModelOf, ActiveModelTrait, ColumnTrait, Insert, ModelTrait, OneOrManyActiveModel, ActiveModelOf, ActiveModelTrait, ColumnTrait, Insert, ModelTrait, OneOrManyActiveModel,
PrimaryKeyOfModel, PrimaryKeyTrait, QueryFilter, RelationBuilder, RelationTrait, RelationType, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder, RelationTrait, RelationType,
Select, Select,
}; };
use sea_query::{Iden, IntoValueTuple}; use sea_query::{Iden, IntoValueTuple};
@ -78,7 +78,7 @@ pub trait EntityTrait: EntityName {
fn find_by<V>(values: V) -> Select<Self> fn find_by<V>(values: V) -> Select<Self>
where where
V: IntoValueTuple, V: IntoValueTuple,
Self::PrimaryKey: PrimaryKeyOfModel<Self::Model>, Self::PrimaryKey: PrimaryKeyToColumn<Self>,
{ {
let mut select = Self::find(); let mut select = Self::find();
let mut keys = Self::PrimaryKey::iter(); let mut keys = Self::PrimaryKey::iter();

View File

@ -1,13 +1,13 @@
use crate::{ColumnTrait, QueryResult, TypeErr}; use crate::{EntityTrait, QueryResult, TypeErr};
pub use sea_query::Value; pub use sea_query::Value;
use std::fmt::Debug; use std::fmt::Debug;
pub trait ModelTrait: Clone + Debug { pub trait ModelTrait: Clone + Debug {
type Column: ColumnTrait; type Entity: EntityTrait;
fn get(&self, c: Self::Column) -> Value; fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> Value;
fn set(&mut self, c: Self::Column, v: Value); fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, TypeErr> fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, TypeErr>
where where

View File

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

View File

@ -1,10 +1,10 @@
use super::{IdenStatic, Iterable, ModelTrait}; use super::{IdenStatic, Iterable, EntityTrait};
pub trait PrimaryKeyTrait: IdenStatic + Iterable {} pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
pub trait PrimaryKeyOfModel<M> pub trait PrimaryKeyToColumn<E>
where where
M: ModelTrait, E: EntityTrait,
{ {
fn into_column(self) -> M::Column; fn into_column(self) -> E::Column;
} }

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
ColumnTrait, EntityTrait, Identity, IntoSimpleExpr, Iterable, ModelTrait, PrimaryKeyOfModel, ColumnTrait, EntityTrait, Identity, IntoSimpleExpr, Iterable, ModelTrait, PrimaryKeyToColumn,
RelationDef, RelationDef,
}; };
use sea_query::{Alias, Expr, IntoCondition, SelectExpr, SelectStatement, SimpleExpr}; use sea_query::{Alias, Expr, IntoCondition, SelectExpr, SelectStatement, SimpleExpr};
@ -226,7 +226,7 @@ pub trait QueryFilter: Sized {
fn belongs_to<E>(mut self, model: &E::Model) -> Self fn belongs_to<E>(mut self, model: &E::Model) -> Self
where where
E: EntityTrait, E: EntityTrait,
E::PrimaryKey: PrimaryKeyOfModel<E::Model>, E::PrimaryKey: PrimaryKeyToColumn<<E::Model as ModelTrait>::Entity>,
{ {
for key in E::PrimaryKey::iter() { for key in E::PrimaryKey::iter() {
let col = key.into_column(); let col = key.into_column();

View File

@ -38,7 +38,7 @@ where
let mut columns = Vec::new(); let mut columns = Vec::new();
let mut values = Vec::new(); let mut values = Vec::new();
let columns_empty = self.columns.is_empty(); let columns_empty = self.columns.is_empty();
for (idx, col) in A::Column::iter().enumerate() { for (idx, col) in <A::Entity as EntityTrait>::Column::iter().enumerate() {
let av = am.take(col); let av = am.take(col);
if columns_empty { if columns_empty {
self.columns.push(av.is_set()); self.columns.push(av.is_set());