Skip to content

Commit

Permalink
Add explicit support for booleans in search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ackwell committed Jul 21, 2024
1 parent 9af0e9a commit 51f22c9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/http/api1/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ use super::error;
/// - exact equality: `key=value`
///
/// - numeric comparison: `key>=value`, `key>value`, `key<=value`, `key<value`
///
/// Supported value types:
///
/// - string: `"example"`
///
/// - number: `1`, `-1`, `1.0`
///
/// - boolean: `true`, `false`
#[derive(Debug, JsonSchema)]
pub struct QueryString(#[schemars(with = "String")] query::Node);

Expand Down Expand Up @@ -158,11 +166,16 @@ fn operation(input: &str) -> ParseResult<query::Operation> {

fn value(input: &str) -> ParseResult<query::Value> {
alt((
map(boolean, query::Value::Boolean),
map(number, query::Value::Number),
map(string, query::Value::String),
))(input)
}

fn boolean(input: &str) -> ParseResult<bool> {
alt((nom_value(true, tag("true")), nom_value(false, tag("false"))))(input)
}

fn number(input: &str) -> ParseResult<query::Number> {
alt((
// Try to parse the number as a potentially-signed integer. If it's followed by `.`, it'll fall through to the float check.
Expand Down Expand Up @@ -403,6 +416,22 @@ mod test {
);
}

#[test]
fn booleans() {
fn harness(value: bool) -> query::Node {
group(vec![(
query::Occur::Should,
leaf(
field_struct("A"),
query::Operation::Eq(query::Value::Boolean(value)),
),
)])
}

assert_eq!(test_parse("A=true"), harness(true));
assert_eq!(test_parse("A=false"), harness(false));
}

#[test]
fn number_types() {
fn harness(number: query::Number) -> query::Node {
Expand Down
1 change: 1 addition & 0 deletions src/search/query/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Relation<F, T> {

#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Boolean(bool),
Number(Number),
String(String),
}
Expand Down
3 changes: 2 additions & 1 deletion src/search/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ fn table_alias(alias_base: &str, language: Language) -> Alias {
impl From<post::Value> for sea_query::Value {
fn from(value: post::Value) -> Self {
match value {
post::Value::Boolean(value) => sea_query::Value::from(value),
post::Value::Number(value) => sea_query::Value::from(value),
post::Value::String(value) => sea_query::Value::String(Some(value.into())),
post::Value::String(value) => sea_query::Value::from(value),
}
}
}
Expand Down

0 comments on commit 51f22c9

Please sign in to comment.