330: Introduce the reset_sortable_fields Settings method r=irevoire a=Kerollmops

I forgot to add the `reset_sortable_fields` method on the `Settings` builder, it is no big deal as the library user (like MeiliSearch) can always call `set_sortable_fields` with an empty list of fields, it is equivalent.

Co-authored-by: Kerollmops <clement@meilisearch.com>
Co-authored-by: Tamo <tamo@meilisearch.com>
Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
This commit is contained in:
bors[bot] 2021-08-30 23:26:54 +00:00 committed by GitHub
commit df38794c7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 7 deletions

View File

@ -257,6 +257,9 @@ struct Settings {
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
filterable_attributes: Setting<HashSet<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
sortable_attributes: Setting<HashSet<String>>,
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
criteria: Setting<Vec<String>>,
@ -448,6 +451,15 @@ async fn main() -> anyhow::Result<()> {
Setting::NotSet => (),
}
// We transpose the settings JSON struct into a real setting update.
match settings.sortable_attributes {
Setting::Set(sortable_attributes) => {
builder.set_sortable_fields(sortable_attributes)
}
Setting::Reset => builder.reset_sortable_fields(),
Setting::NotSet => (),
}
// We transpose the settings JSON struct into a real setting update.
match settings.criteria {
Setting::Set(criteria) => builder.set_criteria(criteria),
@ -1029,6 +1041,7 @@ mod tests {
displayed_attributes: Setting::Set(vec!["name".to_string()]),
searchable_attributes: Setting::Set(vec!["age".to_string()]),
filterable_attributes: Setting::Set(hashset! { "age".to_string() }),
sortable_attributes: Setting::Set(hashset! { "age".to_string() }),
criteria: Setting::Set(vec!["age:asc".to_string()]),
stop_words: Setting::Set(btreeset! { "and".to_string() }),
synonyms: Setting::Set(hashmap! { "alex".to_string() => vec!["alexey".to_string()] }),
@ -1037,7 +1050,7 @@ mod tests {
assert_tokens(
&settings,
&[
Token::Struct { name: "Settings", len: 6 },
Token::Struct { name: "Settings", len: 7 },
Token::Str("displayedAttributes"),
Token::Some,
Token::Seq { len: Some(1) },
@ -1048,12 +1061,16 @@ mod tests {
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::SeqEnd,
Token::Str("facetedAttributes"),
Token::Str("filterableAttributes"),
Token::Some,
Token::Map { len: Some(1) },
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::Str("integer"),
Token::MapEnd,
Token::SeqEnd,
Token::Str("sortableAttributes"),
Token::Some,
Token::Seq { len: Some(1) },
Token::Str("age"),
Token::SeqEnd,
Token::Str("criteria"),
Token::Some,
Token::Seq { len: Some(1) },
@ -1083,6 +1100,7 @@ mod tests {
displayed_attributes: Setting::Reset,
searchable_attributes: Setting::Reset,
filterable_attributes: Setting::Reset,
sortable_attributes: Setting::Reset,
criteria: Setting::Reset,
stop_words: Setting::Reset,
synonyms: Setting::Reset,
@ -1091,12 +1109,14 @@ mod tests {
assert_tokens(
&settings,
&[
Token::Struct { name: "Settings", len: 6 },
Token::Struct { name: "Settings", len: 7 },
Token::Str("displayedAttributes"),
Token::None,
Token::Str("searchableAttributes"),
Token::None,
Token::Str("facetedAttributes"),
Token::Str("filterableAttributes"),
Token::None,
Token::Str("sortableAttributes"),
Token::None,
Token::Str("criteria"),
Token::None,
@ -1115,6 +1135,7 @@ mod tests {
displayed_attributes: Setting::NotSet,
searchable_attributes: Setting::NotSet,
filterable_attributes: Setting::NotSet,
sortable_attributes: Setting::NotSet,
criteria: Setting::NotSet,
stop_words: Setting::NotSet,
synonyms: Setting::NotSet,

View File

@ -141,6 +141,10 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
self.sortable_fields = Setting::Set(names);
}
pub fn reset_sortable_fields(&mut self) {
self.sortable_fields = Setting::Reset;
}
pub fn reset_criteria(&mut self) {
self.criteria = Setting::Reset;
}