From 6a3cba1f80553ef7553835bda06b52d3e1f482a9 Mon Sep 17 00:00:00 2001 From: Marco Cameriero Date: Tue, 20 Sep 2022 14:10:52 -0700 Subject: [PATCH 1/2] Use `syn::Type` instead of `syn::Ident` to parse the value of `#[sqlx(try_from = "...")]` --- sqlx-macros-core/src/derives/attributes.rs | 4 +-- tests/mysql/macros.rs | 33 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/sqlx-macros-core/src/derives/attributes.rs b/sqlx-macros-core/src/derives/attributes.rs index 664e2301a6..bf3c126c45 100644 --- a/sqlx-macros-core/src/derives/attributes.rs +++ b/sqlx-macros-core/src/derives/attributes.rs @@ -3,7 +3,7 @@ use quote::{quote, quote_spanned}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::token::Comma; -use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant}; +use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Type, Variant}; macro_rules! assert_attribute { ($e:expr, $err:expr, $input:expr) => { @@ -62,7 +62,7 @@ pub struct SqlxChildAttributes { pub rename: Option, pub default: bool, pub flatten: bool, - pub try_from: Option, + pub try_from: Option, } pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result { diff --git a/tests/mysql/macros.rs b/tests/mysql/macros.rs index ce8e0d2428..fb736beea3 100644 --- a/tests/mysql/macros.rs +++ b/tests/mysql/macros.rs @@ -435,4 +435,37 @@ async fn test_try_from_attr_with_flatten() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn test_try_from_attr_with_complex_type() -> anyhow::Result<()> { + mod m { + #[derive(sqlx::Type)] + #[sqlx(transparent)] + pub struct ComplexType(T); + + impl std::convert::TryFrom> for u64 { + type Error = std::num::TryFromIntError; + fn try_from(value: ComplexType) -> Result { + u64::try_from(value.0) + } + } + } + + #[derive(sqlx::FromRow)] + struct Record { + #[sqlx(try_from = "m::ComplexType")] + id: u64, + } + + let mut conn = new::().await?; + let (mut conn, id) = with_test_row(&mut conn).await?; + + let record = sqlx::query_as::<_, Record>("select id from tweet") + .fetch_one(&mut conn) + .await?; + + assert_eq!(record.id, id.0 as u64); + + Ok(()) +} + // we don't emit bind parameter type-checks for MySQL so testing the overrides is redundant From c2c77aa163254672140e34b2b720d97c76893371 Mon Sep 17 00:00:00 2001 From: Marco Cameriero Date: Thu, 9 Feb 2023 00:20:57 +0100 Subject: [PATCH 2/2] Fix broken test after rebase --- tests/mysql/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mysql/macros.rs b/tests/mysql/macros.rs index fb736beea3..24807d5606 100644 --- a/tests/mysql/macros.rs +++ b/tests/mysql/macros.rs @@ -460,7 +460,7 @@ async fn test_try_from_attr_with_complex_type() -> anyhow::Result<()> { let (mut conn, id) = with_test_row(&mut conn).await?; let record = sqlx::query_as::<_, Record>("select id from tweet") - .fetch_one(&mut conn) + .fetch_one(&mut *conn) .await?; assert_eq!(record.id, id.0 as u64);