mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 14:34:19 +00:00
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
This commit is contained in:
parent
3440440564
commit
9f1f682285
2 changed files with 35 additions and 2 deletions
|
@ -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<String>,
|
||||
pub default: bool,
|
||||
pub flatten: bool,
|
||||
pub try_from: Option<Ident>,
|
||||
pub try_from: Option<Type>,
|
||||
}
|
||||
|
||||
pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContainerAttributes> {
|
||||
|
|
|
@ -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>(T);
|
||||
|
||||
impl std::convert::TryFrom<ComplexType<i64>> for u64 {
|
||||
type Error = std::num::TryFromIntError;
|
||||
fn try_from(value: ComplexType<i64>) -> Result<Self, Self::Error> {
|
||||
u64::try_from(value.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow)]
|
||||
struct Record {
|
||||
#[sqlx(try_from = "m::ComplexType<i64>")]
|
||||
id: u64,
|
||||
}
|
||||
|
||||
let mut conn = new::<MySql>().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
|
||||
|
|
Loading…
Reference in a new issue