Skip to content

Commit 0840a57

Browse files
committed
fix: solved a bug in the CanyonMapper macro that was causing issues to the conditionally compiled code
1 parent e229264 commit 0840a57

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

canyon_macros/src/canyon_macro.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Provides helpers to build the `#[canyon_macros::canyon]` procedural like attribute macro
2+
#![cfg(feature = "migrations")]
23

34
use canyon_connection::CANYON_TOKIO_RUNTIME;
45
use canyon_migrations::migrations::handler::Migrations;
56
use canyon_migrations::{CM_QUERIES_TO_EXECUTE, QUERIES_TO_EXECUTE};
67
use proc_macro2::TokenStream;
78
use quote::quote;
89

9-
#[cfg(feature = "migrations")]
1010
pub fn main_with_queries() -> TokenStream {
1111
CANYON_TOKIO_RUNTIME.block_on(async {
1212
canyon_connection::init_connections_cache().await;
@@ -25,7 +25,6 @@ pub fn main_with_queries() -> TokenStream {
2525

2626
/// Creates a TokenScream that is used to load the data generated at compile-time
2727
/// by the `CanyonManaged` macros again on the queries register
28-
#[cfg(feature = "migrations")]
2928
fn wire_queries_to_execute(canyon_manager_tokens: &mut Vec<TokenStream>) {
3029
let cm_data = CM_QUERIES_TO_EXECUTE.lock().unwrap();
3130
let data = QUERIES_TO_EXECUTE.lock().unwrap();

canyon_macros/src/lib.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod utils;
1111
use canyon_entity_macro::parse_canyon_entity_proc_macro_attr;
1212
use proc_macro::TokenStream as CompilerTokenStream;
1313
use proc_macro2::{Ident, TokenStream};
14-
use quote::{quote, ToTokens};
14+
use quote::quote;
1515
use syn::{DeriveInput, Fields, Type, Visibility};
1616

1717
use query_operations::{
@@ -442,6 +442,11 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
442442
}
443443
});
444444

445+
446+
// TODO: refactor the code below after the current bugfixes, to conditinally generate
447+
// the required methods and populate the CanyonMapper trait dependencing on the cfg flags
448+
// enabled with a more elegant solution (a fn for feature, for ex)
449+
#[cfg(feature = "postgres")]
445450
// Here it's where the incoming values of the DatabaseResult are wired into a new
446451
// instance, mapping the fields of the type against the columns
447452
let init_field_values = fields.iter().map(|(_vis, ident, _ty)| {
@@ -452,6 +457,8 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
452457
}
453458
});
454459

460+
461+
#[cfg(feature = "mssql")]
455462
let init_field_values_sqlserver = fields.iter().map(|(_vis, ident, ty)| {
456463
let ident_name = ident.to_string();
457464

@@ -530,6 +537,7 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
530537
}
531538
});
532539

540+
#[cfg(feature = "mysql")]
533541
let init_field_values_mysql = fields.iter().map(|(_vis, ident, _ty)| {
534542
let ident_name = ident.to_string();
535543
quote! {
@@ -541,27 +549,40 @@ pub fn implement_row_mapper_for_type(input: proc_macro::TokenStream) -> proc_mac
541549
// The type of the Struct
542550
let ty = ast.ident;
543551

544-
let tokens = quote! {
545-
impl canyon_sql::crud::RowMapper<Self> for #ty {
546-
#[cfg(feature="postgres")]
547-
fn deserialize_postgresql(row: &canyon_sql::db_clients::tokio_postgres::Row) -> #ty {
548-
Self {
549-
#(#init_field_values),*
550-
}
552+
let mut impl_methods = quote! {}; // Collect methods conditionally
553+
554+
#[cfg(feature = "postgres")]
555+
impl_methods.extend(quote! {
556+
fn deserialize_postgresql(row: &canyon_sql::db_clients::tokio_postgres::Row) -> #ty {
557+
Self {
558+
#(#init_field_values),*
551559
}
552-
#[cfg(feature="mssql")]
553-
fn deserialize_sqlserver(row: &canyon_sql::db_clients::tiberius::Row) -> #ty {
554-
Self {
555-
#(#init_field_values_sqlserver),*
556-
}
560+
}
561+
});
562+
563+
#[cfg(feature = "mssql")]
564+
impl_methods.extend(quote! {
565+
fn deserialize_sqlserver(row: &canyon_sql::db_clients::tiberius::Row) -> #ty {
566+
Self {
567+
#(#init_field_values_sqlserver),*
557568
}
558-
#[cfg(feature="mysql")]
559-
fn deserialize_mysql(row: &canyon_sql::db_clients::mysql_async::Row) -> #ty {
560-
Self {
561-
#(#init_field_values_mysql),*
562-
}
569+
}
570+
});
571+
572+
#[cfg(feature = "mysql")]
573+
impl_methods.extend(quote! {
574+
fn deserialize_mysql(row: &canyon_sql::db_clients::mysql_async::Row) -> #ty {
575+
Self {
576+
#(#init_field_values_mysql),*
563577
}
564578
}
579+
});
580+
581+
// Wrap everything in the shared `impl` block
582+
let tokens = quote! {
583+
impl canyon_sql::crud::RowMapper<Self> for #ty {
584+
#impl_methods
585+
}
565586
};
566587

567588
tokens.into()
@@ -588,6 +609,9 @@ fn fields_with_types(fields: &Fields) -> Vec<(Visibility, Ident, Type)> {
588609
.collect::<Vec<_>>()
589610
}
590611

612+
#[cfg(feature = "mssql")]
613+
use quote::ToTokens;
614+
#[cfg(feature = "mssql")]
591615
fn get_field_type_as_string(typ: &Type) -> String {
592616
match typ {
593617
Type::Array(type_) => type_.to_token_stream().to_string(),

0 commit comments

Comments
 (0)