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

Update header parameter to be in nilable type #31

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
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ function testConstraintWithLists() returns error? {
ConstrainedList|csv:Error cList1 = csv:parseString(string `1
2
3
4`, {header: false, customHeadersIfHeadersAbsent: ["a", "b", "c", "d"]});
4`, {header: (), customHeadersIfHeadersAbsent: ["a", "b", "c", "d"]});
test:assertEquals(cList1, [[1], [2], [3], [4]]);

cList1 = csv:parseString(string `1
2
3
4
5
6`, {header: false, customHeadersIfHeadersAbsent: ["a", "b", "c", "d", "e", "f"]});
6`, {header: null, customHeadersIfHeadersAbsent: ["a", "b", "c", "d", "e", "f"]});
test:assertTrue(cList1 is csv:Error);
test:assertTrue((<error>cList1).message().startsWith("Validation failed")
&& (<error>cList1).message().includes("length"));
Expand All @@ -56,7 +56,7 @@ function testConstraintWithLists() returns error? {
3
4
5
6`, {header: false, customHeadersIfHeadersAbsent: ["a", "b", "c", "d", "e", "f"], enableConstraintValidation: false});
6`, {header: (), 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}], {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ function testParseStringArrayAsExpectedTypeWithOutputHeaders() {
[true, false, true, false, true]
]);

string[][]|csv:Error cv1baa_4 = csv:parseString(csvStringWithBooleanValues1, {outputWithHeaders: true, header: false});
string[][]|csv:Error cv1baa_4 = csv:parseString(csvStringWithBooleanValues1, {outputWithHeaders: true});
test:assertEquals(cv1baa_4, [
["b1", "b2", "b3", "b4"],
["true", "false", "true", "false"],
Expand Down Expand Up @@ -466,7 +466,13 @@ function testParseStringArrayAsExpectedTypeWithOutputHeaders() {
[true, false, true, false, true]
]);

string[2][2]|csv:Error cv1baa_9 = csv:parseString(csvStringWithBooleanValues1, {outputWithHeaders: true, header: false});
string[2][2]|csv:Error cv1baa_9 = csv:parseString(csvStringWithBooleanValues1, {outputWithHeaders: true, header: ()});
test:assertEquals(cv1baa_9, [
["b1", "b2"],
["true", "false"]
]);

cv1baa_9 = csv:parseString(csvStringWithBooleanValues1, {outputWithHeaders: true, header: null});
test:assertEquals(cv1baa_9, [
["b1", "b2"],
["true", "false"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,43 +145,43 @@ function testFromCsvStringWithHeaderLessParserOptions() {
{'1: 5, '2: "string5", '3: true, '4: 3, '5: 3, '6: ()}
]);

record {}[]|csv:Error csv1op6_2 = csv:parseString(csvStringData1, {header: false, skipLines: [3, 5]});
record {}[]|csv:Error csv1op6_2 = csv:parseString(csvStringData1, {header: null, skipLines: [3, 5]});
test:assertEquals(csv1op6_2, [
{'1: "a", '2: "b", '3: "c", '4: "d", '5: "e", '6: "f"},
{'1: 1, '2: "string1", '3: true, '4: 2.234, '5: 2.234, '6: ()},
{'1: 3, '2: "string3", '3: false, '4: 1.23, '5: 1.23, '6: ()},
{'1: 5, '2: "string5", '3: true, '4: 3, '5: 3, '6: ()}
]);

record {}[]|csv:Error csv3op6_2 = csv:parseString(csvStringData3, {header: false, skipLines: [1, 3, 5, -1, 100, 100]});
record {}[]|csv:Error csv3op6_2 = csv:parseString(csvStringData3, {header: (), skipLines: [1, 3, 5, -1, 100, 100]});
test:assertEquals(csv3op6_2, [
{'1: 1, '2: "string1", '3: true, '4: 2.234, '5: 2.234, '6: ()},
{'1: 3, '2: "string3", '3: false, '4: 1.23, '5: 1.23, '6: ()},
{'1: 5, '2: "string5", '3: true, '4: 3, '5: 3, '6: ()}
]);

record {}[]|csv:Error csv4op6_2 = csv:parseString(csvStringData4, {header: false, skipLines: [2, 4, -1, 100, 100]});
record {}[]|csv:Error csv4op6_2 = csv:parseString(csvStringData4, {header: (), skipLines: [2, 4, -1, 100, 100]});
test:assertEquals(csv4op6_2, [
{'1: 1, '2: "string1", '3: true, '4: 2.234, '5: 2.234, '6: ()},
{'1: 3, '2: "string3", '3: false, '4: 1.23, '5: 1.23, '6: ()},
{'1: 5, '2: "string5", '3: true, '4: 3, '5: 3, '6: ()}
]);

record {}[]|csv:Error csv5op6_2 = csv:parseString(csvStringData5, {header: false, skipLines: [2, 4, -1, 100, 100]});
record {}[]|csv:Error csv5op6_2 = csv:parseString(csvStringData5, {header: null, skipLines: [2, 4, -1, 100, 100]});
test:assertEquals(csv5op6_2, [
{'1: 1, '2: "string1", '3: true, '4: 2.234, '5: 2.234, '6: ()},
{'1: 3, '2: "string3", '3: false, '4: 1.23, '5: 1.23, '6: ()},
{'1: 5, '2: "string5", '3: true, '4: 3, '5: 3, '6: ()}
]);

record {}[]|csv:Error csv6op6_2 = csv:parseString(csvStringData6, {header: false, skipLines: [2, 4, -1, 100, 100]});
record {}[]|csv:Error csv6op6_2 = csv:parseString(csvStringData6, {header: (), skipLines: [2, 4, -1, 100, 100]});
test:assertEquals(csv6op6_2, [
{'1: 1, '2: "string1", '3: true, '4: 2.234, '5: 2.234, '6: ()},
{'1: 3, '2: "string3", '3: false, '4: 1.23, '5: 1.23, '6: ()},
{'1: 5, '2: "string5", '3: true, '4: 3, '5: 3, '6: ()}
]);

record {}[]|csv:Error csv2op6_2 = csv:parseString(csvStringData2, {header: false, skipLines: [5, 7]});
record {}[]|csv:Error csv2op6_2 = csv:parseString(csvStringData2, {header: (), skipLines: [5, 7]});
test:assertEquals(csv2op6_2, [
{'1: "hello", '2: "hello", '3: (), '4: 12, '5: true, '6: 12.34},
{'1: "// comment"},
Expand Down Expand Up @@ -684,11 +684,11 @@ function testCustomHeaderParserOption2() {
{a: 5, b: "string5", c: true, d: 3, e: 3, f: ()}
]);

record {}[]|csv:Error ct1br2 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: []});
record {}[]|csv:Error ct1br2 = csv:parseString(csvStringData4, {header: (), customHeadersIfHeadersAbsent: []});
test:assertTrue(ct1br2 is csv:Error);
test:assertEquals((<error>ct1br2).message(), "invalid number of headers");

record {int a; string b; boolean c; decimal d; float e; () f;}[]|csv:Error ct1br3 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["a", "b"]});
record {int a; string b; boolean c; decimal d; float e; () f;}[]|csv:Error ct1br3 = csv:parseString(csvStringData4, {header: null, customHeadersIfHeadersAbsent: ["a", "b"]});
test:assertTrue(ct1br3 is csv:Error);
test:assertEquals((<error>ct1br3).message(), "invalid number of headers");

Expand All @@ -704,7 +704,7 @@ function testCustomHeaderParserOption2() {
record {() a; float b; decimal c; boolean d; string e; int f;}[]|csv:Error ct1br5 = csv:parseString(csvStringData4, {header: 1});
test:assertTrue(ct1br5 is csv:Error);

ct1br5 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]});
ct1br5 = csv:parseString(csvStringData4, {header: (), customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]});
test:assertEquals(ct1br5, [
{f: 1, e: "string1", d: true, c: <decimal>2.234, b: <float>2.234, a: ()},
{f: 2, e: "string2", d: false, c: <decimal>0, b: <float>0, a: ()},
Expand All @@ -713,23 +713,23 @@ function testCustomHeaderParserOption2() {
{f: 5, e: "string5", d: true, c: <decimal>3, b: <float>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], customHeadersIfHeadersAbsent: ["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: null, skipLines: [1], customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]});
test:assertEquals(ct1br5_2, [
{f: 2, e: "string2", d: false, c: <decimal>0, b: <float>0, a: ()},
{f: 3, e: "string3", d: false, c: <decimal>1.23, b: <float>1.23, a: ()},
{f: 4, e: "string4", d: true, c: <decimal>-6.51, b: <float>-6.51, a: ()},
{f: 5, e: "string5", d: true, c: <decimal>3, b: <float>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, customHeadersIfHeadersAbsent: ["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: (), customHeadersIfHeadersAbsent: ["f", "e", "d", "c", "b", "a"]});
test:assertEquals(ct1br5_3, [
{f: 2, e: "string2", d: false, c: <decimal>0, b: <float>0, a: ()},
{f: 3, e: "string3", d: false, c: <decimal>1.23, b: <float>1.23, a: ()},
{f: 4, e: "string4", d: true, c: <decimal>-6.51, b: <float>-6.51, a: ()},
{f: 5, e: "string5", d: true, c: <decimal>3, b: <float>3, a: ()}
]);

record {|boolean d1; string e1;|}[]|csv:Error ct1br7 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
record {|boolean d1; string e1;|}[]|csv:Error ct1br7 = csv:parseString(csvStringData4, {header: (), customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
test:assertEquals(ct1br7, [
{e1: "string1", d1: true},
{e1: "string2", d1: false},
Expand All @@ -738,19 +738,19 @@ function testCustomHeaderParserOption2() {
{e1: "string5", d1: true}
]);

record {|boolean d1; string e1;|}[]|csv:Error ct1br7_2 = csv:parseString(csvStringData4, {header: false, skipLines: [1], customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
record {|boolean d1; string e1;|}[]|csv:Error ct1br7_2 = csv:parseString(csvStringData4, {header: null, skipLines: [1], customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
test:assertEquals(ct1br7_2, [
{e1: "string2", d1: false},
{e1: "string3", d1: false},
{e1: "string4", d1: true},
{e1: "string5", d1: true}
]);

record {|boolean d1; string e1;|}[]|csv:Error ct1br8 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["e1", "d1"]});
record {|boolean d1; string e1;|}[]|csv:Error ct1br8 = csv:parseString(csvStringData4, {header: (), customHeadersIfHeadersAbsent: ["e1", "d1"]});
test:assertTrue(ct1br8 is csv:Error);
test:assertEquals((<error>ct1br8).message(), common:generateErrorMessageForInvalidCast("string1", "boolean"));

record {|boolean d1; string e1;|}[]|csv:Error ct1br9 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
record {|boolean d1; string e1;|}[]|csv:Error ct1br9 = csv:parseString(csvStringData4, {header: (), customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
test:assertEquals(ct1br9, [
{e1: "string1", d1: true},
{e1: "string2", d1: false},
Expand All @@ -759,11 +759,11 @@ function testCustomHeaderParserOption2() {
{e1: "string5", d1: true}
]);

record {|boolean d1; string e1;|}[]|csv:Error ct1br11 = csv:parseString(csvStringData1, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1"]});
record {|boolean d1; string e1;|}[]|csv:Error ct1br11 = csv:parseString(csvStringData1, {header: (), customHeadersIfHeadersAbsent: ["f1", "e1"]});
test:assertTrue(ct1br11 is csv:Error);
test:assertEquals((<error>ct1br11).message(), "invalid number of headers");

record {|string d1; string e1;|}[]|csv:Error ct1br12 = csv:parseString(csvStringData4, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
record {|string d1; string e1;|}[]|csv:Error ct1br12 = csv:parseString(csvStringData4, {header: null, customHeadersIfHeadersAbsent: ["f1", "e1", "d1", "c1", "b1", "a1"]});
test:assertEquals(ct1br12, [
{e1: "string1", d1: "true"},
{e1: "string2", d1: "false"},
Expand All @@ -772,7 +772,7 @@ function testCustomHeaderParserOption2() {
{e1: "string5", d1: "true"}
]);

record {|string d1; string e1;|}[]|csv:Error ct1br13 = csv:parseString(csvStringData1, {header: false, customHeadersIfHeadersAbsent: ["f1", "e1", "dd1", "c1", "b1", "a1"]});
record {|string d1; string e1;|}[]|csv:Error ct1br13 = csv:parseString(csvStringData1, {header: (), customHeadersIfHeadersAbsent: ["f1", "e1", "dd1", "c1", "b1", "a1"]});
test:assertTrue(ct1br13 is csv:Error);
test:assertEquals((<error>ct1br13).message(), common:generateErrorMessageForMissingRequiredField("d1"));
}
Expand Down
4 changes: 2 additions & 2 deletions ballerina-tests/user-config-tests/tests/user_configs.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ final csv:ParseOptions option2 = {nilValue: "N/A", lineTerminator: [csv:CRLF, cs
final csv:ParseOptions option3 = {nilValue: "()", header: 1, skipLines: [1, 2]};
final csv:ParseOptions option4 = {nilValue: "", header: 4, skipLines: "1-5"};
final csv:ParseOptions option5 = {nilValue: "", header: 4, skipLines: "1-1"};
final csv:ParseOptions option6 = {nilValue: "()", header: false, skipLines: [1, 2]};
final csv:ParseOptions option6 = {nilValue: "()", header: (), skipLines: [1, 2]};

final csv:ParseOptions ptOption1 = {nilValue: "", header: 1, skipLines: [2, 4]};
final csv:ParseOptions ptOption2 = {nilValue: "", header: 1, skipLines: "2-4"};
final csv:ParseOptions ptOption3 = {nilValue: "", header: 4, skipLines: "1-5"};
final csv:ParseOptions ptOption4 = {nilValue: "", header: 4, skipLines: [-1, -2, 4, 2]};
final csv:ParseOptions ptOption5 = {header: false, skipLines: [-1, -2, 5, 3]};
final csv:ParseOptions ptOption5 = {header: (), skipLines: [-1, -2, 5, 3]};

// Invalid parser options
final csv:ParseOptions invalidParserOptions1 = {header: 4};
Expand Down
2 changes: 1 addition & 1 deletion ballerina/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public type ParseOptions record {|
# The character used to indicate comments in the data.
string:Char comment = "#";
# Specifies whether the header is present and, if so, the number of header lines.
false|int:Unsigned32 header = 0;
int:Unsigned32? header = 0;
# Custom headers for the data, if headers are absent.
string[]? customHeadersIfHeadersAbsent = ();
|};
Expand Down
Loading
Loading