Skip to content

Commit 01bcdf5

Browse files
with_distrinct_attribute accepts None
1 parent 76fb43f commit 01bcdf5

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

.code-samples.meilisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ update_settings_1: |-
315315
"release_date:desc",
316316
"rank:desc"
317317
])
318-
.with_distinct_attribute("movie_id")
318+
.with_distinct_attribute(Some("movie_id"))
319319
.with_searchable_attributes([
320320
"title",
321321
"overview",

meilisearch-index-setting-macro/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::format;
2+
13
use convert_case::{Case, Casing};
24
use proc_macro2::Ident;
35
use quote::quote;
@@ -124,7 +126,7 @@ fn get_index_config_implementation(
124126
let searchable_attr_tokens =
125127
get_settings_token_for_list(&searchable_attributes, "with_searchable_attributes");
126128
let distinct_attr_token =
127-
get_settings_token_for_string(&distinct_key_attribute, "with_distinct_attribute");
129+
get_settings_token_for_string_for_some_string(&distinct_key_attribute, "with_distinct_attribute");
128130

129131
quote! {
130132
#[::meilisearch_sdk::macro_helper::async_trait(?Send)]
@@ -187,3 +189,18 @@ fn get_settings_token_for_string(
187189
}
188190
}
189191
}
192+
193+
fn get_settings_token_for_string_for_some_string(
194+
field_name: &String,
195+
method_name: &str,
196+
) -> proc_macro2::TokenStream {
197+
let method_ident = Ident::new(method_name, proc_macro2::Span::call_site());
198+
199+
if field_name.is_empty() {
200+
proc_macro2::TokenStream::new()
201+
} else {
202+
quote! {
203+
.#method_ident(::std::option::Option::Some(#field_name))
204+
}
205+
}
206+
}

src/settings.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub struct Settings {
8181
pub sortable_attributes: Option<Vec<String>>,
8282
/// Search returns documents with distinct (different) values of the given field.
8383
#[serde(skip_serializing_if = "Option::is_none")]
84-
pub distinct_attribute: Option<String>,
84+
pub distinct_attribute: Option<Option<String>>,
8585
/// Fields in which to search for matching query words sorted by order of importance.
8686
#[serde(skip_serializing_if = "Option::is_none")]
8787
pub searchable_attributes: Option<Vec<String>>,
@@ -217,11 +217,19 @@ impl Settings {
217217
}
218218

219219
#[must_use]
220-
pub fn with_distinct_attribute(self, distinct_attribute: impl AsRef<str>) -> Settings {
221-
Settings {
222-
distinct_attribute: Some(distinct_attribute.as_ref().to_string()),
223-
..self
224-
}
220+
pub fn with_distinct_attribute(self, distinct_attribute: Option< impl AsRef<str>>) -> Settings {
221+
if let Some(distinct_attribute) = distinct_attribute {
222+
Settings {
223+
distinct_attribute: Some(Some(distinct_attribute.as_ref().to_string())),
224+
..self
225+
}
226+
} else {
227+
Settings {
228+
distinct_attribute: Some(None),
229+
..self
230+
}
231+
232+
}
225233
}
226234

227235
#[must_use]

0 commit comments

Comments
 (0)