Skip to content

Commit

Permalink
feat: Add support for nested queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jensborch committed Aug 29, 2024
1 parent 3b3405f commit edc004a
Show file tree
Hide file tree
Showing 29 changed files with 696 additions and 115 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
= Changelog
:repo-uri: https://github.com/jirutka/rsql-parser
:repo-uri: https://github.com/jensborch/rsql-parser
:issue-uri: {repo-uri}/issues

== 2.4.0 (2024-08-29)
* Added support for nested queries

== 2.3.2 (2024-05-01)
* Avoid unnecessary copying ComparisonNode or it's arguments.

Expand Down
23 changes: 16 additions & 7 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
= RSQL / FIQL parser
Jakub Jirutka <https://github.com/jirutka[@jirutka]>
:name: rsql-parser
:version: 2.3.2
:mvn-group: io.github.nstdio
:gh-name: nstdio/{name}
:version: 2.4.0
:mvn-group: com.github.jensborch
:gh-name: jensborch/{name}
:gh-branch: master
:src-base: link:src/main/java/cz/jirutka/rsql/parser

ifdef::env-github[]
image:https://github.com/{gh-name}/actions/workflows/build.yaml/badge.svg["Build", link="https://github.com/nstdio/rsql-parser/actions/workflows/build.yaml"]
image:https://codecov.io/github/nstdio/rsql-parser/branch/master/graph/badge.svg?token=L13HXQDRYY["codecov", link="https://codecov.io/github/nstdio/rsql-parser"]
image:https://github.com/{gh-name}/actions/workflows/build.yml/badge.svg["Build", link="https://github.com/{gh-name}/actions/workflows/build.yml"]
image:https://codecov.io/github/{gh-name}/branch/master/graph/badge.svg?token=L13HXQDRYY["codecov", link="https://codecov.io/github/{gh-name}"]
image:https://maven-badges.herokuapp.com/maven-central/{mvn-group}/{name}/badge.svg["Maven Central", link="https://maven-badges.herokuapp.com/maven-central/{mvn-group}/{name}"]
endif::env-github[]

:important-caption: ❗

IMPORTANT: *This is a fork of the original https://github.com/jirutka/rsql-parser[RSQL parser] with added support for nested queries. If your project doesn't require this feature, it's advisable to use https://github.com/nstdio/rsql-parser[this maintained fork] instead.*

RSQL is a query language for parametrized filtering of entries in RESTful APIs.
It’s based on http://tools.ietf.org/html/draft-nottingham-atompub-fiql-00[FIQL] (Feed Item Query Language) – an URI-friendly syntax for expressing filters across the entries in an Atom Feed.
FIQL is great for use in URI; there are no unsafe characters, so URL encoding is not required.
Expand Down Expand Up @@ -96,11 +100,16 @@ comp-fiql = ( ( "=", { ALPHA } ) | "!" ), "=";
comp-alt = ( ">" | "<" ), [ "=" ];
----

Argument can be no value, single value, or multiple values in parenthesis separated by comma.
Argument can be no value, single value, multiple values in parenthesis separated by comma, or a nested query in parenthesis.

Value that doesn’t contain any reserved character or a white space can be unquoted, other arguments must be enclosed in single or double quotes.

Nested queries are not supported for any of the build ind comparison operators, but can be used when extending RSQL with custom operators.

----
arguments = ( "(", ( value, { "," , value }, )? ")" ) | ( value )?;
arguments = ( "(", ( value, { "," , value }, )? ")" ) |
( value )? |
( "(", comparison, { or, comparison } ")" );
value = unreserved-str | double-quoted | single-quoted;
unreserved-str = unreserved, { unreserved }
Expand Down
24 changes: 16 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
javacc("net.java.dev.javacc:javacc:7.0.13")
}

group = "io.github.nstdio"
group = "com.github.jensborch"
description = "RSQL-parser"

java {
Expand All @@ -51,7 +51,7 @@ publishing {
pom {
name.set("RSQL-parser")
description.set("Parser of RSQL / FIQL (query language for RESTful APIs) written in JavaCC.")
url.set("https://github.com/nstdio/rsql-parser")
url.set("https://github.com/jensborch/rsql-parser")
inceptionYear.set("2011")

licenses {
Expand All @@ -73,22 +73,27 @@ publishing {
name.set("Edgar Asatryan")
email.set("nstdio@gmail.com")
}
developer {
id.set("jensborch")
name.set("Jens Borch Christiansen")
email.set("jens.borch@gmail.com")
}
}

scm {
connection.set("scm:git:git@github.com:nstdio/rsql-parser.git")
developerConnection.set("scm:git:git@github.com:nstdio/rsql-parser.git")
url.set("https://github.com/nstdio/rsql-parser")
connection.set("scm:git:git@github.com:jensborch/rsql-parser.git")
developerConnection.set("scm:git:git@github.com:jensborch/rsql-parser.git")
url.set("https://github.com/jensborch/rsql-parser")
}

ciManagement {
system.set("GitHub Actions")
url.set("https://github.com/nstdio/rsql-parser/actions")
url.set("https://github.com/jensborch/rsql-parser/actions")
}

issueManagement {
system.set("GitHub Issues")
url.set("https://github.com/nstdio/rsql-parser/issues")
url.set("https://github.com/jensborch/rsql-parser/issues")
}
}
}
Expand All @@ -107,7 +112,7 @@ signing {
nexusPublishing {
repositories {
sonatype {
val baseUri = uri("https://s01.oss.sonatype.org")
val baseUri = uri("https://oss.sonatype.org")
nexusUrl.set(baseUri.resolve("/service/local/"))
snapshotRepositoryUrl.set(baseUri.resolve("/content/repositories/snapshots/"))
}
Expand All @@ -122,6 +127,8 @@ release {
}
}



tasks {
test {
useJUnitPlatform()
Expand Down Expand Up @@ -167,3 +174,4 @@ tasks {
dependsOn("publishToSonatype", "closeAndReleaseSonatypeStagingRepository")
}
}

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.3.3-SNAPSHOT
version=2.4.0-SNAPSHOT
3 changes: 1 addition & 2 deletions src/main/java/cz/jirutka/rsql/parser/RSQLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* comp-fiql = ( ( "=", { ALPHA } ) | "!" ), "=";
* comp-alt = ( ">" | "<" ), [ "=" ];
*
* arguments = ( "(", ( value, { "," , value }, )? ")" ) | ( value )?;
* arguments = ( "(", ( value, { "," , value }, )? ")" ) | ( value )? | ( "(", comparison, { or, comparison } ")" );
* value = unreserved-str | double-quoted | single-quoted;
*
* unreserved-str = unreserved, { unreserved }
Expand Down Expand Up @@ -124,7 +124,6 @@ public Node parse(String query) throws RSQLParserException {
try (Reader reader = new StringReader(query)) {
Parser parser = new Parser(reader, nodesFactory);
return parser.Input();

} catch (Exception | TokenMgrError ex) {
throw new RSQLParserException(ex);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/RSQLParserException.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@
*/
public class RSQLParserException extends RuntimeException {

/**
* Constructs a new RSQLParserException with the specified detail message and cause.
*
* @param cause cause of the exception
*/
public RSQLParserException(Throwable cause) {
super(cause);
}

/**
* Constructs a new RSQLParserException with the specified detail message.
*
* @param message the detail message
*/
public RSQLParserException(String message) {
super(message);
}

}
15 changes: 15 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/ast/AbstractNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* The MIT License
*
* Copyright 2013-2014 Jakub Jirutka <jakub@jirutka.cz>.
* Copyright 2024 Jens Borch Christiansen <jens.borch@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,12 +26,26 @@

public abstract class AbstractNode implements Node {

private int nestingLevel;


/**
* Accepts the visitor, calls its visit() method and returns the result.
* This method just calls {@link #accept(RSQLVisitor, Object)} with
* <tt>null</tt> as the second argument.
*/
@Override
public <R, A> R accept(RSQLVisitor<R, A> visitor) {
return accept(visitor, null);
}

@Override
public int getNestingLevel() {
return nestingLevel;
}

@Override
public void setNestingLevel(int nestingLevel) {
this.nestingLevel = nestingLevel;
}
}
12 changes: 12 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/ast/Arity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ static Arity of(int min, int max) {
static Arity nary(int n) {
return new NAry(n);
}

/**
* Creates nullary object.
*
* @return the created arity
*/
static Arity nullary() {
return new NullAry();
}



}
50 changes: 50 additions & 0 deletions src/main/java/cz/jirutka/rsql/parser/ast/ComparisonArguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright 2024 Jens Borch Christiansen <jens.borch@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package cz.jirutka.rsql.parser.ast;

import java.util.List;

/**
* Interface representing arguments in the RSQL grammar.
*/
public interface ComparisonArguments {

boolean isNested();

/**
* Returns arguments as a list of string, if {@link #isNested()} is true the
* list will be empty.
*
* @return a list of string arguments
*/
List<String> asStringList();

/**
* Returns arguments as a node, if {@link #isNested()} is false this will
* return null.
*
* @return arguments as a node or null
*/
Node asNode();
}
Loading

0 comments on commit edc004a

Please sign in to comment.