Skip to content

Commit

Permalink
with_distrinct_attribute accepts None
Browse files Browse the repository at this point in the history
  • Loading branch information
NoodleSamaChan committed Apr 21, 2024
1 parent 76fb43f commit 01bcdf5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ update_settings_1: |-
"release_date:desc",
"rank:desc"
])
.with_distinct_attribute("movie_id")
.with_distinct_attribute(Some("movie_id"))
.with_searchable_attributes([
"title",
"overview",
Expand Down
19 changes: 18 additions & 1 deletion meilisearch-index-setting-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::format;

use convert_case::{Case, Casing};
use proc_macro2::Ident;
use quote::quote;
Expand Down Expand Up @@ -124,7 +126,7 @@ fn get_index_config_implementation(
let searchable_attr_tokens =
get_settings_token_for_list(&searchable_attributes, "with_searchable_attributes");
let distinct_attr_token =
get_settings_token_for_string(&distinct_key_attribute, "with_distinct_attribute");
get_settings_token_for_string_for_some_string(&distinct_key_attribute, "with_distinct_attribute");

quote! {
#[::meilisearch_sdk::macro_helper::async_trait(?Send)]
Expand Down Expand Up @@ -187,3 +189,18 @@ fn get_settings_token_for_string(
}
}
}

fn get_settings_token_for_string_for_some_string(
field_name: &String,
method_name: &str,
) -> proc_macro2::TokenStream {
let method_ident = Ident::new(method_name, proc_macro2::Span::call_site());

if field_name.is_empty() {
proc_macro2::TokenStream::new()
} else {
quote! {
.#method_ident(::std::option::Option::Some(#field_name))
}
}
}
20 changes: 14 additions & 6 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct Settings {
pub sortable_attributes: Option<Vec<String>>,
/// Search returns documents with distinct (different) values of the given field.
#[serde(skip_serializing_if = "Option::is_none")]
pub distinct_attribute: Option<String>,
pub distinct_attribute: Option<Option<String>>,
/// Fields in which to search for matching query words sorted by order of importance.
#[serde(skip_serializing_if = "Option::is_none")]
pub searchable_attributes: Option<Vec<String>>,
Expand Down Expand Up @@ -217,11 +217,19 @@ impl Settings {
}

#[must_use]
pub fn with_distinct_attribute(self, distinct_attribute: impl AsRef<str>) -> Settings {
Settings {
distinct_attribute: Some(distinct_attribute.as_ref().to_string()),
..self
}
pub fn with_distinct_attribute(self, distinct_attribute: Option< impl AsRef<str>>) -> Settings {
if let Some(distinct_attribute) = distinct_attribute {
Settings {
distinct_attribute: Some(Some(distinct_attribute.as_ref().to_string())),
..self
}
} else {
Settings {
distinct_attribute: Some(None),
..self
}

}
}

#[must_use]
Expand Down

0 comments on commit 01bcdf5

Please sign in to comment.