-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Allow single-field named structs to be transparent #3971
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
base: main
Are you sure you want to change the base?
Conversation
This more closely matches the criteria for e.g. #[repr(transparent)] and #[serde(transparent)].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine with a couple of nits + docs and testing.
Tests (Where should those go? The change isn't backend-specific)
Preferably you should add a test for each backend to be sure the behavior is consistent. But if you don't have the time for that, I'd say just pick one:
c0ae489
to
2eb8165
Compare
tests/mysql/macros.rs
Outdated
#[derive(PartialEq, Eq, Debug, sqlx::Type)] | ||
#[sqlx(transparent)] | ||
struct MyIntNamed { | ||
inner: i64, | ||
} | ||
|
||
struct NamedTransparentRecord { | ||
id: MyIntNamed, | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn test_named_transparent_struct() -> anyhow::Result<()> { | ||
let mut conn = new::<MySql>().await?; | ||
let (mut conn, id) = with_test_row(&mut conn).await?; | ||
|
||
let record = sqlx::query_as!(NamedTransparentRecord, "select id as `id: _` from tweet") | ||
.fetch_one(&mut *conn) | ||
.await?; | ||
|
||
assert_eq!(record.id, MyIntNamed { inner: id.0 }); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, this is testing the derive behavior more than the query macro behavior, so it should be moved to derives.rs
.
When you do that, please add #[cfg(feature = "macros")]
to the test like the other tests using query macros there. You might also move the struct definitions into the function so they don't emit "unused code" warnings when the feature is off.
Either that, or just rewrite the test to not use the query macros since that's not really what we're looking to test here.
tests/postgres/macros.rs
Outdated
#[derive(PartialEq, Eq, Debug, sqlx::Type)] | ||
#[sqlx(transparent)] | ||
struct MyIntNamed { | ||
inner: i64, | ||
} | ||
|
||
struct NamedTransparentRecord { | ||
id: MyIntNamed, | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn test_named_transparent_struct() -> anyhow::Result<()> { | ||
let mut conn = new::<Postgres>().await?; | ||
let mut conn = with_test_row(&mut conn).await?; | ||
|
||
let record = sqlx::query_as!(NamedTransparentRecord, r#"select id as "id: _" from tweet"#) | ||
.fetch_one(&mut *conn) | ||
.await?; | ||
|
||
assert_eq!(record.id, MyIntNamed { inner: 1 }); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing re. moving to derives.rs
.
tests/sqlite/macros.rs
Outdated
#[derive(PartialEq, Eq, Debug, sqlx::Type)] | ||
#[sqlx(transparent)] | ||
struct MyIntNamed { | ||
inner: i64, | ||
} | ||
|
||
struct NamedTransparentRecord { | ||
id: MyIntNamed, | ||
} | ||
|
||
#[sqlx_macros::test] | ||
async fn test_named_transparent_struct() -> anyhow::Result<()> { | ||
let mut conn = new::<Sqlite>().await?; | ||
|
||
let record = sqlx::query_as!(NamedTransparentRecord, r#"select id as "id: _" from tweet"#) | ||
.fetch_one(&mut conn) | ||
.await?; | ||
|
||
assert_eq!(record.id, MyIntNamed { inner: 1 }); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing re. moving to derives.rs
.
53d62c3
to
985d526
Compare
985d526
to
d125276
Compare
This more closely matches the criteria for e.g.
#[repr(transparent)]
and#[serde(transparent)]
.TODO, if this is deemed a desirable change:
Does your PR solve an issue?
Couldn't find an issue filed already.
Is this a breaking change?
No, code that previously compiled is unchanged, only code that used to be a compile error now does compile.