Skip to content

Commit

Permalink
Add gradle configurations for jsonpath operation
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Mar 21, 2024
1 parent cb140d3 commit 4a0ab9a
Show file tree
Hide file tree
Showing 17 changed files with 686 additions and 333 deletions.
3 changes: 3 additions & 0 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ task updateTomlFiles {
doLast {
def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version)
newConfig = newConfig.replace("@toml.version@", tomlVersion)
newConfig = newConfig.replace("@jsonpath.version@", project.javaJsonPathVersion)
newConfig = newConfig.replace("@jsonsmart.version@", project.javaJsonSmartVersion)
newConfig = newConfig.replace("@accessors.version@", project.javaAccessorsSmartVersion)
ballerinaTomlFile.text = newConfig

def newCompilerPluginToml = compilerPluginTomlFilePlaceHolder.text.replace("@project.version@", project.version)
Expand Down
3 changes: 2 additions & 1 deletion ballerina/errors.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
// specific language governing permissions and limitations
// under the License.

# Represents any error related to Ballerina JsonPath module
# Represents the error type of the ballerina/data.jsondata module. This error type represents any error that can occur
# during the execution of jsondata APIs.
public type Error distinct error;
2 changes: 1 addition & 1 deletion ballerina/init.bal
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ isolated function init() {
}

isolated function setModule() = @java:Method {
'class: "io.ballerina.lib.data.jsondata.utils.ModuleUtils"
'class: "io.ballerina.lib.data.ModuleUtils"
} external;
4 changes: 0 additions & 4 deletions ballerina/json_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public type Options record {|
boolean allowDataProjection = true;
|};

# Represents the error type of the ballerina/data.jsondata module. This error type represents any error that can occur
# during the execution of jsondata APIs.
public type Error distinct error;

# Defines the name of the JSON Object key.
#
# + value - The name of the JSON Object key
Expand Down
2 changes: 1 addition & 1 deletion ballerina/jsonpath.bal → ballerina/read.bal
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ public class JsonPathRawTemplateImpl {
# + query - JSON path expression
# + return - extracted details as JSON value, a jsonpath:Error otherwise
public isolated function readJson(json 'json, JsonPathRawTemplateImpl query) returns json|Error = @java:Method {
'class: "io.ballerina.stdlib.jsonpath.BJsonPath"
'class: "io.ballerina.lib.data.jsonpath.BJsonPath"
} external;
51 changes: 35 additions & 16 deletions ballerina/tests/jsonpath_complex_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ function testSelectElementByNumericalConditionalExpression() returns error? {
JsonPathRawTemplate condition7 = `$[?(@.a2 > 1.1)]`;
JsonPathRawTemplate condition8 = `$[?(@.a1 == 2.34)]`;
JsonPathRawTemplate condition9 = `$[?(@.a1 == 12.34)]`;
JsonPathRawTemplate condition10 = `$[?(@.a1 != 12.34)]`;
JsonPathRawTemplate condition11 = `$[?(@.a1 <= ${d1} && @.a1 > ${i1})]`;

json result = check read([decimalJson, floatJson, intJson], condition1);
test:assertTrue(result is json[]);
Expand Down Expand Up @@ -552,6 +554,14 @@ function testSelectElementByNumericalConditionalExpression() returns error? {
result = check read([decimalJson, floatJson, intJson], condition9);
test:assertTrue(result is json[]);
test:assertEquals(result, <json[]>[]);

result = check read([decimalJson, floatJson, intJson], condition10);
test:assertTrue(result is json[]);
test:assertEquals(result, [decimalJson, floatJson, intJson]);

result = check read([decimalJson, floatJson, intJson], condition11);
test:assertTrue(result is json[]);
test:assertEquals(result, <json[]>[decimalJson, floatJson]);
}

@test:Config {}
Expand Down Expand Up @@ -638,22 +648,6 @@ function testSelectElementByPatternMatchingExpression() returns error? {
test:assertEquals(result, {"a1": 2.34, "a2": 3.65});
}

@test:Config {}
function testA() returns error? {

json result = check read(j4, `$.a2[1][?(@.a2 in ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[stringJson]);

result = check read(j4, `$.a2[1][?(@.a2 nin ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[]);

result = check read(j4, `$.a2[1][?(@.a1 nin ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[stringJson]);

result = check read(j4, `$.a2[1][?(@.a1 in ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[]);
}

@test:Config {}
function testFunctionExpression() returns error? {
json result = check read(j4, `$..a1.sum()`);
Expand Down Expand Up @@ -716,3 +710,28 @@ function testFunctionExpression() returns error? {
result = check read(decimalJson, `$['a1'].keys()`);
test:assertEquals(result, ());
}

@test:Config {}
function testListOperations() returns error? {

json result = check read(j4, `$.a2[1][?(@.a2 in ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[stringJson]);

result = check read(j4, `$.a2[1][?(@.a2 nin ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[]);

result = check read(j4, `$.a2[1][?(@.a1 nin ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[stringJson]);

result = check read(j4, `$.a2[1][?(@.a1 in ['string', 'string2'])]`);
test:assertEquals(result, <json[]>[]);
}

@test:Config {}
function testGeneralJsonPathExpresions() returns error? {
json result = check read(j5, `$`);
test:assertEquals(result, j5);

result = check read(j5, `@`);
test:assertEquals(result, j5);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ function errorTest() {
result = read(j1, `$.a1[1]`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.a1[1]' on the provided JSON value");

result = read((), `$.a1[1]`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "json object can not be null");

result = read(j1, ``);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "path can not be null or empty");
}
Loading

0 comments on commit 4a0ab9a

Please sign in to comment.