You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Could not find any related issue nor pull request.
Description
It would be nice specify that I want a specific field of my struct to be encoded or decoded using a different implementation of the original Decode and Encode of the specific type, something like serde serialize_with and deserialize_with.
Prefered solution
What I imagine is that sqlx would provide a field attribute called decode_with and encode_with (just like serde) which expects a function name, and this function name would have the implementation inside for whatever we want.
encode_with would be callable with
fn<'q,O,DB>(&T,&mutDB::ArgumentBuffer<'q>) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError>
where
DB:Database,// This is the output type we want to encode to, say for example String, i32, bool, etc.O:Encode<'q,DB>;
and decode_with
fn<'r,I,DB>(DB::ValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError>
where
DB:Database,// This is the input type we want to decode from.I:Decode<'r,DB>;
and the implementation may look something like this:
#[derive(sqlx::Type)]#[repr(i8)]pubenumStatus{Processed = 1,Processing = 2,Failed = 3,}implDisplayforStatus{ ... }#[derive(sqlx::FromRow)]pubstructData{pubid:Uuid,#[sqlx(encode_with = "encode_as_string", decode_with = "decode_as_string")]pubstatus:Status,// In this case, would use the `sqlx::Type` derived impls.pubstatus_code:Status,}fnencode_as_string<'q,DB>(value:&StatusCode,buf:&mutDB::ArgumentBuffer<'q>,) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError>whereDB: sqlx::database::Database,String: sqlx::Encode<'q,DB>,{
<Stringas sqlx::Encode<'q,DB>>::encode(value.to_string(), buf)}fndecode_as_string<'r,DB>(value: <DBasDatabase>::ValueRef<'r>) -> Result<Status, sqlx::error::BoxDynError>whereDB:Database,String:Decode<'r,DB>{let value = <StringasDecode<'r,DB>>::decode(value)?;/// Match each enum as String.match value { ...}}
Is this a breaking change? Why or why not?
It's not breaking change, it would only add another proc macro attribute that supports this.
The text was updated successfully, but these errors were encountered:
I have found these related issues/pull requests
Could not find any related issue nor pull request.
Description
It would be nice specify that I want a specific field of my struct to be
encoded
ordecoded
using a different implementation of the originalDecode
andEncode
of the specific type, something like serdeserialize_with
anddeserialize_with
.Prefered solution
What I imagine is that sqlx would provide a field attribute called
decode_with
andencode_with
(just like serde) which expects a function name, and this function name would have the implementation inside for whatever we want.encode_with
would be callable withand
decode_with
and the implementation may look something like this:
Is this a breaking change? Why or why not?
It's not breaking change, it would only add another proc macro attribute that supports this.
The text was updated successfully, but these errors were encountered: