Skip to content

Commit

Permalink
Fix ugly code
Browse files Browse the repository at this point in the history
  • Loading branch information
guilload committed Dec 21, 2023
1 parent 2074c2a commit ef638fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
34 changes: 14 additions & 20 deletions quickwit/quickwit-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::str::FromStr;

use anyhow::{bail, Context};
use json_comments::StripComments;
use once_cell::sync::OnceCell;
use once_cell::sync::Lazy;
use quickwit_common::net::is_valid_hostname;
use quickwit_common::uri::Uri;
use regex::Regex;
Expand Down Expand Up @@ -105,12 +105,10 @@ pub struct ConfigApiSchemas;

/// Checks whether an identifier conforms to Quickwit object naming conventions.
pub fn validate_identifier(label: &str, value: &str) -> anyhow::Result<()> {
static IDENTIFIER_REGEX: OnceCell<Regex> = OnceCell::new();

if IDENTIFIER_REGEX
.get_or_init(|| Regex::new(r"^[a-zA-Z][a-zA-Z0-9-_\.]{2,254}$").expect("Failed to compile regular expression. This should never happen! Please, report on https://github.com/quickwit-oss/quickwit/issues."))
.is_match(value)
{
static IDENTIFIER_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^[a-zA-Z][a-zA-Z0-9-_\.]{2,254}$").expect("regular expression should compile")
});
if IDENTIFIER_REGEX.is_match(value) {
return Ok(());
}
bail!(
Expand All @@ -123,34 +121,30 @@ pub fn validate_identifier(label: &str, value: &str) -> anyhow::Result<()> {
/// Index ID patterns accept the same characters as identifiers AND accept `*`
/// chars to allow for glob-like patterns.
pub fn validate_index_id_pattern(pattern: &str) -> anyhow::Result<()> {
static IDENTIFIER_REGEX_WITH_GLOB_PATTERN: OnceCell<Regex> = OnceCell::new();

if !IDENTIFIER_REGEX_WITH_GLOB_PATTERN
.get_or_init(|| Regex::new(r"^[a-zA-Z\*][a-zA-Z0-9-_\.\*]{0,254}$").expect("Failed to compile regular expression. This should never happen! Please, report on https://github.com/quickwit-oss/quickwit/issues."))
.is_match(pattern)
{
static IDENTIFIER_REGEX_WITH_GLOB_PATTERN: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^[a-zA-Z\*][a-zA-Z0-9-_\.\*]{0,254}$")
.expect("regular expression should compile")
});
if !IDENTIFIER_REGEX_WITH_GLOB_PATTERN.is_match(pattern) {
bail!(
"index ID pattern `{pattern}` is invalid. patterns must match the following regular \
"index ID pattern `{pattern}` is invalid: patterns must match the following regular \
expression: `^[a-zA-Z\\*][a-zA-Z0-9-_\\.\\*]{{0,254}}$`"
);
}

// Forbid multiple stars in the pattern to force the user making simpler patterns
// as multiple stars does not bring any value.
if pattern.contains("**") {
bail!(
"index ID pattern `{pattern}` is invalid. patterns must not contain multiple \
"index ID pattern `{pattern}` is invalid: patterns must not contain multiple \
consecutive `*`"
);
}

// If there is no star in the pattern, we need at least 3 characters.
if !pattern.contains('*') && pattern.len() < 3 {
bail!(
"index ID pattern `{pattern}` is invalid. an index ID must have at least 3 characters"
"index ID pattern `{pattern}` is invalid: an index ID must have at least 3 characters"
);
}

Ok(())
}

Expand Down Expand Up @@ -283,6 +277,6 @@ mod tests {
assert!(validate_index_id_pattern("foo!")
.unwrap_err()
.to_string()
.contains("index ID pattern `foo!` is invalid."));
.contains("index ID pattern `foo!` is invalid:"));
}
}
31 changes: 18 additions & 13 deletions quickwit/quickwit-serve/src/search_api/rest_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use std::convert::TryFrom;
use std::sync::Arc;

use anyhow::Context;
use futures::stream::StreamExt;
use hyper::header::HeaderValue;
use hyper::HeaderMap;
Expand Down Expand Up @@ -57,21 +56,27 @@ use crate::{with_arg, BodyFormat};
pub struct SearchApi;

pub(crate) async fn extract_index_id_patterns(
comma_separated_index_patterns: String,
comma_separated_index_id_patterns: String,
) -> Result<Vec<String>, Rejection> {
let index_pattern = percent_decode_str(&comma_separated_index_patterns)
.decode_utf8()
.context("index pattern does not contain valid utf8 characters")
.map_err(|error| crate::rest::InvalidArgument(error.to_string()))?;

let mut index_ids_patterns = Vec::new();
for index_id_pattern in index_pattern.split(',').collect::<Vec<_>>() {
let percent_decoded_comma_separated_index_id_patterns =
percent_decode_str(&comma_separated_index_id_patterns)
.decode_utf8()
.map_err(|error| {
let message = format!(
"failed to percent decode comma-separated index ID patterns \
`{comma_separated_index_id_patterns}`: {error}"
);
crate::rest::InvalidArgument(message)
})?;
let mut index_id_patterns = Vec::new();

for index_id_pattern in percent_decoded_comma_separated_index_id_patterns.split(',') {
validate_index_id_pattern(index_id_pattern)
.map_err(|error| crate::rest::InvalidArgument(error.to_string()))?;
index_ids_patterns.push(index_id_pattern.to_string());
index_id_patterns.push(index_id_pattern.to_string());
}
assert!(!index_ids_patterns.is_empty());
Ok(index_ids_patterns)
assert!(!index_id_patterns.is_empty());
Ok(index_id_patterns)
}

#[derive(Debug, Default, Eq, PartialEq, Deserialize, utoipa::ToSchema)]
Expand Down Expand Up @@ -654,7 +659,7 @@ mod tests {
.unwrap();
assert_eq!(
rejection.0,
"index ID pattern `quickwit-demo-index**` is invalid. patterns must not contain \
"index ID pattern `quickwit-demo-index**` is invalid: patterns must not contain \
multiple consecutive `*`"
);
}
Expand Down

0 comments on commit ef638fd

Please sign in to comment.