From fe22cef7222b1bfcbf7d2fd9c12f3eed2f96689f Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Sat, 2 Sep 2023 05:54:18 +0100 Subject: [PATCH] ensure Uuid works with relationships --- ensemble/src/types/datetime.rs | 1 + ensemble/src/types/uuid.rs | 1 + ensemble_derive/src/model/mod.rs | 2 +- ensemble_derive/src/model/serde.rs | 10 ++++++++-- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ensemble/src/types/datetime.rs b/ensemble/src/types/datetime.rs index 70662d1..2ff253a 100644 --- a/ensemble/src/types/datetime.rs +++ b/ensemble/src/types/datetime.rs @@ -8,6 +8,7 @@ use std::time::Duration; /// A date and time value, used for storing timestamps in the database. #[derive(Clone, Eq, PartialEq, Hash)] +#[repr(transparent)] pub struct DateTime(pub fastdate::DateTime); impl Display for DateTime { diff --git a/ensemble/src/types/uuid.rs b/ensemble/src/types/uuid.rs index 20e5ba7..d055d49 100644 --- a/ensemble/src/types/uuid.rs +++ b/ensemble/src/types/uuid.rs @@ -8,6 +8,7 @@ use rbs::Value; use serde::Deserializer; #[derive(serde::Serialize, Clone, Eq, PartialEq, Hash, Default)] +#[repr(transparent)] pub struct Uuid(uuid::Uuid); impl<'de> serde::Deserialize<'de> for Uuid { diff --git a/ensemble_derive/src/model/mod.rs b/ensemble_derive/src/model/mod.rs index 39511b8..a09688b 100644 --- a/ensemble_derive/src/model/mod.rs +++ b/ensemble_derive/src/model/mod.rs @@ -108,7 +108,7 @@ fn impl_eager_load(fields: &Fields) -> TokenStream { let ident = &field.ident; quote_spanned! {field.span() => - stringify!(#ident) => self.#ident.eager_query(related.iter().map(|model| model.#ident.value).collect()), + stringify!(#ident) => self.#ident.eager_query(related.iter().map(|model| &model.#ident.value).cloned().collect()), } }); diff --git a/ensemble_derive/src/model/serde.rs b/ensemble_derive/src/model/serde.rs index dcdf1e6..8b536d0 100644 --- a/ensemble_derive/src/model/serde.rs +++ b/ensemble_derive/src/model/serde.rs @@ -219,7 +219,8 @@ fn visitor_deserialize( return None; } - Some(quote_spanned! {f.span()=> let #ident = #ident.ok_or_else(|| _serde::de::Error::missing_field(stringify!(#column)))?; }) + let ty = &f.ty; + Some(quote_spanned! {f.span()=> let #ident: #ty = #ident.ok_or_else(|| _serde::de::Error::missing_field(stringify!(#column)))?; }) }); let ensure_no_leftovers = if needs_collect { @@ -236,7 +237,11 @@ fn visitor_deserialize( let ident = &f.ident; let Some((relationship_type, related, (relationship_key, relationship_expr))) = &f.relationship(primary_key) else { - return quote_spanned! {f.span()=> #ident: #ident }; + return if f.attr.used_in_relationship { + quote_spanned! {f.span()=> #ident: #ident.clone() } + } else { + quote_spanned! {f.span()=> #ident: #ident } + } }; let relationship_ident = Ident::new(&relationship_type.to_string(), f.span()); @@ -285,6 +290,7 @@ fn visitor_deserialize( }; Ok(quote! { + #[allow(clippy::clone_on_copy)] impl<'de> _serde::de::Visitor<'de> for #visitor_name { type Value = #name;