Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Prettify API and Improve Build Scripts #7

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ org = "ballerina"
name = "data.jsondata"
version = "0.1.0"
dependencies = [
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.float"},
Expand All @@ -24,6 +25,21 @@ modules = [
{org = "ballerina", packageName = "data.jsondata", moduleName = "data.jsondata"}
]

[[package]]
org = "ballerina"
name = "file"
version = "1.9.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerina", packageName = "file", moduleName = "file"}
]

[[package]]
org = "ballerina"
name = "io"
Expand Down Expand Up @@ -120,6 +136,16 @@ modules = [
{org = "ballerina", packageName = "lang.value", moduleName = "lang.value"}
]

[[package]]
org = "ballerina"
name = "os"
version = "1.8.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "test"
Expand All @@ -134,3 +160,12 @@ modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

[[package]]
org = "ballerina"
name = "time"
version = "2.4.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

17 changes: 2 additions & 15 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@

import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
repositories {
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/plugin-gradle'
credentials {
username System.getenv("packageUser")
password System.getenv("packagePAT")
}
}
}
dependencies {
classpath "io.ballerina:plugin-gradle:${project.ballerinaGradlePluginVersion}"
}
plugins {
id 'io.ballerina.plugin'
}

description = 'Ballerina - Data JSON Module'
Expand Down Expand Up @@ -57,8 +46,6 @@ def stripBallerinaExtensionVersion(String extVersion) {
}
}

apply plugin: 'io.ballerina.plugin'

ballerina {
testCoverageParam = "--code-coverage --coverage-format=xml --includes=io.ballerina.lib.data.*:ballerina.*"
packageOrganization = packageOrg
Expand Down
10 changes: 10 additions & 0 deletions ballerina/json_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public isolated function parseStream(stream<byte[], error?> s, Options options =
public isolated function toJson(anydata v)
returns json|Error = @java:Method {'class: "io.ballerina.lib.data.jsondata.json.Native"} external;

# Prettifies a `json` value to print it.
#
# + value - The `json` value to be prettified
# + indentation - The number of spaces for an indentation
# + return - The prettified `json` as a string
public isolated function prettify(json value, int indentation = 4) returns string {
string indent = getIndentation(indentation);
return prettifyJson(value, indent, 0);
}

# Represent the options that can be used to modify the behaviour of the projection.
#
# + allowDataProjection - Enable or disable projection
Expand Down
224 changes: 224 additions & 0 deletions ballerina/tests/prettify_test.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;

@test:Config {
groups: ["prettify", "string"]
}
function testStringValue() returns error? {
json value = "Sam";
string actual = prettify(value);
string expected = check getStringContentFromFile("string_value.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "int"]
}
function testIntValue() returns error? {
json value = 515;
string actual = prettify(value);
string expected = check getStringContentFromFile("int_value.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "boolean"]
}
function testBooleanValue() returns error? {
json value = false;
string actual = prettify(value);
string expected = check getStringContentFromFile("boolean_value.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "null"]
}
function testNullValue() returns error? {
json value = null;
string actual = prettify(value);
string expected = " null";
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "null"]
}
isolated function testNullInMap() returns error? {
json value = {
name: "Sam",
age: null,
address: {
number: 308,
street: "Negra Arroyo Lane",
city: null
}
};
string actual = prettify(value);
string expected = check getStringContentFromFile("null_in_map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array"]
}
function testStringArray() returns error? {
json value = ["sam", "bam", "tan"];
string actual = prettify(value);
string expected = check getStringContentFromFile("string_array.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array"]
}
function testEmptyArray() returns error? {
json value = [];
string actual = prettify(value);
string expected = check getStringContentFromFile("empty_array.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testEmptyMap() returns error? {
json value = {};
string actual = prettify(value);
string expected = check getStringContentFromFile("empty_map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testArrayOfEmptyMaps() returns error? {
json value = [
{},
{},
{}
];
string actual = prettify(value);
string expected = check getStringContentFromFile("array_of_empty_maps.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testMapWithStringField() returns error? {
json value = {
name: "Walter White"
};
string actual = prettify(value);
string expected = check getStringContentFromFile("map_with_string_field.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testMapWithMultipleStringFields() returns error? {
json value = {
name: "Walter White",
subject: "Chemistry",
city: "Albequerque"
};
string actual = prettify(value);
string expected = check getStringContentFromFile("map_with_multiple_string_fields.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testMap() returns error? {
json value = {
person: {
name: "Walter White",
age: 51,
address: {
number: 308,
street: "Negra Arroyo Lane",
city: "Albequerque"
}
}
};
string actual = prettify(value);
string expected = check getStringContentFromFile("map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testArrayOfMap() returns error? {
json value = [
{
name: "Walter White",
age: 51,
address: {
number: 308,
street: "Negra Arroyo Lane",
city: "Albequerque"
}
},
{
name: "Jesse Pinkman",
age: 26,
address: {
number: 9809,
street: "Margo Street",
city: "Albequerque"
}
}
];
string actual = prettify(value);
string expected = check getStringContentFromFile("array_of_map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array", "map"]
}
function testComplexExample() returns error? {
json value = check getJsonContentFromFile("complex_example.json");
string actual = prettify(value);
string expected = check getStringContentFromFile("complex_example.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array", "map"]
}
function testComplexExampleWithCustomIndentation() returns error? {
json value = check getJsonContentFromFile("complex_example.json");
string actual = prettify(value, 2);
string expected = check getStringContentFromFile("complex_example_with_custom_indentation.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array", "map"]
}
function testComplexExampleWithCustomIndentationInvalidTest() returns error? {
json value = check getJsonContentFromFile("complex_example.json");
string actual = prettify(value, 2);
string expected = check getStringContentFromFile("complex_example.json");
test:assertNotEquals(actual, expected);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{},
{},
{}
]
20 changes: 20 additions & 0 deletions ballerina/tests/resources/expected_results/array_of_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"name": "Walter White",
"age": 51,
"address": {
"number": 308,
"street": "Negra Arroyo Lane",
"city": "Albequerque"
}
},
{
"name": "Jesse Pinkman",
"age": 26,
"address": {
"number": 9809,
"street": "Margo Street",
"city": "Albequerque"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
Loading
Loading