Skip to content

Commit 4336a63

Browse files
95ulissegrgi
authored andcommitted
Allow using complex types in try_from when deriving FromRow (#2115)
* Use `syn::Type` instead of `syn::Ident` to parse the value of `#[sqlx(try_from = "...")]` * Fix broken test after rebase
1 parent 2718d6f commit 4336a63

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

sqlx-macros-core/src/derives/attributes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use quote::{quote, quote_spanned};
33
use syn::punctuated::Punctuated;
44
use syn::spanned::Spanned;
55
use syn::token::Comma;
6-
use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant};
6+
use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Type, Variant};
77

88
macro_rules! assert_attribute {
99
($e:expr, $err:expr, $input:expr) => {
@@ -62,7 +62,7 @@ pub struct SqlxChildAttributes {
6262
pub rename: Option<String>,
6363
pub default: bool,
6464
pub flatten: bool,
65-
pub try_from: Option<Ident>,
65+
pub try_from: Option<Type>,
6666
pub skip: bool,
6767
}
6868

tests/mysql/macros.rs

+33
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,37 @@ async fn test_try_from_attr_with_flatten() -> anyhow::Result<()> {
435435
Ok(())
436436
}
437437

438+
#[sqlx_macros::test]
439+
async fn test_try_from_attr_with_complex_type() -> anyhow::Result<()> {
440+
mod m {
441+
#[derive(sqlx::Type)]
442+
#[sqlx(transparent)]
443+
pub struct ComplexType<T>(T);
444+
445+
impl std::convert::TryFrom<ComplexType<i64>> for u64 {
446+
type Error = std::num::TryFromIntError;
447+
fn try_from(value: ComplexType<i64>) -> Result<Self, Self::Error> {
448+
u64::try_from(value.0)
449+
}
450+
}
451+
}
452+
453+
#[derive(sqlx::FromRow)]
454+
struct Record {
455+
#[sqlx(try_from = "m::ComplexType<i64>")]
456+
id: u64,
457+
}
458+
459+
let mut conn = new::<MySql>().await?;
460+
let (mut conn, id) = with_test_row(&mut conn).await?;
461+
462+
let record = sqlx::query_as::<_, Record>("select id from tweet")
463+
.fetch_one(&mut *conn)
464+
.await?;
465+
466+
assert_eq!(record.id, id.0 as u64);
467+
468+
Ok(())
469+
}
470+
438471
// we don't emit bind parameter type-checks for MySQL so testing the overrides is redundant

0 commit comments

Comments
 (0)