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 {
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 {
#(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 {
#(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 {
#(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 {
#(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!(
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 {
#(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 {
#(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> {
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::PrimaryKeyOfModel<Model> for #ident {
fn into_column(self) -> <Model as ModelTrait>::Column {
impl sea_orm::PrimaryKeyToColumn<Entity> for #ident {
fn into_column(self) -> <Entity as EntityTrait>::Column {
match self {
#(Self::#variant => Column::#variant),*
}

View File

@ -1,4 +1,4 @@
use crate::{ColumnTrait, EntityTrait, Value};
use crate::{EntityTrait, Value};
use std::fmt::Debug;
#[derive(Clone, Debug, Default)]
@ -51,15 +51,15 @@ where
}
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>

View File

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

View File

@ -1,6 +1,6 @@
pub use crate::{
ActiveModelOf, ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel, DeriveColumn,
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,
};

View File

@ -1,10 +1,10 @@
use super::{IdenStatic, Iterable, ModelTrait};
use super::{IdenStatic, Iterable, EntityTrait};
pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
pub trait PrimaryKeyOfModel<M>
pub trait PrimaryKeyToColumn<E>
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::{
ColumnTrait, EntityTrait, Identity, IntoSimpleExpr, Iterable, ModelTrait, PrimaryKeyOfModel,
ColumnTrait, EntityTrait, Identity, IntoSimpleExpr, Iterable, ModelTrait, PrimaryKeyToColumn,
RelationDef,
};
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
where
E: EntityTrait,
E::PrimaryKey: PrimaryKeyOfModel<E::Model>,
E::PrimaryKey: PrimaryKeyToColumn<<E::Model as ModelTrait>::Entity>,
{
for key in E::PrimaryKey::iter() {
let col = key.into_column();

View File

@ -38,7 +38,7 @@ where
let mut columns = Vec::new();
let mut values = Vec::new();
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);
if columns_empty {
self.columns.push(av.is_set());