feat: allow model with generics attribute (#400)

This commit is contained in:
Billy Chan 2022-01-14 01:18:13 +08:00 committed by GitHub
parent 0f5516b6bf
commit 2e038a7eae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 3 deletions

11
issues/400/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[workspace]
# A separate workspace
[package]
name = "sea-orm-issues-400"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]}

3
issues/400/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
mod model;
pub fn main() {}

79
issues/400/src/model.rs Normal file
View File

@ -0,0 +1,79 @@
use std::marker::PhantomData;
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: AccountId<String>,
pub name: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Clone, Debug, PartialEq)]
pub struct AccountId<T>(Uuid, PhantomData<T>);
impl<T> AccountId<T> {
pub fn new(id: Uuid) -> Self {
AccountId(id, PhantomData)
}
}
impl<T> From<AccountId<T>> for Uuid {
fn from(account_id: AccountId<T>) -> Self {
account_id.0
}
}
impl<T> sea_orm::TryFromU64 for AccountId<T> {
fn try_from_u64(_n: u64) -> Result<Self, sea_orm::DbErr> {
Err(sea_orm::DbErr::Exec(format!(
"{} cannot be converted from u64",
stringify!(AccountId<T>)
)))
}
}
impl<T> From<AccountId<T>> for sea_orm::Value {
fn from(source: AccountId<T>) -> Self {
sea_orm::Value::Uuid(Some(Box::new(source.into())))
}
}
impl<T> sea_orm::TryGetable for AccountId<T> {
fn try_get(
res: &sea_orm::QueryResult,
pre: &str,
col: &str,
) -> Result<Self, sea_orm::TryGetError> {
let val: Uuid = res.try_get(pre, col).map_err(sea_orm::TryGetError::DbErr)?;
Ok(AccountId::<T>::new(val))
}
}
impl<T> sea_orm::sea_query::Nullable for AccountId<T> {
fn null() -> sea_orm::Value {
sea_orm::Value::Uuid(None)
}
}
impl<T> sea_orm::sea_query::ValueType for AccountId<T> {
fn try_from(v: sea_orm::Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
match v {
sea_orm::Value::Uuid(Some(x)) => Ok(AccountId::<T>::new(*x)),
_ => Err(sea_orm::sea_query::ValueTypeErr),
}
}
fn type_name() -> String {
stringify!(AccountId).to_owned()
}
fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::Uuid
}
}

View File

@ -1,10 +1,10 @@
use crate::util::{escape_rust_keyword, trim_starting_raw_identifier}; use crate::util::{escape_rust_keyword, trim_starting_raw_identifier};
use heck::CamelCase; use heck::CamelCase;
use proc_macro2::{Ident, Span, TokenStream}; use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned}; use quote::{quote, quote_spanned};
use syn::{ use syn::{
parse::Error, punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Data, Fields, parse::Error, punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Data, Fields,
Lit, Meta, Lit, LitStr, Meta, Type,
}; };
/// Method to derive an Model /// Method to derive an Model
@ -256,7 +256,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
}; };
if col_type.is_empty() { if col_type.is_empty() {
let field_span = field.span(); let field_span = field.span();
let ty = format_ident!("{}", temp); let ty: Type = LitStr::new(temp, field_span).parse()?;
let def = quote_spanned! { field_span => { let def = quote_spanned! { field_span => {
std::convert::Into::<sea_orm::ColumnType>::into( std::convert::Into::<sea_orm::ColumnType>::into(
<#ty as sea_orm::sea_query::ValueType>::column_type() <#ty as sea_orm::sea_query::ValueType>::column_type()