Skip to content

Commit 44984db

Browse files
committed
feat: Allow =in= and =out= operators to have no arguments.
Closes: gh-9
1 parent 46e8a8f commit 44984db

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

src/main/java/cz/jirutka/rsql/parser/ast/RSQLOperators.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public abstract class RSQLOperators {
3838
GREATER_THAN_OR_EQUAL = new ComparisonOperator("=ge=", ">=", Arity.nary(1)),
3939
LESS_THAN = new ComparisonOperator("=lt=", "<", Arity.nary(1)),
4040
LESS_THAN_OR_EQUAL = new ComparisonOperator("=le=", "<=", Arity.nary(1)),
41-
IN = new ComparisonOperator("=in=", Arity.of(1, Integer.MAX_VALUE)),
42-
NOT_IN = new ComparisonOperator("=out=", Arity.of(1, Integer.MAX_VALUE)),
41+
IN = new ComparisonOperator("=in=", Arity.of(0, Integer.MAX_VALUE)),
42+
NOT_IN = new ComparisonOperator("=out=", Arity.of(0, Integer.MAX_VALUE)),
4343
IS_NULL = new ComparisonOperator("=null=", Arity.nary(0)),
4444
NOT_NULL = new ComparisonOperator("=notnull=", Arity.nary(0));
4545

src/main/javacc/RSQLParser.jj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,25 @@ List<String> Arguments():
206206
final Object value;
207207
}
208208
{
209-
( <LPAREN> value = CommaSepArguments() <RPAREN> ) { return (List) value; }
209+
( <LPAREN> value = OptionalCommaSepArguments() <RPAREN> ) { return (List) value; }
210210
|
211211
(value = Argument() { return Collections.singletonList((String) value); })?
212212
{
213213
return Collections.emptyList();
214214
}
215215
}
216216

217+
List<String> OptionalCommaSepArguments():
218+
{
219+
List<String> list = null;
220+
}
221+
{
222+
[ list = CommaSepArguments() ]
223+
{
224+
return list == null ? Collections.emptyList() : list;
225+
}
226+
}
227+
217228
List<String> CommaSepArguments():
218229
{
219230
final List<String> list = new ArrayList<String>(3);

src/test/groovy/cz/jirutka/rsql/parser/RSQLParserTest.groovy

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,46 @@ class RSQLParserTest extends Specification {
269269
ex.cause instanceof UnknownOperatorException
270270
}
271271

272+
def 'Should parse empty multi-argument operators'() {
273+
expect:
274+
parse(input) == expected
275+
276+
where:
277+
input | expected
278+
's0=in=()' | 'in'('s0')
279+
's0=in=( )' | 'in'('s0')
280+
's0=out=()' | out('s0')
281+
's0=out=( )' | out('s0')
282+
}
283+
284+
285+
def 'Should parse multi-argument operators with whitespaces values'() {
286+
expect:
287+
parse(input) == expected
288+
289+
where:
290+
input | expected
291+
"s0=in=(' ')" | 'in'('s0', ' ')
292+
"s0=in=(' ')" | 'in'('s0', ' ')
293+
"s0=in=(' ',' ')" | 'in'('s0', ' ', ' ')
294+
"s0=in=(' ' or ' ')" | 'in'('s0', ' ', ' ')
295+
"s0=in=(' ',' ',' ')" | 'in'('s0', ' ', ' ', ' ')
296+
"s0=out=(' ')" | 'out'('s0', ' ')
297+
"s0=out=(' ')" | 'out'('s0', ' ')
298+
"s0=out=(' ' or ' ')" | 'out'('s0', ' ', ' ')
299+
"s0=out=(' ',' ',' ')" | 'out'('s0', ' ', ' ', ' ')
300+
}
301+
302+
def 'Should throw when coma separated args contains only coma'() {
303+
when:
304+
parse(input)
305+
306+
then:
307+
thrown RSQLParserException
308+
309+
where:
310+
input << [ 's0=in=( , )', 's0=in=( or )']
311+
}
272312

273313
//////// Helpers ////////
274314

@@ -277,6 +317,7 @@ class RSQLParserTest extends Specification {
277317
def and(Node... nodes) { new AndNode(nodes as List) }
278318
def or(Node... nodes) { new OrNode(nodes as List) }
279319
def eq(sel, arg) { new ComparisonNode(EQUAL, sel, [arg as String]) }
320+
def 'in'(sel, ...args) { new ComparisonNode(IN, sel, args as List) }
280321
def out(sel, ...args) { new ComparisonNode(NOT_IN, sel, args as List) }
281322
def isNull(sel) { new ComparisonNode(IS_NULL, sel, []) }
282323
def notNull(sel) { new ComparisonNode(NOT_NULL, sel, []) }

src/test/groovy/cz/jirutka/rsql/parser/ast/RSQLOperatorsSpec.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class RSQLOperatorsSpec extends Specification {
1919
GREATER_THAN_OR_EQUAL | new String[]{'=ge=', '>='} | 1 | 1
2020
LESS_THAN | new String[]{'=lt=', '<'} | 1 | 1
2121
LESS_THAN_OR_EQUAL | new String[]{'=le=', '<='} | 1 | 1
22-
IN | new String[]{'=in='} | 1 | Integer.MAX_VALUE
23-
NOT_IN | new String[]{'=out='} | 1 | Integer.MAX_VALUE
22+
IN | new String[]{'=in='} | 0 | Integer.MAX_VALUE
23+
NOT_IN | new String[]{'=out='} | 0 | Integer.MAX_VALUE
2424
IS_NULL | new String[]{'=null='} | 0 | 0
2525
NOT_NULL | new String[]{'=notnull='} | 0 | 0
2626
}

0 commit comments

Comments
 (0)