Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: (#2934) Allows flatten attribute to work for Optional values when deriving FromRow #3667

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion sqlx-core/src/from_row.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{error::Error, row::Row};
use std::marker::PhantomData;

use crate::{
error::{Error, UnexpectedNullError},
row::Row,
};

/// A record that can be built from a row returned by the database.
///
Expand Down Expand Up @@ -487,3 +492,47 @@ impl_from_row_for_tuple!(
(14) -> T15;
(15) -> T16;
);

pub struct Wrapper<T>(pub PhantomData<T>);

pub trait FromOptRow<'r, R, T> {
fn __from_row(&self, row: &'r R) -> Result<T, Error>;
}

impl<'r, R, T> FromOptRow<'r, R, Option<T>> for Wrapper<Option<T>>
where
R: Row,
T: FromRow<'r, R>,
{
fn __from_row(&self, row: &'r R) -> Result<Option<T>, Error> {
let value = T::from_row(row).map(Some);
if let Err(Error::ColumnDecode { source, .. }) = value.as_ref() {
if let Some(UnexpectedNullError) = source.downcast_ref() {
return Ok(None);
}
}
value
}
}

impl<'r, R, T> FromOptRow<'r, R, T> for &Wrapper<T>
where
R: Row,
T: FromRow<'r, R>,
{
fn __from_row(&self, row: &'r R) -> Result<T, Error> {
T::from_row(row)
}
}

#[doc(hidden)]
#[macro_export]
macro_rules! __from_opt_row {
($t:ty, $row:expr) => {{
use std::marker::PhantomData;
use $crate::from_row::{FromOptRow, Wrapper};
let wrapper = Wrapper(PhantomData::<$t>);
let value = (&wrapper).__from_row($row);
value
}};
}
6 changes: 2 additions & 4 deletions sqlx-macros-core/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ fn expand_derive_from_row_struct(
}
// Flatten
(true, None, false) => {
predicates.push(parse_quote!(#ty: ::sqlx::FromRow<#lifetime, R>));
parse_quote!(<#ty as ::sqlx::FromRow<#lifetime, R>>::from_row(__row))
parse_quote!(::sqlx::__from_opt_row!(#ty, __row))
}
// Flatten + Try from
(true, Some(try_from), false) => {
predicates.push(parse_quote!(#try_from: ::sqlx::FromRow<#lifetime, R>));
parse_quote!(
<#try_from as ::sqlx::FromRow<#lifetime, R>>::from_row(__row)
::sqlx::__from_opt_row!(#try_from, __row)
.and_then(|v| {
<#ty as ::std::convert::TryFrom::<#try_from>>::try_from(v)
.map_err(|e| {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub use sqlx_core::connection::{ConnectOptions, Connection};
pub use sqlx_core::database::{self, Database};
pub use sqlx_core::describe::Describe;
pub use sqlx_core::executor::{Execute, Executor};
pub use sqlx_core::from_row::FromRow;
pub use sqlx_core::from_row::{FromRow, Wrapper, FromOptRow};
pub use sqlx_core::__from_opt_row;
pub use sqlx_core::pool::{self, Pool};
#[doc(hidden)]
pub use sqlx_core::query::query_with_result as __query_with_result;
Expand Down