Skip to content

Commit

Permalink
Merge pull request #418 from Brendonovich/fix-selects
Browse files Browse the repository at this point in the history
use generated select & include modules
  • Loading branch information
Brendonovich committed Dec 11, 2023
2 parents cef079c + a7c1101 commit 68a2f9a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
29 changes: 19 additions & 10 deletions crates/generator-macros/src/select_include/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use convert_case::Casing;
use prisma_client_rust_generator_shared::Arity;
use prisma_client_rust_generator_shared::{Arity, RelationArity};
use proc_macro2::TokenStream;
use quote::quote;
use syn::Field;
Expand Down Expand Up @@ -59,15 +59,10 @@ pub fn definitions(input: &Input) -> TokenStream {
return None;
}

let field = quote! {
#(#attrs)*
pub #ident: #dollar::#model_path::#ty
};

let field_module = field_in_selectables
let (field_type, field_module) = field_in_selectables
.zip(field_in_selection.and_then(|f| f.sub_selection.as_ref()))
.and_then(|(field_in_selectables, (variant, sub_selection))| {
let Arity::Relation(relation_model_path, _) = &field_in_selectables.arity else {
let Arity::Relation(relation_model_path, arity) = &field_in_selectables.arity else {
return None;
};

Expand All @@ -79,8 +74,22 @@ pub fn definitions(input: &Input) -> TokenStream {
}
};

Some(value)
});
let base = quote!(#ident::Data);

let typ = match arity {
RelationArity::One => base,
RelationArity::Many => quote!(Vec<#base>),
RelationArity::Optional => quote!(Option<#base>),
};

Some((typ, Some(value)))
})
.unwrap_or_else(|| (quote!(#dollar::#model_path::#ty), None));

let field = quote! {
#(#attrs)*
pub #ident: #field_type
};

Some((field, field_module))
})
Expand Down
27 changes: 15 additions & 12 deletions crates/generator-macros/src/select_include/selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,6 @@ pub fn selection_to_params(input: &Input, variant: Variant) -> Vec<TokenStream>
},
Arity::Relation(relation_model_path, relation_arity) => {
match (relation_arity, sub_selection) {
(RelationArity::One, None) => quote! {
#into(#variant_type_path::Fetch)
},
(RelationArity::One, Some((selection_variant, selection))) => quote! {
#into(
#variant_type_path::#selection_variant(
#dollar::#relation_model_path::#selection_variant! {
selection_to_params @ #selection
}.into_iter().collect()
)
)
},
(RelationArity::Many, None) => quote! {
#into(
#variant_type_path::Fetch(
Expand All @@ -115,6 +103,21 @@ pub fn selection_to_params(input: &Input, variant: Variant) -> Vec<TokenStream>
)
)
},
(RelationArity::One | RelationArity::Optional, None) => quote! {
#into(#variant_type_path::Fetch)
},
(
RelationArity::One | RelationArity::Optional,
Some((selection_variant, selection)),
) => quote! {
#into(
#variant_type_path::#selection_variant(
#dollar::#relation_model_path::#selection_variant! {
selection_to_params @ #selection
}.into_iter().collect()
)
)
},
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions crates/generator-shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod select_include;
pub enum RelationArity {
One,
Many,
Optional,
}

impl Parse for RelationArity {
Expand All @@ -28,7 +29,13 @@ impl Parse for RelationArity {
Ok(match ident.to_string().as_str() {
"One" => Self::One,
"Many" => Self::Many,
_ => return Err(syn::Error::new_spanned(ident, "expected `One` or `Many`")),
"Optional" => Self::Optional,
_ => {
return Err(syn::Error::new_spanned(
ident,
"expected `One`, `Many`, or `Optional`",
))
}
})
}
}
Expand All @@ -38,6 +45,7 @@ impl ToTokens for RelationArity {
tokens.extend(match self {
Self::One => quote!(One),
Self::Many => quote!(Many),
Self::Optional => quote!(Optional),
})
}
}
Expand Down Expand Up @@ -99,7 +107,8 @@ impl FieldTuple {

let relation_arity = match &field.ast_field().arity {
FieldArity::List => RelationArity::Many,
_ => RelationArity::One,
FieldArity::Optional => RelationArity::Optional,
FieldArity::Required => RelationArity::One,
};

Arity::Relation(
Expand Down

1 comment on commit 68a2f9a

@vercel
Copy link

@vercel vercel bot commented on 68a2f9a Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.