#[derive(Serialize)] for Record#1073
#[derive(Serialize)] for Record#1073rich-murphey wants to merge 6 commits intolaunchbadge:masterfrom
Conversation
sqlx-macros/src/query/mod.rs
Outdated
| #[derive(Debug)] | ||
| #[derive(Debug,Serialize)] | ||
| // I cannot get this to work: | ||
| // #[cfg_attr(feature = "json", derive(Serialize))] |
There was a problem hiding this comment.
The json feature needs to be tested outside of quote! so it can look for the feature in the sqlx-macros crate and not whatever crate invoked the derive.
There was a problem hiding this comment.
Additionally, you'll want to do serde::Serialize so the user doesn't need to have the derive imported.
There was a problem hiding this comment.
Nit: Should re-export serde in a #[doc(hidden)] exports module and use it as ::sqlx::export::serde::Deserialize so the user doesn't need to depend on serde directly either.
|
when enabled, this breaks any query containing a column that is not Serialize-able, such as:
So enabling it with an existing feature such as "json" would cause issues. I'm not sure this deserves a separate feature flag. Need some direction here. An 'export' module seems like a good idea. I don't see one as it stands, and it sounds easy enough to add. |
|
With these changes, Record implements Serialize when the "serialize" feature flag is asserted. I added a unit test that confirms the json text of a query result. |
|
Here's an example use case for this feature: |
|
Here's an example actix-web streaming response: The query_json!() macro is defined here: |
|
I don't really like the idea of a Cargo feature enabling backwards-incompatible codegen. That sounds like adding to the nightmare we already have with the runtime features. I think this could be worked into our proposal on configuring the macros in user code: #916 |
| assert_eq!(&json, r#"[{"id":1}]"#); | ||
|
|
||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
This isn't a sufficient test to ensure that the codegen works for external users. Maybe add a small project in examples/postgres/ to demonstrate (which will be covered whenever we get around to turning on CI for examples).
There was a problem hiding this comment.
OK, I'll do the equivalent of examples/postgres/json unless you have more preferences.
There was a problem hiding this comment.
Hey @abonander, thanks for clarifying the concern that the feature flag would cause compile time errors for users' existing code. And also thanks for the pointer to #916.
We might consider using a visually similar syntax for specifying the derives, such as:
sqlx::query!("SELECT * FROM foo", derive(Serialize))
Given that the feature flag appears to be unworkable, I'm ready to close this PR in favor of a configurable query!() macro.
There was a problem hiding this comment.
That old conflict with a call to a function named derive, though not sure whether that's a problem 😄
There was a problem hiding this comment.
Good point. Even though it could be disambiguated by matching derive(($id:ident),*), yea, that would still break any code that might pass derive(a,b,c...) as a bound SQL parameter.
|
@abonander suggested considering this in #916 - a proposal on configuring the macros in user code. I'll go ahead and close this in a few days unless someone has more to add. |
It might be convenient for some who use actix-web, to derive serde::Serialize for a Record created by the query!() macro.
The idea here is that one could write an actix-web method such as:
As it stands, this PR will fail CI tests because use of serde::Serialize is not behind a feature flag such as "json". I just could not find a way to make the feature flag work. Please feel free to make any changes to this that you see fit!