Use serde-cs::CS with StarOr to reduce the logic duplication

This commit is contained in:
Kerollmops 2022-06-02 11:14:46 +02:00
parent 10d3b367dc
commit 64b5b2e1f8
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
3 changed files with 20 additions and 14 deletions

View File

@ -8,11 +8,13 @@ use meilisearch_lib::index::{
};
use meilisearch_lib::MeiliSearch;
use serde::Deserialize;
use serde_cs::vec::CS;
use serde_json::Value;
use crate::analytics::{Analytics, SearchAggregator};
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::extractors::sequential_extractor::SeqHandler;
use crate::routes::{fold_star_or, StarOr};
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(
@ -28,16 +30,16 @@ pub struct SearchQueryGet {
q: Option<String>,
offset: Option<usize>,
limit: Option<usize>,
attributes_to_retrieve: Option<String>,
attributes_to_crop: Option<String>,
attributes_to_retrieve: Option<CS<StarOr<String>>>,
attributes_to_crop: Option<CS<StarOr<String>>>,
#[serde(default = "DEFAULT_CROP_LENGTH")]
crop_length: usize,
attributes_to_highlight: Option<String>,
attributes_to_highlight: Option<CS<StarOr<String>>>,
filter: Option<String>,
sort: Option<String>,
#[serde(default = "Default::default")]
show_matches_position: bool,
facets: Option<String>,
facets: Option<CS<StarOr<String>>>,
#[serde(default = "DEFAULT_HIGHLIGHT_PRE_TAG")]
highlight_pre_tag: String,
#[serde(default = "DEFAULT_HIGHLIGHT_POST_TAG")]
@ -50,19 +52,20 @@ impl From<SearchQueryGet> for SearchQuery {
fn from(other: SearchQueryGet) -> Self {
let attributes_to_retrieve = other
.attributes_to_retrieve
.map(|attrs| attrs.split(',').map(String::from).collect());
.map(CS::into_inner)
.and_then(fold_star_or);
let attributes_to_crop = other
.attributes_to_crop
.map(|attrs| attrs.split(',').map(String::from).collect());
.map(CS::into_inner)
.and_then(fold_star_or);
let attributes_to_highlight = other
.attributes_to_highlight
.map(|attrs| attrs.split(',').map(String::from).collect());
.map(CS::into_inner)
.and_then(fold_star_or);
let facets = other
.facets
.map(|attrs| attrs.split(',').map(String::from).collect());
let facets = other.facets.map(CS::into_inner).and_then(fold_star_or);
let filter = match other.filter {
Some(f) => match serde_json::from_str(&f) {

View File

@ -49,7 +49,10 @@ impl<T: FromStr> FromStr for StarOr<T> {
/// Extracts the raw values from the `StarOr` types and
/// return None if a `StarOr::Star` is encountered.
pub fn fold_star_or<T>(content: impl IntoIterator<Item = StarOr<T>>) -> Option<Vec<T>> {
pub fn fold_star_or<T, O>(content: impl IntoIterator<Item = StarOr<T>>) -> Option<O>
where
O: FromIterator<T>,
{
content
.into_iter()
.map(|value| match value {

View File

@ -81,9 +81,9 @@ async fn get_tasks(
// We first transform a potential indexUid=* into a "not specified indexUid filter"
// for every one of the filters: type, status, and indexUid.
let type_ = type_.map(CS::into_inner).and_then(fold_star_or);
let status = status.map(CS::into_inner).and_then(fold_star_or);
let index_uid = index_uid.map(CS::into_inner).and_then(fold_star_or);
let type_: Option<Vec<_>> = type_.map(CS::into_inner).and_then(fold_star_or);
let status: Option<Vec<_>> = status.map(CS::into_inner).and_then(fold_star_or);
let index_uid: Option<Vec<_>> = index_uid.map(CS::into_inner).and_then(fold_star_or);
// Then we filter on potential indexes and make sure that the search filter
// restrictions are also applied.