Verified Commit a5f50cfd authored by Himanshu Kapoor's avatar Himanshu Kapoor
Browse files

feat(parser): add support for <= and >= operators

For dates, add support for <= and >= operators that aim to standardize
inclusivity/exclusivity of the specified date.
parent 41c1891a
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -151,7 +151,7 @@ exports[`graphql: compiles 'label != "backend" and author = currentUser() and we
    issues(
      authorUsername: "foo"
      weight: "1"
      updatedAfter: "2024-01-01"
      updatedAfter: "2024-01-01 23:59"
      not: {labelName: "backend"}
      first: 10
      includeSubgroups: true
@@ -199,7 +199,7 @@ exports[`graphql: compiles 'label in ("devops::plan", "devops::create")' with de
exports[`graphql: compiles 'updated > today()' with default fields: ['id', 'title'] 1`] = `
"query GLQL {
  group(fullPath: "gitlab-org") {
    issues(updatedAfter: "2024-01-01", first: 10, includeSubgroups: true) {
    issues(updatedAfter: "2024-01-01 23:59", first: 10, includeSubgroups: true) {
      nodes {
        id
        title
@@ -221,7 +221,7 @@ exports[`graphql: compiles 'weight = 1 and updated > startOfDay("-7")' with defa
  group(fullPath: "gitlab-org") {
    issues(
      weight: "1"
      updatedAfter: "2023-12-25"
      updatedAfter: "2023-12-25 23:59"
      first: 10
      includeSubgroups: true
    ) {
@@ -1013,7 +1013,7 @@ exports[`graphql: compiles with cursor after 1`] = `
  group(fullPath: "gitlab-org") {
    issues(
      weight: "1"
      updatedAfter: "2023-12-25"
      updatedAfter: "2023-12-25 23:59"
      after: "eyJpZCI6IjEifQ=="
      first: 10
      includeSubgroups: true
@@ -1039,7 +1039,7 @@ exports[`graphql: compiles with cursor before 1`] = `
  group(fullPath: "gitlab-org") {
    issues(
      weight: "1"
      updatedAfter: "2023-12-25"
      updatedAfter: "2023-12-25 23:59"
      before: "eyJpZCI6IjEifQ=="
      first: 10
      includeSubgroups: true
+9 −1
Original line number Diff line number Diff line
@@ -64,7 +64,15 @@ impl FieldType {
            Nullable | StringLike | NumberLike | BooleanLike | EnumLike(_) | StringEnumLike(_)
            | ReferenceLike(..) => vec![Equal, NotEqual].into_iter().collect(),
            WithOperators(_, ops) => ops.clone().into_iter().collect(),
            DateLike => vec![Equal, GreaterThan, LessThan].into_iter().collect(),
            DateLike => vec![
                Equal,
                GreaterThan,
                GreaterThanEquals,
                LessThan,
                LessThanEquals,
            ]
            .into_iter()
            .collect(),
            ListLike(HasOne, _) => vec![In, NotEqual].into_iter().collect(),
            ListLike(HasMany, _) => vec![In, Equal, NotEqual].into_iter().collect(),
            PairedWith(f, _) => f.operators(),
+4 −4
Original line number Diff line number Diff line
@@ -142,7 +142,7 @@ pub fn to_graphql_components(query: &Query, context: &Context) -> GraphQLCompone
        let value = to_graphql_value(expr);

        match expr.operator {
            Equal | GreaterThan | LessThan => {
            Equal | GreaterThan | LessThan | GreaterThanEquals | LessThanEquals => {
                let mut and_component = IndexMap::new();
                and_component.insert(attribute, value);
                components.and.push(and_component);
@@ -278,8 +278,8 @@ fn to_graphql_attribute(expr: &Expression, context: &Context) -> String {
        }
        (Group, _, _) => panic!("group interpreted as token instead of captured by optimizer"),
        (Weight, _, Null | Token(_)) => "weightWildcardId".to_string(),
        (_, GreaterThan, _) => format!("{}After", expr.field),
        (_, LessThan, _) => format!("{}Before", expr.field),
        (_, GreaterThan | GreaterThanEquals, _) => format!("{}After", expr.field),
        (_, LessThan | LessThanEquals, _) => format!("{}Before", expr.field),
        (Unknown(f), _, _) => match f.as_str() {
            "limit" => "first".to_string(),
            "reaction" => "myReactionEmoji".to_string(),
@@ -484,7 +484,7 @@ mod tests {
                let value = to_graphql_value(expr);

                match expr.operator {
                    Equal | GreaterThan | LessThan => {
                    Equal | GreaterThan | LessThan | LessThanEquals | GreaterThanEquals => {
                        valid &= components.and.iter().any(|map| map.get(&attribute) == Some(&value));
                    }
                    NotEqual => {
+4 −0
Original line number Diff line number Diff line
@@ -24,7 +24,9 @@ fn operator(input: &str) -> IResult<&str, Operator, GlqlError> {
        ),
        map(terminated(char('='), multispace0), |_| Equal),
        map(terminated(tag("!="), multispace0), |_| NotEqual),
        map(terminated(tag(">="), multispace0), |_| GreaterThanEquals),
        map(terminated(char('>'), multispace0), |_| GreaterThan),
        map(terminated(tag("<="), multispace0), |_| LessThanEquals),
        map(terminated(char('<'), multispace0), |_| LessThan),
    ))(input)
    .map_err(|_| nom::Err::Failure(GlqlError::parse_error(input, InvalidOperator)))
@@ -85,6 +87,8 @@ mod tests {
        test_parser_result(operator, "!= rest", NotEqual);
        test_parser_result(operator, "> rest", GreaterThan);
        test_parser_result(operator, "< rest", LessThan);
        test_parser_result(operator, ">= rest", GreaterThanEquals);
        test_parser_result(operator, "<= rest", LessThanEquals);
        test_parser_error(operator, "invalid");
        test_parser_error(operator, "123");
        test_parser_error(operator, "");
+8 −6
Original line number Diff line number Diff line
use crate::types::Operator;
use crate::types::Operator::{self, *};
use crate::types::Query;

use crate::types::Value;

fn get_op_str(opr: Operator) -> &'static str {
    match opr {
        Operator::Equal => "=",
        Operator::GreaterThan => ">",
        Operator::In => "in",
        Operator::LessThan => "<",
        Operator::NotEqual => "!=",
        Equal => "=",
        GreaterThan => ">",
        GreaterThanEquals => ">=",
        In => "in",
        LessThan => "<",
        LessThanEquals => "<=",
        NotEqual => "!=",
    }
}

Loading