Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
seanaye committed Jan 10, 2025
1 parent 39e8be3 commit e3175ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
7 changes: 4 additions & 3 deletions sqlx-macros-core/src/derives/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote_spanned;
use syn::{
parenthesized, parse::discouraged::AnyDelimiter, punctuated::Punctuated, token::{self, 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 @@ -170,7 +170,8 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
if meta.input.peek(syn::token::Paren) {
let content;
parenthesized!(content in meta.input);
let literal: Token![nullable] = content.parse()?;
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);
Expand All @@ -180,7 +181,7 @@ pub fn parse_child_attributes(input: &[Attribute]) -> syn::Result<SqlxChildAttri
Ok(())
})?;

if json && flatten {
if json.is_some() && flatten {
fail!(
attr,
"Cannot use `json` and `flatten` together on the same field"
Expand Down
2 changes: 1 addition & 1 deletion sqlx-macros-core/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn expand_derive_from_row_struct(
)
},
// Try from + Json nullable
(false, Some(try_from), Some(JsonAttribute::Nullable)) => {
(false, Some(_), Some(JsonAttribute::Nullable)) => {
panic!("Cannot use both try from and json nullable")
},
// Json
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_eq!(record.j, None);
Ok(())
}

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

0 comments on commit e3175ac

Please sign in to comment.