Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions typesense/src/traits/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ impl<T: Document> ToTypesenseField for T {
}
}

/// Generic implementation for a Vec of any type that is also a Typesense document.
impl<T: Document> ToTypesenseField for Vec<T> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
"object[]"
}
}

impl<T: ToTypesenseField> ToTypesenseField for Option<T> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
T::to_typesense_type()
}
}

/// macro used internally to add implementations of ToTypesenseField for several rust types.
#[macro_export]
macro_rules! impl_to_typesense_field (
Expand All @@ -42,18 +27,6 @@ macro_rules! impl_to_typesense_field (
$typesense_type
}
}
impl $crate::prelude::ToTypesenseField for Vec<$for> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
impl $crate::prelude::ToTypesenseField for Vec<Option<$for>> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
};

($for:ty, $typesense_type:expr, $any:ident $(: $any_bound:path)?) => {
Expand All @@ -63,18 +36,6 @@ macro_rules! impl_to_typesense_field (
$typesense_type
}
}
impl<$any $(: $any_bound)?> $crate::prelude::ToTypesenseField for Vec<$for> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
impl<$any $(: $any_bound)?> $crate::prelude::ToTypesenseField for Vec<Option<$for>> {
#[inline(always)]
fn to_typesense_type() -> &'static str {
concat!($typesense_type, "[]")
}
}
};
);

Expand Down
46 changes: 46 additions & 0 deletions typesense/tests/client/derive_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ struct Manufacturer {
city: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub enum Priority {
Low,
Medium,
High,
}

impl ToTypesenseField for Priority {
fn to_typesense_type() -> &'static str {
"string"
}
}

/// The main struct that uses every feature of the derive macro.
#[derive(Typesense, Serialize, Deserialize, Debug, PartialEq, Clone)]
#[typesense(
Expand Down Expand Up @@ -105,6 +118,12 @@ struct MegaProduct {

tags: Option<Vec<String>>,

priority: Option<Priority>,

priorities: Vec<Priority>,

priorities_opt: Vec<Option<Priority>>,

#[typesense(rename = "primary_address.city")]
#[serde(rename = "primary_address.city")]
primary_city: String,
Expand Down Expand Up @@ -249,6 +268,24 @@ async fn logic_test_derive_macro_with_generic_client_lifecycle() {
"Field 'tags' should have type 'string[]'"
);
}
"priority" => {
assert_eq!(
actual_field.r#type, "string",
"Field 'priority' should have type 'string[]'"
);
}
"priorities" => {
assert_eq!(
actual_field.r#type, "string[]",
"Field 'priorities' should have type 'string[]'"
);
}
"priorities_opt" => {
assert_eq!(
actual_field.r#type, "string[]",
"Field 'priorities_opt' should have type 'string[]'"
);
}
"details" => {
assert!(false, "Parent field 'details' should have been skipped")
}
Expand Down Expand Up @@ -413,6 +450,9 @@ async fn logic_test_derive_macro_with_generic_client_lifecycle() {
},
],
tags: Some(vec!["steel".to_owned(), "heavy-duty".to_owned()]),
priority: Some(Priority::Low),
priorities: vec![Priority::Low],
priorities_opt: vec![Some(Priority::Low)],
primary_city: "City".to_owned(),
locale: "Xin chào!".to_owned(),
qty: 123,
Expand Down Expand Up @@ -542,6 +582,9 @@ async fn logic_test_derive_macro_with_generic_client_lifecycle() {
let update_payload = MegaProductPartial {
price: Some(25.99),
tags: Some(Some(vec!["steel".to_owned(), "sale".to_owned()])),
priority: Some(Some(Priority::Medium)),
priorities: Some(vec![Priority::Medium]),
priorities_opt: Some(vec![Some(Priority::Medium)]),
..Default::default()
};

Expand All @@ -562,6 +605,9 @@ async fn logic_test_derive_macro_with_generic_client_lifecycle() {
updated_product.tags,
Some(vec!["steel".to_owned(), "sale".to_owned()])
);
assert_eq!(updated_product.priority, Some(Priority::Medium));
assert_eq!(updated_product.priorities, vec![Priority::Medium]);
assert_eq!(updated_product.priorities_opt, vec![Some(Priority::Medium)]);
assert_eq!(updated_product.title, product1.title); // Unchanged field

// Delete Document
Expand Down
21 changes: 19 additions & 2 deletions typesense_derive/src/field_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ fn build_regular_field(field: &Field, field_attrs: &FieldAttributes) -> proc_mac
(&field.ty, false)
};

// Peel `Vec<_>` (and `Vec<Option<_>>`) so `ToTypesenseField` resolves on
// the element type and `"[]"` is appended at runtime. Lets downstream
// crates use local element types without hitting orphan rules.
let (ty, is_vec) = if let Some(vec_inner) = ty_inner_type(ty, "Vec") {
let inner = ty_inner_type(vec_inner, "Option").unwrap_or(vec_inner);
(inner, true)
} else {
(ty, false)
};

let field_name = if let Some(rename) = &field_attrs.rename {
quote! { #rename }
} else {
Expand All @@ -279,9 +289,16 @@ fn build_regular_field(field: &Field, field_attrs: &FieldAttributes) -> proc_mac
};

let typesense_field_type = if let Some(override_str) = &field_attrs.type_override {
quote! { #override_str }
quote! { ::std::string::String::from(#override_str) }
} else if is_vec {
quote! {
format!(
"{}[]",
<#ty as ::typesense::prelude::ToTypesenseField>::to_typesense_type()
)
}
} else {
quote! { <#ty as ::typesense::prelude::ToTypesenseField>::to_typesense_type() }
quote! { <#ty as ::typesense::prelude::ToTypesenseField>::to_typesense_type().to_owned() }
};

let optional = field_attrs
Expand Down
Loading