Skip to content

Commit

Permalink
Merge pull request #402 from onlycs/main
Browse files Browse the repository at this point in the history
Use impl Into for where and setparams, create functions
  • Loading branch information
Brendonovich authored Dec 13, 2024
2 parents 64bd7cf + 6de9c6c commit 1221408
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions crates/generator/src/composite_types/set_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub fn module_part(comp_type: CompositeTypeWalker) -> CompositeTypeModulePart {
(
field.name().to_string(),
quote! {
pub fn set(val: #field_type) -> SetParam {
SetParam::#variant_name(val)
pub fn set(val: impl Into<#field_type>) -> SetParam {
SetParam::#variant_name(val.into())
}
},
),
Expand Down
4 changes: 2 additions & 2 deletions crates/generator/src/composite_types/where_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub fn module_part(comp_type: CompositeTypeWalker) -> CompositeTypeModulePart {
(
field.name().to_string(),
quote! {
pub fn equals(val: #field_type) -> WhereParam {
WhereParam::#where_variant_name(val)
pub fn equals(val: impl Into<#field_type>) -> WhereParam {
WhereParam::#where_variant_name(val.into())
}
},
),
Expand Down
16 changes: 14 additions & 2 deletions crates/generator/src/models/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ pub fn create_fn(model: ModelWalker) -> Option<TokenStream> {
})
.unzip();

let types_impl_into = types.iter().map(|t| {
if t.to_string().contains("Param") {
quote!(#t)
} else {
quote!(impl Into<#t>)
}
});

Some(quote! {
pub fn create(self, #(#names: #types,)* mut _params: Vec<SetParam>) -> CreateQuery<'a> {
pub fn create(self, #(#names: #types_impl_into,)* mut _params: Vec<SetParam>) -> CreateQuery<'a> {
#(let #names = #names.into();)*

_params.extend([
#(#names::#push_wrapper(#names)),*
]);
Expand Down Expand Up @@ -60,7 +70,9 @@ pub fn create_unchecked_fn(model: ModelWalker) -> Option<TokenStream> {
.unzip();

Some(quote! {
pub fn create_unchecked(self, #(#names: #types,)* mut _params: Vec<UncheckedSetParam>) -> CreateUncheckedQuery<'a> {
pub fn create_unchecked(self, #(#names: impl Into<#types>,)* mut _params: Vec<UncheckedSetParam>) -> CreateUncheckedQuery<'a> {
#(let #names = #names.into();)*

_params.extend([
#(#names::set(#names)),*
]);
Expand Down
8 changes: 4 additions & 4 deletions crates/generator/src/models/set_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ fn field_set_params(
quote! {
pub struct Set(#field_type);

pub fn set<T: From<Set>>(create: #field_type) -> T {
Set(create).into()
pub fn set<T: From<Set>>(create: Impl Into<#field_type>) -> T {
Set(create.into()).into()
}

impl From<Set> for SetParam {
Expand Down Expand Up @@ -421,8 +421,8 @@ fn field_set_params(
}
}

pub fn set<T: From<Set>>(value: #field_type) -> T {
Set(value).into()
pub fn set<T: From<Set>>(value: impl Into<#field_type>) -> T {
Set(value.into()).into()
}

pub struct UpdateOperation(pub #param_enum_path);
Expand Down
15 changes: 8 additions & 7 deletions crates/generator/src/models/where_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub fn model_data(
let field_name_snake = snake_ident(field.name());

(
(quote!(#field_name_snake: #field_type), field_type),
(quote!(#field_name_snake: Impl Into<#field_type>), field_type),
(field.scalar_field_type().to_prisma_value(&field_name_snake, &FieldArity::Required), field_name_snake)
)
}).unzip();
Expand All @@ -308,14 +308,15 @@ pub fn model_data(
#field_names_joined,
#pcr::SerializedWhereValue::Object(vec![#((#variant_data_names::NAME.to_string(), #prisma_values)),*])
)
}, }
},
}
]);

let accessor_name = snake_ident(&variant_name_string);

Some(quote! {
pub fn #accessor_name<T: From<UniqueWhereParam>>(#(#field_defs),*) -> T {
UniqueWhereParam::#variant_name(#(#field_names_snake),*).into()
UniqueWhereParam::#variant_name(#(#field_names_snake.into()),*).into()
}
})
}
Expand Down Expand Up @@ -654,8 +655,8 @@ pub fn field_module(
arity.is_optional()
) {
(true, _, _) | (_, true, false) => quote! {
pub fn equals<T: From<Equals>>(value: #field_type) -> T {
Equals(value).into()
pub fn equals<T: From<Equals>>(value: impl Into<#field_type>) -> T {
Equals(value.into()).into()
}

impl From<Equals> for UniqueWhereParam {
Expand All @@ -670,8 +671,8 @@ pub fn field_module(
}
},
(false, false, _) => quote! {
pub fn equals<T: From<Equals>>(value: #field_type) -> T {
Equals(value).into()
pub fn equals<T: From<Equals>>(value: impl Into<#field_type>) -> T {
Equals(value.into()).into()
}
}
};
Expand Down

0 comments on commit 1221408

Please sign in to comment.