From 434b6d19a5a47e0d3b4399d9937565723328643e Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 30 Jun 2021 00:27:49 -0600 Subject: juggle Id so FromSql works out --- tosin-macros/src/lib.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'tosin-macros/src') diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs index 15a0266..0da725e 100644 --- a/tosin-macros/src/lib.rs +++ b/tosin-macros/src/lib.rs @@ -77,8 +77,8 @@ fn to_field_spec(field: &syn::Field) -> FieldInfo { if field_type == &parse_type("Option") { FieldInfo { tosin_field: quote! { ::tosin::db::models::Field::IntField { name: stringify!(#field_name) } }, - diesel_column: quote! { #field_name -> Integer }, - diesel_type: quote! { ::tosin::db::sql_types::Integer }, + diesel_column: quote! { #field_name -> BigInt }, + diesel_type: quote! { ::tosin::db::sql_types::BigInt }, } } else if field_type == &parse_type("Id") { // TODO foreign key constraint @@ -132,7 +132,22 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream { } else { panic!("not on a struct"); }; - let real_types: Vec<_> = ast_data.fields.iter().map(|field| &field.ty).collect(); + let real_db_types: Vec<_> = ast_data + .fields + .iter() + .map(|field| { + if field + .ident + .as_ref() + .map_or(false, |name| name.to_string() == "id") + && field.ty == syn::parse_str("Option").unwrap() + { + syn::parse_str("Id").unwrap() + } else { + field.ty.clone() + } + }) + .collect(); let FieldsInfo { tosin_fields, diesel_columns, @@ -147,11 +162,11 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream { } impl<__DB: tosin::db::diesel_backend::Backend, __ST> tosin::db::Queryable<__ST, __DB> for #name - where (#(#real_types),*): tosin::db::Queryable<__ST, __DB> { - type Row = <(#(#real_types),*) as tosin::db::Queryable<__ST, __DB>>::Row; + where (#(#real_db_types),*): tosin::db::Queryable<__ST, __DB> { + type Row = <(#(#real_db_types),*) as tosin::db::Queryable<__ST, __DB>>::Row; fn build(row: Self::Row) -> Self { - let row: (#(#real_types),*) = tosin::db::Queryable::build(row); + let row: (#(#real_db_types),*) = tosin::db::Queryable::build(row); todo!() } } -- cgit v1.2.3