-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
sql_types::Json: Add documentation how to de/serialize to custom type #4798
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
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
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.
Thanks for opening this PR.
I think that's a good location to document these things and it's also a good idea to include examples. I left a few remarks on what's potentially broken and causes the CI to fail.
| /// struct Contact { | ||
| /// pub id: i32, | ||
| /// pub name: String, | ||
| /// #[diesel(deserialize_as = serde_json::Value)] |
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.
This requires an impl TryFrom<serde_json::Value> for Address
| /// # .get_result::<serde_json::Value>(connection)?; | ||
| /// | ||
| /// let santa = contacts.filter(id.eq(1)).first(connection)?; | ||
| /// assert_eq!(santas_address, santda.address); |
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.
| /// assert_eq!(santas_address, santda.address); | |
| /// assert_eq!(santas_address, santa.address); |
that's likely a typo
| /// }; | ||
| /// | ||
| /// #let inserted_address = insert_into(contacts) | ||
| /// # .values((name.eq("Claus"), address.eq(&santas_address))) |
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.
For this variant you need to directly convert the address to a serde_json::Value as there is no attribute that's applied.
Following #4793 (comment), this adds documentation to the
sql_types::Jsontype on how to de/serialize from/to custom types with JSON.Right now this is in draft mode, to get some feedback whether I added the docs in the right location (I believe so, because that's where I'd expect them) and whether the idea of having a code example at that location was the right way of doing this.
What is left to do here:
serialize_aspartjson()function in sqlite to get the desired behaviour out of SQLite (link to the approriate SQLite docs)Into::into()approach for converting from (for example)serde_json::Valueto your custom type when usingdeserialize_asrather thanTryFrom, which might be unexpected (Question for you: Any reason why this isFrom/Intorather thanTryFrom?)CC @weiznich