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

Add json(nullable) macro attribute #3677

Open
wants to merge 4 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
24 changes: 18 additions & 6 deletions sqlx-macros-core/src/derives/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote_spanned;
use syn::{
punctuated::Punctuated, token::Comma, Attribute, DeriveInput, Field, LitStr, Meta, Token, Type,
Variant,
parenthesized, punctuated::Punctuated, token::Comma, Attribute, DeriveInput, Field, LitStr, Meta, Token, Type, Variant
};

macro_rules! assert_attribute {
Expand Down Expand Up @@ -61,13 +60,18 @@ pub struct SqlxContainerAttributes {
pub default: bool,
}

pub enum JsonAttribute {
NonNullable,
Nullable
}

pub struct SqlxChildAttributes {
pub rename: Option<String>,
pub default: bool,
pub flatten: bool,
pub try_from: Option<Type>,
pub skip: bool,
pub json: bool,
pub json: Option<JsonAttribute>,
}

pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContainerAttributes> {
Expand Down Expand Up @@ -144,7 +148,7 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
let mut try_from = None;
let mut flatten = false;
let mut skip: bool = false;
let mut json = false;
let mut json = None;

for attr in input.iter().filter(|a| a.path().is_ident("sqlx")) {
attr.parse_nested_meta(|meta| {
Expand All @@ -163,13 +167,21 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
} else if meta.path.is_ident("skip") {
skip = true;
} else if meta.path.is_ident("json") {
json = true;
if meta.input.peek(syn::token::Paren) {
let content;
parenthesized!(content in meta.input);
let literal: Ident = content.parse()?;
assert_eq!(literal.to_string(), "nullable", "Unrecognized `json` attribute. Valid values are `json` or `json(nullable)`");
json = Some(JsonAttribute::Nullable);
} else {
json = Some(JsonAttribute::NonNullable);
}
}

Ok(())
})?;

if json && flatten {
if json.is_some() && flatten {
fail!(
attr,
"Cannot use `json` and `flatten` together on the same field"
Expand Down
29 changes: 20 additions & 9 deletions sqlx-macros-core/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::{
};

use super::{
attributes::{parse_child_attributes, parse_container_attributes},
attributes::{parse_child_attributes, parse_container_attributes, JsonAttribute},
rename_all,
};

Expand Down Expand Up @@ -99,20 +99,20 @@ fn expand_derive_from_row_struct(

let expr: Expr = match (attributes.flatten, attributes.try_from, attributes.json) {
// <No attributes>
(false, None, false) => {
(false, None, None) => {
predicates
.push(parse_quote!(#ty: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(#ty: ::sqlx::types::Type<R::Database>));

parse_quote!(__row.try_get(#id_s))
}
// Flatten
(true, None, false) => {
(true, None, None) => {
predicates.push(parse_quote!(#ty: ::sqlx::FromRow<#lifetime, R>));
parse_quote!(<#ty as ::sqlx::FromRow<#lifetime, R>>::from_row(__row))
}
// Flatten + Try from
(true, Some(try_from), false) => {
(true, Some(try_from), None) => {
predicates.push(parse_quote!(#try_from: ::sqlx::FromRow<#lifetime, R>));
parse_quote!(
<#try_from as ::sqlx::FromRow<#lifetime, R>>::from_row(__row)
Expand All @@ -130,11 +130,11 @@ fn expand_derive_from_row_struct(
)
}
// Flatten + Json
(true, _, true) => {
(true, _, Some(_)) => {
panic!("Cannot use both flatten and json")
}
// Try from
(false, Some(try_from), false) => {
(false, Some(try_from), None) => {
predicates
.push(parse_quote!(#try_from: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(#try_from: ::sqlx::types::Type<R::Database>));
Expand All @@ -154,8 +154,8 @@ fn expand_derive_from_row_struct(
})
)
}
// Try from + Json
(false, Some(try_from), true) => {
// Try from + Json mandatory
(false, Some(try_from), Some(JsonAttribute::NonNullable)) => {
predicates
.push(parse_quote!(::sqlx::types::Json<#try_from>: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(::sqlx::types::Json<#try_from>: ::sqlx::types::Type<R::Database>));
Expand All @@ -175,14 +175,25 @@ fn expand_derive_from_row_struct(
})
)
},
// Try from + Json nullable
(false, Some(_), Some(JsonAttribute::Nullable)) => {
panic!("Cannot use both try from and json nullable")
},
// Json
(false, None, true) => {
(false, None, Some(JsonAttribute::NonNullable)) => {
predicates
.push(parse_quote!(::sqlx::types::Json<#ty>: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(::sqlx::types::Json<#ty>: ::sqlx::types::Type<R::Database>));

parse_quote!(__row.try_get::<::sqlx::types::Json<_>, _>(#id_s).map(|x| x.0))
},
(false, None, Some(JsonAttribute::Nullable)) => {
predicates
.push(parse_quote!(::core::option::Option<::sqlx::types::Json<#ty>>: ::sqlx::decode::Decode<#lifetime, R::Database>));
predicates.push(parse_quote!(::core::option::Option<::sqlx::types::Json<#ty>>: ::sqlx::types::Type<R::Database>));

parse_quote!(__row.try_get::<::core::option::Option<::sqlx::types::Json<_>>, _>(#id_s).map(|x| x.and_then(|y| y.0)))
},
};

if attributes.default {
Expand Down
24 changes: 24 additions & 0 deletions tests/mysql/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,30 @@ async fn test_from_row_json_attr() -> anyhow::Result<()> {
Ok(())
}

#[sqlx_macros::test]
async fn test_from_row_json_attr_nullable() -> anyhow::Result<()> {
#[derive(serde::Deserialize)]
struct J {
a: u32,
b: u32,
}

#[derive(sqlx::FromRow)]
struct Record {
#[sqlx(json(nullable))]
j: Option<J>,
}

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

let record = sqlx::query_as::<_, Record>("select NULL as j")
.fetch_one(&mut conn)
.await?;

assert!(record.j.is_none());
Ok(())
}

#[sqlx_macros::test]
async fn test_from_row_json_try_from_attr() -> anyhow::Result<()> {
#[derive(serde::Deserialize)]
Expand Down