Skip to content

Commit

Permalink
implemented (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
leviska authored Apr 11, 2023
1 parent a26dfc8 commit 6ae5159
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
17 changes: 14 additions & 3 deletions sea-orm-macros/src/derives/from_query_result.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use syn::{ext::IdentExt, Data, DataStruct, Field, Fields};
use syn::{ext::IdentExt, parse_quote, Data, DataStruct, Field, Fields, GenericParam, Generics};

/// Method to derive a [QueryResult](sea_orm::QueryResult)
pub fn expand_derive_from_query_result(ident: Ident, data: Data) -> syn::Result<TokenStream> {
pub fn expand_derive_from_query_result(
ident: Ident,
data: Data,
mut generics: Generics,
) -> syn::Result<TokenStream> {
let fields = match data {
Data::Struct(DataStruct {
fields: Fields::Named(named),
Expand All @@ -29,9 +33,16 @@ pub fn expand_derive_from_query_result(ident: Ident, data: Data) -> syn::Result<
})
.collect();

for param in &mut generics.params {
if let GenericParam::Type(type_param) = param {
type_param.bounds.push(parse_quote!(sea_orm::TryGetable));
}
}
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

Ok(quote!(
#[automatically_derived]
impl sea_orm::FromQueryResult for #ident {
impl #impl_generics sea_orm::FromQueryResult for #ident #ty_generics #where_clause {
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> std::result::Result<Self, sea_orm::DbErr> {
Ok(Self {
#(#field: row.try_get(pre, #name)?),*
Expand Down
9 changes: 7 additions & 2 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,14 @@ pub fn derive_active_enum(input: TokenStream) -> TokenStream {
#[cfg(feature = "derive")]
#[proc_macro_derive(FromQueryResult)]
pub fn derive_from_query_result(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input);
let DeriveInput {
ident,
data,
generics,
..
} = parse_macro_input!(input);

match derives::expand_derive_from_query_result(ident, data) {
match derives::expand_derive_from_query_result(ident, data, generics) {
Ok(ts) => ts.into(),
Err(e) => e.to_compile_error().into(),
}
Expand Down
46 changes: 46 additions & 0 deletions tests/derive_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use sea_orm::{FromQueryResult, TryGetable};

#[derive(FromQueryResult)]
struct SimpleTest {
_foo: i32,
_bar: String,
}

#[derive(FromQueryResult)]
struct GenericTest<T> {
_foo: i32,
_bar: T,
}

#[derive(FromQueryResult)]
struct DoubleGenericTest<T, F> {
_foo: T,
_bar: F,
}

#[derive(FromQueryResult)]
struct BoundsGenericTest<T: Copy + Clone + 'static> {
_foo: T,
}

#[derive(FromQueryResult)]
struct WhereGenericTest<T>
where
T: Copy + Clone + 'static,
{
_foo: T,
}

#[derive(FromQueryResult)]
struct AlreadySpecifiedBoundsGenericTest<T: TryGetable> {
_foo: T,
}

#[derive(FromQueryResult)]
struct MixedGenericTest<T: Clone, F>
where
F: Copy + Clone + 'static,
{
_foo: T,
_bar: F,
}

0 comments on commit 6ae5159

Please sign in to comment.