Skip to content

Commit

Permalink
ensure Uuid works with relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Sep 2, 2023
1 parent 9d1393b commit fe22cef
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions ensemble/src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions ensemble/src/types/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ensemble_derive/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
});

Expand Down
10 changes: 8 additions & 2 deletions ensemble_derive/src/model/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit fe22cef

Please sign in to comment.