aboutsummaryrefslogtreecommitdiff
path: root/tosin-macros/src/lib.rs
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-06-30 00:27:49 -0600
committerMelody Horn <melody@boringcactus.com>2021-06-30 00:27:49 -0600
commit434b6d19a5a47e0d3b4399d9937565723328643e (patch)
treefef8238cbffe8f3b0188e85a80174c4337bb3e7f /tosin-macros/src/lib.rs
parent9829bbfcd57c57e237c6aacb96a78d0b9f5bab68 (diff)
downloadtosin-434b6d19a5a47e0d3b4399d9937565723328643e.tar.gz
tosin-434b6d19a5a47e0d3b4399d9937565723328643e.zip
juggle Id so FromSql works out
Diffstat (limited to 'tosin-macros/src/lib.rs')
-rw-r--r--tosin-macros/src/lib.rs27
1 files changed, 21 insertions, 6 deletions
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<impl quote::ToTokens> {
if field_type == &parse_type("Option<Id>") {
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<Id>").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!()
}
}