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

[WIP] Support for #[sqlx(default)] #495

Merged
merged 1 commit into from
Jul 12, 2020
Merged
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
6 changes: 4 additions & 2 deletions sqlx-macros/src/derives/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct SqlxContainerAttributes {

pub struct SqlxChildAttributes {
pub rename: Option<String>,
pub default: bool,
}

pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContainerAttributes> {
Expand Down Expand Up @@ -116,6 +117,7 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai

pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttributes> {
let mut rename = None;
let mut default = false;

for attr in input {
let meta = attr
Expand All @@ -132,7 +134,7 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
lit: Lit::Str(val),
..
}) if path.is_ident("rename") => try_set!(rename, val.value(), value),

Meta::Path(path) if path.is_ident("default") => default = true,
u => fail!(u, "unexpected attribute"),
},
u => fail!(u, "unexpected attribute"),
Expand All @@ -143,7 +145,7 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
}
}

Ok(SqlxChildAttributes { rename })
Ok(SqlxChildAttributes { rename, default })
}

pub fn check_transparent_attributes(
Expand Down
17 changes: 14 additions & 3 deletions sqlx-macros/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,20 @@ fn expand_derive_from_row_struct(
};
let ty = &field.ty;

Some(parse_quote!(
let #id: #ty = row.try_get(#id_s)?;
))
if attributes.default {
Some(
parse_quote!(let #id: #ty = row.try_get(#id_s).or_else(|e| match e {
sqlx_core::error::Error::ColumnNotFound(_) => {
Ok(Default::default())
},
e => Err(e)
})?;),
)
} else {
Some(parse_quote!(
let #id: #ty = row.try_get(#id_s)?;
))
}
});

let names = fields.iter().map(|field| &field.ident);
Expand Down
23 changes: 23 additions & 0 deletions tests/postgres/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,26 @@ async fn test_from_row_with_rename() -> anyhow::Result<()> {

Ok(())
}

#[cfg(feature = "macros")]
#[sqlx_macros::test]
async fn test_default() -> anyhow::Result<()> {
#[derive(Debug, sqlx::FromRow)]
struct HasDefault {
not_default: i32,
#[sqlx(default)]
default: Option<i32>,
}

let mut conn = new::<Postgres>().await?;

let has_default: HasDefault = sqlx::query_as(r#"SELECT 1 AS not_default"#)
.fetch_one(&mut conn)
.await?;
println!("{:?}", has_default);

assert_eq!(has_default.not_default, 1);
assert_eq!(has_default.default, None);

Ok(())
}