Search code examples
javarestsearch-engine

jirutka/rsql-parser : How to define an operator that has no argument? (=empty=, =notEmpty=)


I defined two new custom operators named =empty= and =notEmpty= just like I always have.

public abstract class RsqlParserOperators extends RSQLOperators {
...
    public static final ComparisonOperator LIKE = new ComparisonOperator("=like=", true);
...
    public static final ComparisonOperator EMPTY = new ComparisonOperator("=empty=", false);
    public static final ComparisonOperator NOT_EMPTY = new ComparisonOperator("=notEmpty=", false);

    public static Set<ComparisonOperator> operators() {
        Set<ComparisonOperator> set = defaultOperators();
...
        set.add(LIKE);
...
        set.add(EMPTY);
        set.add(NOT_EMPTY);
        return set;
    }
}

The peculiar thing about these new operators is that they wont need an argument.

When I tried to use one of them, for example with a filter like serie=empty=, I ended up with the following exception.

cz.jirutka.rsql.parser.ParseException: Encountered "<EOF>" at line 1, column 12.
Was expecting one of:
    <UNRESERVED_STR> ...
    <SINGLE_QUOTED_STR> ...
    <DOUBLE_QUOTED_STR> ...
    "(" ...

Any ideas bout how to define an argumentless operator?

Thanks.


Solution

  • I faced the exact same problem and ended up creating a new ComparisonOperator called IS_EMPTY.

    public static final ComparisonOperator IS_EMPTY = new ComparisonOperator("=isEmpty=", false);
    

    And it takes a single argument of true or false

    final Node rootNode = new RSQLParser(RsqlParserOperators.operators()).parse("subEntity=isEmpty=true");   
    

    Of course you still need to convert the argument from a String to a boolean in your converter.