From 40eef579819f654f26ad1b55fd4311cee3cc3b6e Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Mon, 12 Aug 2024 11:48:46 +0530 Subject: [PATCH] Refactor user configuration parameter --- .../tests/constraint_validation_test.bal | 6 +- .../tests/parse_list_type_as_list_test.bal | 60 +++++++++---------- .../tests/parse_list_type_as_record_test.bal | 38 ++++++------ .../tests/test_union_types_with_headers.bal | 10 ++-- .../user_config_with_parser_options_test.bal | 52 ++++++++-------- ballerina/types.bal | 35 ++++++++--- .../lib/data/csvdata/csv/CsvCreator.java | 2 +- .../lib/data/csvdata/csv/CsvParser.java | 12 ++-- .../lib/data/csvdata/csv/CsvTraversal.java | 28 ++++----- .../lib/data/csvdata/utils/Constants.java | 4 +- .../lib/data/csvdata/utils/CsvConfig.java | 12 ++-- .../lib/data/csvdata/utils/CsvUtils.java | 8 +-- 12 files changed, 143 insertions(+), 124 deletions(-) diff --git a/ballerina-tests/constraint-validation-tests/tests/constraint_validation_test.bal b/ballerina-tests/constraint-validation-tests/tests/constraint_validation_test.bal index de74848..56db0c6 100644 --- a/ballerina-tests/constraint-validation-tests/tests/constraint_validation_test.bal +++ b/ballerina-tests/constraint-validation-tests/tests/constraint_validation_test.bal @@ -38,7 +38,7 @@ function testConstraintWithLists() returns error? { ConstrainedList|csv:Error cList1 = csv:parseString(string `1 2 3 - 4`, {header: false, customHeadersIfHeaderAbsent: ["a", "b", "c", "d"]}); + 4`, {header: false, customHeadersIfHeadersAbsent: ["a", "b", "c", "d"]}); test:assertEquals(cList1, [[1], [2], [3], [4]]); cList1 = csv:parseString(string `1 @@ -46,7 +46,7 @@ function testConstraintWithLists() returns error? { 3 4 5 - 6`, {header: false, customHeadersIfHeaderAbsent: ["a", "b", "c", "d", "e", "f"]}); + 6`, {header: false, customHeadersIfHeadersAbsent: ["a", "b", "c", "d", "e", "f"]}); test:assertTrue(cList1 is csv:Error); test:assertTrue((cList1).message().startsWith("Validation failed") && (cList1).message().includes("length")); @@ -56,7 +56,7 @@ function testConstraintWithLists() returns error? { 3 4 5 - 6`, {header: false, customHeadersIfHeaderAbsent: ["a", "b", "c", "d", "e", "f"], enableConstraintValidation: false}); + 6`, {header: false, customHeadersIfHeadersAbsent: ["a", "b", "c", "d", "e", "f"], enableConstraintValidation: false}); test:assertEquals(cList1, [[1], [2], [3], [4], [5], [6]]); cList1 = csv:transform([{"a": 1}, {"a": 1}, {"a": 1}, {"a": 1}], {}); diff --git a/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_list_test.bal b/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_list_test.bal index 1bd498c..086e760 100644 --- a/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_list_test.bal +++ b/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_list_test.bal @@ -450,119 +450,119 @@ function testArrayIndexes() { @test:Config function testParseListsWithOutputHeaders() { - [string, boolean, int][]|csv:Error ct1bt1 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 1}); + [string, boolean, int][]|csv:Error ct1bt1 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1}); test:assertEquals(ct1bt1, [ ["a", true, 1] ]); - ct1bt1 = csv:parseList([["a", "b", "c"], ["a", "b", "c"]], {headerRows: 2}); + ct1bt1 = csv:parseList([["a", "b", "c"], ["a", "b", "c"]], {headersRows: 2}); test:assertEquals(ct1bt1, []); - ct1bt1 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"]}); + ct1bt1 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"]}); test:assertEquals(ct1bt1, [ ["a", true, 1] ]); - ct1bt1 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 0, customHeaders: ["h1", "h2", "h3"]}); + ct1bt1 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 0, customHeaders: ["h1", "h2", "h3"]}); test:assertEquals(ct1bt1, [ ["a", true, 1], ["a", true, 1] ]); - ct1bt1 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 21}); + ct1bt1 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 21}); test:assertEquals(ct1bt1, []); - (string|boolean|int)[][]|csv:Error ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 1}); + (string|boolean|int)[][]|csv:Error ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1}); test:assertEquals(ct1bt1_2, [ ["a", true, 1] ]); - ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "b", "c"]], {headerRows: 2}); + ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "b", "c"]], {headersRows: 2}); test:assertEquals(ct1bt1_2, []); - ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"]}); + ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"]}); test:assertEquals(ct1bt1_2, [ ["a", true, 1] ]); - ct1bt1_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 0, customHeaders: ["h1", "h2", "h3"]}); + ct1bt1_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 0, customHeaders: ["h1", "h2", "h3"]}); test:assertEquals(ct1bt1_2, [ ["a", true, 1], ["a", true, 1] ]); - ct1bt1_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 21}); + ct1bt1_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 21}); test:assertEquals(ct1bt1_2, []); - [string, boolean, int][]|csv:Error ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 1, outputWithHeaders: true}); + [string, boolean, int][]|csv:Error ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 1, outputWithHeaders: true}); test:assertEquals(ct1bt1_3, [ ["a", true, 1], ["a", true, 1] ]); - ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 2, outputWithHeaders: true}); + ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 2, outputWithHeaders: true}); test:assertTrue(ct1bt1_3 is csv:Error); test:assertEquals((ct1bt1_3).message(), "Custom headers should be provided"); - ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"], ["a", "true", "1"]], {headerRows: 2, outputWithHeaders: true}); + ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"], ["a", "true", "1"]], {headersRows: 2, outputWithHeaders: true}); test:assertTrue(ct1bt1_3 is csv:Error); test:assertEquals((ct1bt1_3).message(), "Custom headers should be provided"); - ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertTrue(ct1bt1_3 is csv:Error); test:assertEquals((ct1bt1_3).message(), common:generateErrorMessageForInvalidValueForArrayType("h2", "1", "boolean")); - ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertTrue(ct1bt1_3 is csv:Error); test:assertEquals((ct1bt1_3).message(), common:generateErrorMessageForInvalidValueForArrayType("h2", "1", "boolean")); - ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 21, outputWithHeaders: true}); + ct1bt1_3 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 21, outputWithHeaders: true}); test:assertEquals(ct1bt1_3, []); - string[][]|csv:Error ct1bt1_4 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 1, outputWithHeaders: true}); + string[][]|csv:Error ct1bt1_4 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1, outputWithHeaders: true}); test:assertEquals(ct1bt1_4, [ ["a", "b", "c"], ["a", "true", "1"] ]); - ct1bt1_4 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + ct1bt1_4 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt1_4, [ ["h1", "h2", "h3"], ["a", "true", "1"], ["a", "true", "1"] ]); - (int|boolean|string)[][]|csv:Error ct1bt1_4_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 1, outputWithHeaders: true}); + (int|boolean|string)[][]|csv:Error ct1bt1_4_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1, outputWithHeaders: true}); test:assertEquals(ct1bt1_4_2, [ ["a", "b", "c"], ["a", true, 1] ]); - string[][]|csv:Error ct1bt1_4_3 = csv:parseList([["a", "b", "c"], ["a", "true", "1"], ["a", "true", "1"], ["a", "true", "1"]], {headerRows: 2, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + string[][]|csv:Error ct1bt1_4_3 = csv:parseList([["a", "b", "c"], ["a", "true", "1"], ["a", "true", "1"], ["a", "true", "1"]], {headersRows: 2, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt1_4_3, [ ["h1", "h2", "h3"], ["a", "true", "1"], ["a", "true", "1"] ]); - (int|boolean|string)[][]|csv:Error ct1bt1_4_4 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headerRows: 2, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + (int|boolean|string)[][]|csv:Error ct1bt1_4_4 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 2, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt1_4_4, [ ["h1", "h2", "h3"] ]); - [string, boolean|string, int|string][]|csv:Error ct1bt1_5 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + [string, boolean|string, int|string][]|csv:Error ct1bt1_5 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt1_5, [ ["h1", "h2", "h3"], ["a", true, 1] ]); - ct1bt1_5 = csv:parseList([["a", "true", "2"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + ct1bt1_5 = csv:parseList([["a", "true", "2"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt1_5, [ ["h1", "h2", "h3"], ["a", true, 1] ]); - ct1bt1_5 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + ct1bt1_5 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt1_5, [ ["h1", "h2", "h3"], ["a", true, 1], @@ -575,35 +575,35 @@ function testParseListsWithOutputHeaders() { ["1", "a"] ]); - [string, boolean|string, int|string...][]|csv:Error ct1bt2_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + [string, boolean|string, int|string...][]|csv:Error ct1bt2_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt2_2, [ ["h1", "h2", "h3"], ["a", true, 1] ]); - [string...][]|csv:Error ct1bt2_3 = csv:parseList([["a", "true", "2"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + [string...][]|csv:Error ct1bt2_3 = csv:parseList([["a", "true", "2"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt2_3, [ ["h1", "h2", "h3"], ["a", "true", "1"] ]); - ct1bt2_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + ct1bt2_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 0, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt2_2, [ ["h1", "h2", "h3"], ["a", true, 1], ["a", true, 1] ]); - string[2][1]|csv:Error ct1bt6 = csv:parseList([["a", "b", "c"], ["a", "true", "1"], ["a", "true", "1"], ["a", "true", "1"]], {headerRows: 2, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + string[2][1]|csv:Error ct1bt6 = csv:parseList([["a", "b", "c"], ["a", "true", "1"], ["a", "true", "1"], ["a", "true", "1"]], {headersRows: 2, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt6, [ ["h1"], ["a"] ]); - [string, boolean|string, int|string...][1]|csv:Error ct1bt6_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + [string, boolean|string, int|string...][1]|csv:Error ct1bt6_2 = csv:parseList([["a", "true", "1"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt6_2, [ ["h1", "h2", "h3"] ]); - [string...][1]|csv:Error ct1bt6_3 = csv:parseList([["a", "true", "2"], ["a", "true", "1"]], {headerRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); + [string...][1]|csv:Error ct1bt6_3 = csv:parseList([["a", "true", "2"], ["a", "true", "1"]], {headersRows: 1, customHeaders: ["h1", "h2", "h3"], outputWithHeaders: true}); test:assertEquals(ct1bt6_3, [ ["h1", "h2", "h3"] ]); diff --git a/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_record_test.bal b/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_record_test.bal index d1c2fa1..848e76e 100644 --- a/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_record_test.bal +++ b/ballerina-tests/parse-list-types-tests/tests/parse_list_type_as_record_test.bal @@ -551,20 +551,20 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedType7() { @test:Config function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { - record{string a; boolean b; int c;}[]|csv:Error ct1br4 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 0, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + record{string a; boolean b; int c;}[]|csv:Error ct1br4 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 0, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct1br4, [ {a: "a", b: true, c: 1}, {a: "a", b: true, c: 1}, {a: "a", b: true, c: 1} ]); - record{string a; boolean b; int c;}[]|csv:Error ct1br4_2 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 1, customHeaders: ["a", "c", "b"]}); + record{string a; boolean b; int c;}[]|csv:Error ct1br4_2 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 1, customHeaders: ["a", "c", "b"]}); test:assertEquals(ct1br4_2, [ {a: "a", b: true, c: 1}, {a: "a", b: true, c: 1} ]); - record{string a; boolean b; int c;}[]|csv:Error ct1br4_3 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 1, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + record{string a; boolean b; int c;}[]|csv:Error ct1br4_3 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 1, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct1br4_3, [ {a: "a", b: true, c: 1}, {a: "a", b: true, c: 1} @@ -572,60 +572,60 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { record{string a; boolean b; int c;}[]|csv:Error ct1br4_4 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct1br4_4, [ {a: "a", b: true, c: 1} ]); record{string a; boolean b; int c;}[]|csv:Error ct1br4_5 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, outputWithHeaders: true}); test:assertTrue(ct1br4_5 is csv:Error); test:assertEquals((ct1br4_5).message(), "Custom headers should be provided"); record{|string a; boolean b; int...;|}[]|csv:Error ct1br4_4_2 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct1br4_4_2, [ {a: "a", b: true, c: 1} ]); record{|string a; boolean b; int...;|}[]|csv:Error ct1br4_5_2 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, outputWithHeaders: true}); test:assertTrue(ct1br4_5_2 is csv:Error); test:assertEquals((ct1br4_5).message(), "Custom headers should be provided"); - map[]|csv:Error ct2br4_3 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 1, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + map[]|csv:Error ct2br4_3 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 1, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct2br4_3, [ {a: "a", b: true, c: 1}, {a: "a", b: true, c: 1} ]); - map[]|csv:Error ct2br4_3_2 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 3, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + map[]|csv:Error ct2br4_3_2 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 3, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct2br4_3_2, []); map[]|csv:Error ct2br4_4 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct2br4_4, [ {a: "a", b: true, c: 1} ]); map[]|csv:Error ct2br4_5 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct2br4_5, [ {a: "a", b: "true", c: "1"}, {a: "a", b: "true", c: "1"} ]); map[]|csv:Error ct2br4_7 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 4, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 4, customHeaders: ["a", "c", "b"], outputWithHeaders: true}); test:assertEquals(ct2br4_7, []); map[]|csv:Error ct2br4_6 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 2, outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 2, outputWithHeaders: true}); test:assertTrue(ct2br4_6 is csv:Error); test:assertEquals((ct1br4_5).message(), "Custom headers should be provided"); map[]|csv:Error ct2br4_8 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 1, outputWithHeaders: true}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 1, outputWithHeaders: true}); test:assertEquals(ct2br4_8, [ {"a": "a", "1": "1", "true": "true"}, {"a": "a", "1": "1", "true": "true"}, @@ -633,7 +633,7 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { ]); map[]|csv:Error ct2br4_8_2 = csv:parseList( - [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headerRows: 1, outputWithHeaders: false}); + [["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 1, outputWithHeaders: false}); test:assertEquals(ct2br4_8_2, [ {"a": "a", "1": "1", "true": "true"}, {"a": "a", "1": "1", "true": "true"}, @@ -641,7 +641,7 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { ]); map[]|csv:Error ct2br4_9 = csv:parseList( - [["a", "c", "b"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headerRows: 1, outputWithHeaders: true}); + [["a", "c", "b"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headersRows: 1, outputWithHeaders: true}); test:assertEquals(ct2br4_9, [ {a: "a", b: "true", c: "2"}, {a: "a", b: "true", c: "3"}, @@ -650,7 +650,7 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { ]); map[]|csv:Error ct2br4_10 = csv:parseList( - [["a", "b", "c"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headerRows: 1, outputWithHeaders: true}); + [["a", "b", "c"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headersRows: 1, outputWithHeaders: true}); test:assertEquals(ct2br4_10, [ {a: "a", c: true, b: 2}, {a: "a", c: true, b: 3}, @@ -659,7 +659,7 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { ]); map[]|csv:Error ct2br4_10_2 = csv:parseList( - [["a", "b", "c"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headerRows: 1, outputWithHeaders: false}); + [["a", "b", "c"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headersRows: 1, outputWithHeaders: false}); test:assertEquals(ct2br4_10_2, [ {a: "a", c: true, b: 2}, {a: "a", c: true, b: 3}, @@ -668,7 +668,7 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() { ]); ct2br4_10_2 = csv:parseList( - [["a", "b", "c"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headerRows: 1, customHeaders: ["c", "d", "e"], outputWithHeaders: false}); + [["a", "b", "c"], ["a", "2", "true"], ["a", "3", "true"], ["a", "4", "true"], ["a", "5", "true"]], {headersRows: 1, customHeaders: ["c", "d", "e"], outputWithHeaders: false}); test:assertEquals(ct2br4_10_2, [ {c: "a", e: true, d: 2}, {c: "a", e: true, d: 3}, diff --git a/ballerina-tests/union-type-tests/tests/test_union_types_with_headers.bal b/ballerina-tests/union-type-tests/tests/test_union_types_with_headers.bal index 7066c6d..9b03961 100644 --- a/ballerina-tests/union-type-tests/tests/test_union_types_with_headers.bal +++ b/ballerina-tests/union-type-tests/tests/test_union_types_with_headers.bal @@ -9,12 +9,12 @@ csv:TransformOptions op4 = {headersOrder: ["a", "b"], outputWithHeaders: true}; csv:ParseOptions op5 = {outputWithHeaders: true}; csv:ParseListOptions op6 = {outputWithHeaders: true}; -csv:ParseListOptions op7 = {outputWithHeaders: true, headerRows: 1}; -csv:ParseListOptions op8 = {outputWithHeaders: true, headerRows: 2}; -csv:ParseListOptions op9 = {outputWithHeaders: true, headerRows: 2, customHeaders: ["a", "b", "c", "d"]}; -csv:ParseListOptions op10 = {outputWithHeaders: true, headerRows: 1, customHeaders: ["a", "b", "c", "d"]}; +csv:ParseListOptions op7 = {outputWithHeaders: true, headersRows: 1}; +csv:ParseListOptions op8 = {outputWithHeaders: true, headersRows: 2}; +csv:ParseListOptions op9 = {outputWithHeaders: true, headersRows: 2, customHeaders: ["a", "b", "c", "d"]}; +csv:ParseListOptions op10 = {outputWithHeaders: true, headersRows: 1, customHeaders: ["a", "b", "c", "d"]}; csv:ParseListOptions op11 = {outputWithHeaders: true, customHeaders: ["a", "b", "c", "d"]}; -csv:ParseListOptions op12 = {headerRows: 1, customHeaders: ["a", "b", "c", "d"]}; +csv:ParseListOptions op12 = {headersRows: 1, customHeaders: ["a", "b", "c", "d"]}; csv:ParseListOptions op13 = {customHeaders: ["a", "b", "c", "d"]}; type UnionType1 boolean[][]|string[][]; diff --git a/ballerina-tests/user-config-tests/tests/user_config_with_parser_options_test.bal b/ballerina-tests/user-config-tests/tests/user_config_with_parser_options_test.bal index 6be4c25..91d84f2 100644 --- a/ballerina-tests/user-config-tests/tests/user_config_with_parser_options_test.bal +++ b/ballerina-tests/user-config-tests/tests/user_config_with_parser_options_test.bal @@ -657,7 +657,7 @@ function testCustomHeaderOption() { @test:Config function testCustomHeaderParserOption2() { - record {}[]|csv:Error ct1br = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeaderAbsent: ["a", "b"]}); + record {}[]|csv:Error ct1br = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeadersAbsent: ["a", "b"]}); test:assertEquals(ct1br, [ {a: 1, b: "string1", c: true, d: 2.234, e: 2.234, f: ()}, {a: 2, b: "string2", c: false, d: 0, e: 0, f: ()}, @@ -666,7 +666,7 @@ function testCustomHeaderParserOption2() { {a: 5, b: "string5", c: true, d: 3, e: 3, f: ()} ]); - ct1br = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeaderAbsent: ["f", "e", "d", "c", "b", "a"]}); + ct1br = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]}); test:assertEquals(ct1br, [ {a: 1, b: "string1", c: true, d: 2.234, e: 2.234, f: ()}, {a: 2, b: "string2", c: false, d: 0, e: 0, f: ()}, @@ -675,7 +675,7 @@ function testCustomHeaderParserOption2() { {a: 5, b: "string5", c: true, d: 3, e: 3, f: ()} ]); - ct1br = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeaderAbsent: ["b", "a"]}); + ct1br = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeadersAbsent: ["b", "a"]}); test:assertEquals(ct1br, [ {a: 1, b: "string1", c: true, d: 2.234, e: 2.234, f: ()}, {a: 2, b: "string2", c: false, d: 0, e: 0, f: ()}, @@ -684,15 +684,15 @@ function testCustomHeaderParserOption2() { {a: 5, b: "string5", c: true, d: 3, e: 3, f: ()} ]); - record {}[]|csv:Error ct1br2 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: []}); + record {}[]|csv:Error ct1br2 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: []}); test:assertTrue(ct1br2 is csv:Error); test:assertEquals((ct1br2).message(), "Invalid length for the headers"); - record {int a; string b; boolean c; decimal d; float e; () f;}[]|csv:Error ct1br3 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: ["a", "b"]}); + record {int a; string b; boolean c; decimal d; float e; () f;}[]|csv:Error ct1br3 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["a", "b"]}); test:assertTrue(ct1br3 is csv:Error); test:assertEquals((ct1br3).message(), "Invalid length for the headers"); - record {int a; string b; boolean c; decimal d; float e; () f;}[]|csv:Error ct1br4 = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeaderAbsent: ["a", "b", "c", "d", "e", "f"]}); + record {int a; string b; boolean c; decimal d; float e; () f;}[]|csv:Error ct1br4 = csv:parseString(csvStringData1, {header: 1, customHeadersIfHeadersAbsent: ["a", "b", "c", "d", "e", "f"]}); test:assertEquals(ct1br4, [ {a: 1, b: "string1", c: true, d: 2.234, e: 2.234, f: ()}, {a: 2, b: "string2", c: false, d: 0, e: 0, f: ()}, @@ -701,10 +701,10 @@ function testCustomHeaderParserOption2() { {a: 5, b: "string5", c: true, d: 3, e: 3, f: ()} ]); - record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5 = csv:parseString(csvStringData4, {header: 1, customHeadersIfHeaderAbsent: ["f", "e", "d", "c", "b", "a"]}); + record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5 = csv:parseString(csvStringData4, {header: 1, customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]}); test:assertTrue(ct1br5 is csv:Error); - ct1br5 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: ["f", "e", "d", "c", "b", "a"]}); + ct1br5 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]}); test:assertEquals(ct1br5, [ {f: 1, e: "string1", d: true, c: 2.234, b: 2.234, a: ()}, {f: 2, e: "string2", d: false, c: 0, b: 0, a: ()}, @@ -713,7 +713,7 @@ function testCustomHeaderParserOption2() { {f: 5, e: "string5", d: true, c: 3, b: 3, a: ()} ]); - record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5_2 = csv:parseString(csvStringData4, {header: false, skipLines: [1], customHeadersIfHeaderAbsent: ["f", "e", "d", "c", "b", "a"]}); + record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5_2 = csv:parseString(csvStringData4, {header: false, skipLines: [1], customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]}); test:assertEquals(ct1br5_2, [ {f: 2, e: "string2", d: false, c: 0, b: 0, a: ()}, {f: 3, e: "string3", d: false, c: 1.23, b: 1.23, a: ()}, @@ -721,7 +721,7 @@ function testCustomHeaderParserOption2() { {f: 5, e: "string5", d: true, c: 3, b: 3, a: ()} ]); - record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5_3 = csv:parseString(csvStringData4, {skipLines: [1], header: false, customHeadersIfHeaderAbsent: ["f", "e", "d", "c", "b", "a"]}); + record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5_3 = csv:parseString(csvStringData4, {skipLines: [1], header: false, customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]}); test:assertEquals(ct1br5_3, [ {f: 2, e: "string2", d: false, c: 0, b: 0, a: ()}, {f: 3, e: "string3", d: false, c: 1.23, b: 1.23, a: ()}, @@ -729,7 +729,7 @@ function testCustomHeaderParserOption2() { {f: 5, e: "string5", d: true, c: 3, b: 3, a: ()} ]); - record {|boolean d1; string e1;|}[]|csv:Error ct1br7 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); + record {|boolean d1; string e1;|}[]|csv:Error ct1br7 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); test:assertEquals(ct1br7, [ {e1: "string1", d1: true}, {e1: "string2", d1: false}, @@ -738,7 +738,7 @@ function testCustomHeaderParserOption2() { {e1: "string5", d1: true} ]); - record {|boolean d1; string e1;|}[]|csv:Error ct1br7_2 = csv:parseString(csvStringData4, {header: false, skipLines: [1], customHeadersIfHeaderAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); + record {|boolean d1; string e1;|}[]|csv:Error ct1br7_2 = csv:parseString(csvStringData4, {header: false, skipLines: [1], customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); test:assertEquals(ct1br7_2, [ {e1: "string2", d1: false}, {e1: "string3", d1: false}, @@ -746,11 +746,11 @@ function testCustomHeaderParserOption2() { {e1: "string5", d1: true} ]); - record {|boolean d1; string e1;|}[]|csv:Error ct1br8 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: ["e1", "d1"]}); + record {|boolean d1; string e1;|}[]|csv:Error ct1br8 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["e1", "d1"]}); test:assertTrue(ct1br8 is csv:Error); test:assertEquals((ct1br8).message(), common:generateErrorMessageForInvalidCast("string1", "boolean")); - record {|boolean d1; string e1;|}[]|csv:Error ct1br9 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); + record {|boolean d1; string e1;|}[]|csv:Error ct1br9 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); test:assertEquals(ct1br9, [ {e1: "string1", d1: true}, {e1: "string2", d1: false}, @@ -759,11 +759,11 @@ function testCustomHeaderParserOption2() { {e1: "string5", d1: true} ]); - record {|boolean d1; string e1;|}[]|csv:Error ct1br11 = csv:parseString(csvStringData1, {header: false, customHeadersIfHeaderAbsent: ["f1", "e1"]}); + record {|boolean d1; string e1;|}[]|csv:Error ct1br11 = csv:parseString(csvStringData1, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1"]}); test:assertTrue(ct1br11 is csv:Error); test:assertEquals((ct1br11).message(), "Invalid length for the headers"); - record {|string d1; string e1;|}[]|csv:Error ct1br12 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeaderAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); + record {|string d1; string e1;|}[]|csv:Error ct1br12 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]}); test:assertEquals(ct1br12, [ {e1: "string1", d1: "true"}, {e1: "string2", d1: "false"}, @@ -772,7 +772,7 @@ function testCustomHeaderParserOption2() { {e1: "string5", d1: "true"} ]); - record {|string d1; string e1;|}[]|csv:Error ct1br13 = csv:parseString(csvStringData1, {header: false, customHeadersIfHeaderAbsent: ["f1", "e1", "dd1", "c1", "b1", "a1"]}); + record {|string d1; string e1;|}[]|csv:Error ct1br13 = csv:parseString(csvStringData1, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "dd1", "c1", "b1", "a1"]}); test:assertTrue(ct1br13 is csv:Error); test:assertEquals((ct1br13).message(), common:generateErrorMessageForMissingRequiredField("d1")); } @@ -1001,30 +1001,30 @@ function testSkipLineParameterWithOutputHeaderConfig() { result2 = csv:parseList(csv2, {outputWithHeaders: true, skipLines: "2-3", customHeaders: ["a", "b"]}); test:assertEquals(result2, [["a", "b"], [1, 2], [4, 5]]); - result = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 1, skipLines: "2-3", customHeaders: ["a", "b"]}); + result = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 1, skipLines: "2-3", customHeaders: ["a", "b"]}); test:assertEquals(result, [{a: 2, b: 3}]); - result2 = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 1, skipLines: "2-3", customHeaders: ["a", "b"]}); + result2 = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 1, skipLines: "2-3", customHeaders: ["a", "b"]}); test:assertEquals(result2, [["a", "b"], [2, 3]]); - result = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 2, skipLines: "2-3", customHeaders: ["a", "b"]}); + result = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 2, skipLines: "2-3", customHeaders: ["a", "b"]}); test:assertEquals(result, [{a: 3, b: 4}]); - result2 = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 2, skipLines: "2-3", customHeaders: ["a", "b"]}); + result2 = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 2, skipLines: "2-3", customHeaders: ["a", "b"]}); test:assertEquals(result2, [["a", "b"], [3, 4]]); - result = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 1, skipLines: "2-3"}); + result = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 1, skipLines: "2-3"}); test:assertEquals(result, [{'1: 2, '2: 3}]); - result2 = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 1, skipLines: "2-3"}); + result2 = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 1, skipLines: "2-3"}); test:assertEquals(result2, [[1, 2], [2, 3]]); - result2 = csv:parseList(csv2, {outputWithHeaders: false, headerRows: 1, skipLines: "2-3"}); + result2 = csv:parseList(csv2, {outputWithHeaders: false, headersRows: 1, skipLines: "2-3"}); test:assertEquals(result2, [[2, 3]]); - result = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 2, customHeaders: ["a", "b"], skipLines: "2-3"}); + result = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 2, customHeaders: ["a", "b"], skipLines: "2-3"}); test:assertEquals(result, [{a: 3, b: 4}]); - result2 = csv:parseList(csv2, {outputWithHeaders: true, headerRows: 2, customHeaders: ["a", "b"], skipLines: "2-3"}); + result2 = csv:parseList(csv2, {outputWithHeaders: true, headersRows: 2, customHeaders: ["a", "b"], skipLines: "2-3"}); test:assertEquals(result2, [["a", "b"], [3, 4]]); } diff --git a/ballerina/types.bal b/ballerina/types.bal index 030adb9..c825679 100644 --- a/ballerina/types.bal +++ b/ballerina/types.bal @@ -1,7 +1,20 @@ +// 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. + # Represents an error. -# -# This type is used to capture error details that occur during the execution of a program. -# It can hold an error message, an optional error cause, and an optional map of additional details. public type Error error; # Defines the name of the JSON Object key. @@ -18,7 +31,8 @@ public const annotation NameConfig Name on record field; public type Options record { # Allows data projection with specific settings. # - # This field can be either a record or a boolean. If it's a record, it contains the following fields: + # This configuration can be either a record or a boolean. + # If it's a record, it contains `nilAsOptionalField` and `absentAsNilableType` options. # If it's set to `false`, data projection is not allowed. record { # If `true`, nil values will be considered as optional fields in the projection. @@ -30,8 +44,7 @@ public type Options record { int[]|string skipLines = []; # If `true`, enables validation of constraints during processing. boolean enableConstraintValidation = true; - # If `true`, the resulted CSV contains the headers as the first row. - # This field is only considered if the expected type is a subset of `anydata[][]` + # If `true`, when the result is a list it will contain headers as the first row. boolean outputWithHeaders = false; }; @@ -57,7 +70,7 @@ public type ParseOptions record {| # Specifies whether the header is present and, if so, the number of header lines. false|int:Unsigned32 header = 0; # Custom headers for the data, if headers are absent. - string[]? customHeadersIfHeaderAbsent = (); + string[]? customHeadersIfHeadersAbsent = (); |}; # Represents options for treating a list as a record. @@ -65,7 +78,7 @@ public type ParseListOptions record {| *Options; # If `0`, all the source data will treat as data rows. # Otherwise specify the header rows(Starts from 1) in the source data. - int:Unsigned32 headerRows = 0; + int:Unsigned32 headersRows = 0; # Specify the header names of the source data. # This field will overwrite the header values in the header rows. # This will be mandatory if the header row parameter is larger than one. @@ -82,14 +95,20 @@ public type TransformOptions record {| # Enum representing possible line terminators. public enum LineTerminator { + # Line Feed (LF) line terminator: `\n` LF = "\n", + # Carriage Return and Line Feed (CRLF) line terminator: `\r\n` CRLF = "\r\n" }; # Enum representing possible nil values. public enum NilValue { + # Represents a nil value as the string "null". NULL = "null", + # Represents a nil value as "N/A". NOT_APPLICABLE = "N/A", + # Represents an empty string as a nil value. EMPTY_STRING = "", + # Represents a nil value as Ballerina nil value `()`. BAL_NULL = "()" }; diff --git a/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvCreator.java b/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvCreator.java index 174e97c..06741f4 100644 --- a/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvCreator.java +++ b/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvCreator.java @@ -111,7 +111,7 @@ static void convertAndUpdateCurrentCsvNode(CsvParser.StateMachine sm, } public static String getHeaderValueForColumnIndex(CsvParser.StateMachine sm) { - if (sm.config.customHeadersIfHeaderAbsent == null && (sm.config.header == Boolean.FALSE)) { + if (sm.config.customHeadersIfHeadersAbsent == null && (sm.config.header == Boolean.FALSE)) { String header = String.valueOf(sm.columnIndex + 1); Map fieldHierarchy = sm.fieldHierarchy; fieldHierarchy.remove(header); diff --git a/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvParser.java b/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvParser.java index cf7aaa8..7cb9684 100644 --- a/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvParser.java +++ b/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvParser.java @@ -273,17 +273,17 @@ public Object execute(Reader reader, Type type, CsvConfig config, BTypedesc bTyp expectedArrayElementType); case TypeTags.UNION_TAG: boolean outputHeaders = config.outputWithHeaders; - Object customHeaders = config.customHeadersIfHeaderAbsent; + Object customHeaders = config.customHeadersIfHeadersAbsent; Object mapValue = execute(reader, TypeCreator.createArrayType( TypeCreator.createMapType(PredefinedTypes.TYPE_STRING) ), CsvConfig.createConfigOptionsForUnion(config), bTypedesc); config.stringConversion = true; config.outputWithHeaders = outputHeaders; if (config.outputWithHeaders && customHeaders == null) { - config.customHeadersIfHeaderAbsent = this.headers; + config.customHeadersIfHeadersAbsent = this.headers; } if (customHeaders != null) { - config.customHeadersIfHeaderAbsent = customHeaders; + config.customHeadersIfHeadersAbsent = customHeaders; } return CsvTraversal.traverse((BArray) mapValue, config, bTypedesc); default: @@ -295,9 +295,9 @@ public Object execute(Reader reader, Type type, CsvConfig config, BTypedesc bTyp if (config.header != Boolean.FALSE) { currentState = HEADER_START_STATE; } else { - Object customHeadersIfHeaderAbsent = config.customHeadersIfHeaderAbsent; - if (customHeadersIfHeaderAbsent != null) { - CsvCreator.addCustomHeadersIfNotNull(this, customHeadersIfHeaderAbsent); + Object customHeadersIfHeadersAbsent = config.customHeadersIfHeadersAbsent; + if (customHeadersIfHeadersAbsent != null) { + CsvCreator.addCustomHeadersIfNotNull(this, customHeadersIfHeadersAbsent); } currentState = ROW_START_STATE; addFieldNamesForNonHeaderState(); diff --git a/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvTraversal.java b/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvTraversal.java index 4c03daa..211ee64 100644 --- a/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvTraversal.java +++ b/native/src/main/java/io/ballerina/lib/data/csvdata/csv/CsvTraversal.java @@ -272,11 +272,11 @@ public void traverseCsvWithMappingAsExpectedType(long length, BArray csv, Type e } Object o = csv.get(i); - if (i < config.headerRows && i != config.headerRows - 1) { + if (i < config.headersRows && i != config.headersRows - 1) { continue; } - if (i >= config.headerRows && ignoreRow(rowNumber + 1, config.skipLines)) { + if (i >= config.headersRows && ignoreRow(rowNumber + 1, config.skipLines)) { rowNumber++; continue; } @@ -290,7 +290,7 @@ public void traverseCsvWithMappingAsExpectedType(long length, BArray csv, Type e rootCsvNode.add(this.arraySize, rowValue); this.arraySize++; } - if (i >= config.headerRows) { + if (i >= config.headersRows) { rowNumber++; } } @@ -310,11 +310,11 @@ public void traverseCsvWithListAsExpectedType(long length, BArray csv, Type expe Object o = csv.get(i); if (!addHeadersForOutput && config.outputWithHeaders - && (o instanceof BMap || (config.customHeaders != null || i == config.headerRows - 1))) { + && (o instanceof BMap || (config.customHeaders != null || i == config.headersRows - 1))) { // Headers will add to the list only in the first iteration insertHeaderValuesForTheCsvIfApplicable(o, expectedArrayType); } - if (i < config.headerRows) { + if (i < config.headersRows) { continue; } @@ -365,11 +365,11 @@ public void traverseCsvWithUnionExpectedType(long length, BArray csv, } if (CsvUtils.isExpectedTypeIsMap(memberType)) { - if (i < config.headerRows && i != config.headerRows - 1) { + if (i < config.headersRows && i != config.headersRows - 1) { continue outerLoop; } - if (i >= config.headerRows && ignoreRow(rowNumber + 1, config.skipLines)) { + if (i >= config.headersRows && ignoreRow(rowNumber + 1, config.skipLines)) { rowNumber++; continue outerLoop; } @@ -377,11 +377,11 @@ public void traverseCsvWithUnionExpectedType(long length, BArray csv, } else if (CsvUtils.isExpectedTypeIsArray(memberType)) { if (!addHeadersForOutput && config.outputWithHeaders && (o instanceof BMap || (config.customHeaders != null - || i == config.headerRows - 1))) { + || i == config.headersRows - 1))) { // Headers will add to the list only in the first iteration insertHeaderValuesForTheCsvIfApplicable(o, memberType); } - if (i < config.headerRows) { + if (i < config.headersRows) { continue outerLoop; } @@ -530,8 +530,8 @@ private void constructCsvMapFromNonMapping(BArray csvElement, String[] headers = new String[csvElement.size()]; if (this.headers == null) { this.headers = CsvUtils.createHeadersForParseLists(csvElement, headers, config); - if (!this.isFirstRowInserted && config.headerRows >= 1) { - // To skip the row at the position [config.headerRows - 1] from being added to the result. + if (!this.isFirstRowInserted && config.headersRows >= 1) { + // To skip the row at the position [config.headersRows - 1] from being added to the result. this.isFirstRowIsHeader = true; this.isFirstRowInserted = true; return; @@ -606,12 +606,12 @@ private void insertHeaderValuesForTheCsvIfApplicable(Object obj, Type type) { private BString[] generateCsvHeadersForMappingRow(BMap map, Object headersOrder, int size) { BString[] keys = new BString[size]; if (headersOrder != null) { - String[] headerOrder = ((BArray) headersOrder).getStringArray(); - if (headerOrder.length != size) { + String[] order = ((BArray) headersOrder).getStringArray(); + if (order.length != size) { throw DiagnosticLog.error(DiagnosticErrorCode.INVALID_HEADER_NAMES_LENGTH); } for (int i = 0; i < size; i++) { - keys[i] = StringUtils.fromString(headerOrder[i]); + keys[i] = StringUtils.fromString(order[i]); } } else { if (headersForArrayConversion == null) { diff --git a/native/src/main/java/io/ballerina/lib/data/csvdata/utils/Constants.java b/native/src/main/java/io/ballerina/lib/data/csvdata/utils/Constants.java index 3205e02..8b12602 100644 --- a/native/src/main/java/io/ballerina/lib/data/csvdata/utils/Constants.java +++ b/native/src/main/java/io/ballerina/lib/data/csvdata/utils/Constants.java @@ -27,9 +27,9 @@ public final class ConfigConstants { public static final BString ENABLE_CONSTRAINT_VALIDATION = StringUtils. fromString("enableConstraintValidation"); public static final BString OUTPUT_WITH_HEADERS = StringUtils.fromString("outputWithHeaders"); - public static final BString HEADER_ROWS = StringUtils.fromString("headerRows"); + public static final BString HEADER_ROWS = StringUtils.fromString("headersRows"); public static final BString CUSTOM_HEADERS_IF_HEADER_ABSENT = - StringUtils.fromString("customHeadersIfHeaderAbsent"); + StringUtils.fromString("customHeadersIfHeadersAbsent"); public static final BString CUSTOM_HEADERS = StringUtils.fromString("customHeaders"); public static final BString HEADERS_ORDER = StringUtils.fromString("headersOrder"); diff --git a/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvConfig.java b/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvConfig.java index dd6bd56..eae6a63 100644 --- a/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvConfig.java +++ b/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvConfig.java @@ -26,8 +26,8 @@ public class CsvConfig { public boolean stringConversion = false; public boolean enableConstraintValidation = false; public boolean outputWithHeaders = false; - public Object customHeadersIfHeaderAbsent = null; - public long headerRows = 0; + public Object customHeadersIfHeadersAbsent = null; + public long headersRows = 0; public Object customHeaders = null; private CsvConfig(CsvConfig config) { @@ -58,14 +58,14 @@ public static CsvConfig createParseListOptions(BMap options) { public CsvConfig(boolean nilAsOptionalField, boolean absentAsNilableType, boolean allowDataProjection, Object skipLines, boolean enableConstraintValidation, boolean outputWithHeaders, - long headerRows, Object headers) { + long headersRows, Object headers) { this.nilAsOptionalField = nilAsOptionalField; this.absentAsNilableType = absentAsNilableType; this.allowDataProjection = allowDataProjection; this.skipLines = skipLines; this.enableConstraintValidation = enableConstraintValidation; this.outputWithHeaders = outputWithHeaders; - this.headerRows = headerRows; + this.headersRows = headersRows; this.customHeaders = headers; } @@ -118,7 +118,7 @@ public static CsvConfig createParseOptions(BMap options) { public CsvConfig(boolean nilAsOptionalField, boolean absentAsNilableType, boolean allowDataProjection, Object skipLines, boolean enableConstraintValidation, boolean outputWithHeaders, char delimiter, String locale, char textEnclosure, char escapeChar, Object lineTerminator, - Object nilValue, char comment, Object header, Object customHeadersIfHeaderAbsent) { + Object nilValue, char comment, Object header, Object customHeadersIfHeadersAbsent) { this.nilAsOptionalField = nilAsOptionalField; this.absentAsNilableType = absentAsNilableType; this.allowDataProjection = allowDataProjection; @@ -133,7 +133,7 @@ public CsvConfig(boolean nilAsOptionalField, boolean absentAsNilableType, boolea this.nilValue = nilValue; this.comment = comment; this.header = header; - this.customHeadersIfHeaderAbsent = customHeadersIfHeaderAbsent; + this.customHeadersIfHeadersAbsent = customHeadersIfHeadersAbsent; } public static CsvConfig createConfigOptionsForUnion(CsvConfig config) { diff --git a/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvUtils.java b/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvUtils.java index b7ff4c6..f891145 100644 --- a/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvUtils.java +++ b/native/src/main/java/io/ballerina/lib/data/csvdata/utils/CsvUtils.java @@ -74,7 +74,7 @@ public static boolean isBasicType(Type type) { public static String[] createHeadersForParseLists(BArray csvElement, String[] headers, CsvConfig config) { Object customHeaders = config.customHeaders; - long headerRows = config.headerRows; + long headersRows = config.headersRows; int length = headers.length; if (customHeaders instanceof BArray array) { @@ -87,15 +87,15 @@ public static String[] createHeadersForParseLists(BArray csvElement, String[] he return headers; } - if (headerRows == 1) { + if (headersRows == 1) { return csvElement.getStringArray(); } - if (headerRows > 1) { + if (headersRows > 1) { throw DiagnosticLog.error(DiagnosticErrorCode.NO_CUSTOM_HEADER_PROVIDED); } - // when headerRows = 0 and customHeaders = null + // when headersRows = 0 and customHeaders = null for (int i = 0; i < length; i++) { headers[i] = String.valueOf(i + 1); }