Skip to content

Commit

Permalink
Rename parse list api
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Aug 8, 2024
1 parent e336a7a commit 91d9a3d
Show file tree
Hide file tree
Showing 20 changed files with 468 additions and 467 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Book record {
public function main() returns error? {
string[][] bookArray = [["Clean Code","2008"],["Clean Architecture","2017"]];
Book[] author = check csv:parseLists(bookArray, customHeaders = ["name", "year"]);
Book[] author = check csv:parseList(bookArray, customHeaders = ["name", "year"]);
io:println(author);
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function testConstraintWithLists() returns error? {
test:assertTrue((<error>cList1).message().startsWith("Validation failed")
&& (<error>cList1).message().includes("length"));

cList1 = csv:parseLists([["1"], ["2"], ["3"], ["4"]], {customHeaders: ["a", "b", "c", "d"]});
cList1 = csv:parseList([["1"], ["2"], ["3"], ["4"]], {customHeaders: ["a", "b", "c", "d"]});
test:assertEquals(cList1, [[1], [2], [3], [4]]);

cList1 = csv:parseLists([["1"], ["2"], ["3"], ["4"], ["5"], ["6"]], {customHeaders: ["a", "b", "c", "d"]});
cList1 = csv:parseList([["1"], ["2"], ["3"], ["4"], ["5"], ["6"]], {customHeaders: ["a", "b", "c", "d"]});
test:assertTrue(cList1 is csv:Error);
test:assertTrue((<error>cList1).message().startsWith("Validation failed")
&& (<error>cList1).message().includes("length"));
Expand Down Expand Up @@ -115,15 +115,15 @@ function testConstraintWithRecords() returns error? {
test:assertTrue((<error>cRec1).message().startsWith("Validation failed")
&& (<error>cRec1).message().includes("minLength"));

cRec1 = csv:parseLists([["4", "abc"], ["3", "cde"]], {customHeaders: ["a", "b"]});
cRec1 = csv:parseList([["4", "abc"], ["3", "cde"]], {customHeaders: ["a", "b"]});
test:assertEquals(cRec1, [{a: 4, b: "abc"}, {a: 3, b: "cde"}]);

cRec1 = csv:parseLists([["4", "abc"], ["11", "cde"]], {customHeaders: ["a", "b"]});
cRec1 = csv:parseList([["4", "abc"], ["11", "cde"]], {customHeaders: ["a", "b"]});
test:assertTrue(cRec1 is csv:Error);
test:assertTrue((<error>cRec1).message().startsWith("Validation failed")
&& (<error>cRec1).message().includes("maxValue"));

cRec1 = csv:parseLists([["4", "abc"], ["5", "b"]], {customHeaders: ["a", "b"]});
cRec1 = csv:parseList([["4", "abc"], ["5", "b"]], {customHeaders: ["a", "b"]});
test:assertTrue(cRec1 is csv:Error);
test:assertTrue((<error>cRec1).message().startsWith("Validation failed")
&& (<error>cRec1).message().includes("minLength"));
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,10 @@ function testsRecordsOutputWithHeadrs() {
bm3anyd3t_4 = csv:transform([{"a": 1, "b": 2}, {"c": 3, "d": 4}], {outputWithHeaders: true});
test:assertEquals(bm3anyd3t_4, [{"a": 1, "b": 2}, {"c": 3, "d": 4}]);

bm3anyd3t_3 = csv:parseLists([["a", "b"], ["c", "d", "e"]], {outputWithHeaders: true});
bm3anyd3t_3 = csv:parseList([["a", "b"], ["c", "d", "e"]], {outputWithHeaders: true});
test:assertEquals(bm3anyd3t_3, [["a", "b"], ["c", "d", "e"]]);

bm3anyd3t_4 = csv:parseLists([["a", "b"], ["c", "d", "e"]], {outputWithHeaders: true});
bm3anyd3t_4 = csv:parseList([["a", "b"], ["c", "d", "e"]], {outputWithHeaders: true});
test:assertTrue(bm3anyd3t_4 is csv:Error);
test:assertEquals((<error> bm3anyd3t_4).message(), "CSV data rows with varying headers are not yet supported");
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
readonly & int a;
readonly & string b;
(readonly & boolean) | (readonly & decimal) c;
}[]|csv:Error rt11a = csv:parseLists(
}[]|csv:Error rt11a = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {customHeaders: ["a", "b", "c"]});

test:assertEquals(rt11a , [
Expand All @@ -336,7 +336,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
{a: 3, b: "string3", c: true}
]);

record{A a; B b; C c;}[]|csv:Error rt12a = csv:parseLists(
record{A a; B b; C c;}[]|csv:Error rt12a = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {customHeaders: ["a", "b", "c"]});

test:assertEquals(rt12a , [
Expand All @@ -345,14 +345,14 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
{a: 3, b: "string3", c: true}
]);

record{A a; B b; C c;}[]|csv:Error rt12a_2 = csv:parseLists(
record{A a; B b; C c;}[]|csv:Error rt12a_2 = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {customHeaders: ["a", "b", "c"], stringConversion: false});

test:assertTrue(rt12a_2 is csv:Error);
test:assertEquals((<csv:Error>rt12a_2).message(),
common:generateErrorMessageForInvalidFieldType("1", "a"));

record{string|decimal a; B b; C c;}[]|csv:Error rt12a_3 = csv:parseLists(
record{string|decimal a; B b; C c;}[]|csv:Error rt12a_3 = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {customHeaders: ["a", "b", "c"]});

test:assertEquals(rt12a_3 , [
Expand All @@ -361,7 +361,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
{a: <decimal>3, b: "string3", c: true}
]);

record{A2 a; B2 b; C2 c;}[]|csv:Error rt13a = csv:parseLists(
record{A2 a; B2 b; C2 c;}[]|csv:Error rt13a = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {customHeaders: ["a", "b", "c"]});

test:assertEquals(rt13a , [
Expand All @@ -370,7 +370,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
{a: <int>3, b: "string3", c: true}
]);

record{|A2 a; B2 b; C2...;|}[]|csv:Error rt14a = csv:parseLists(
record{|A2 a; B2 b; C2...;|}[]|csv:Error rt14a = csv:parseList(
[["1", "string", "true", "string"], ["2", "string2", "false", "string2"], ["3", "string3", "true", "string3"]]
, {customHeaders: ["a", "b", "c", "d"]});

Expand All @@ -380,7 +380,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
{a: <int>3, b: "string3", c: true, d: "string3"}
]);

record{|C2...;|}[]|csv:Error rt15a = csv:parseLists(
record{|C2...;|}[]|csv:Error rt15a = csv:parseList(
[["1", "string", "true", "string"], ["2", "string2", "false", "string2"], ["3", "string3", "true", "string3"]]
, {customHeaders: ["a", "b", "c", "d"]});

Expand All @@ -390,7 +390,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
{a: "3", b: "string3", c: true, d: "string3"}
]);

record{|C2...;|}[]|csv:Error rt15a_2 = csv:parseLists(
record{|C2...;|}[]|csv:Error rt15a_2 = csv:parseList(
[["1", "string", "true", "string"], ["2", "string2", "false", "string2"], ["3", "string3", "true", "string3"]],
{stringConversion: false, customHeaders: ["a", "b", "c", "d"]});

Expand All @@ -401,7 +401,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
]);

[readonly & int, readonly & string, (readonly & boolean) | (readonly & decimal)][]|csv:Error rt16a =
csv:parseLists(
csv:parseList(
[["1", "string", "true"],
["2", "string2", "false"],
["3", "string3", "true"]]);
Expand All @@ -412,7 +412,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
[3, "string3", true]
]);

[A, B, C][]|csv:Error rt17a = csv:parseLists(
[A, B, C][]|csv:Error rt17a = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]]);

test:assertEquals(rt17a , [
Expand All @@ -421,13 +421,13 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
[3, "string3", true]
]);

[A, B, C][]|csv:Error rt17a_2 = csv:parseLists(
[A, B, C][]|csv:Error rt17a_2 = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {stringConversion: false});

test:assertTrue(rt17a_2 is csv:Error);
test:assertEquals((<error>rt17a_2).message(), common:generateErrorMessageForInvalidValueForArrayType("1", "0", "type_compatible_tests:A"));

[A2, B2, C2][]|csv:Error rt18a = csv:parseLists(
[A2, B2, C2][]|csv:Error rt18a = csv:parseList(
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]]);

test:assertEquals(rt18a , [
Expand All @@ -436,7 +436,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
[<int>3, "string3", true]
]);

[A2, B2, C2...][]|csv:Error rt19a = csv:parseLists(
[A2, B2, C2...][]|csv:Error rt19a = csv:parseList(
[["1", "string", "true", "string"], ["2", "string2", "false", "string2"], ["3", "string3", "true", "string3"]]);

test:assertEquals(rt19a , [
Expand All @@ -445,7 +445,7 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
[<int>3, "string3", true, "string3"]
]);

[C2...][]|csv:Error rt20a = csv:parseLists(
[C2...][]|csv:Error rt20a = csv:parseList(
[["1", "string", "true", "string"], ["2", "string2", "false", "string2"], ["3", "string3", "true", "string3"]]);

test:assertEquals(rt20a, [["1", "string",true, "string"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ csv:TransformOptions op4 = {headersOrder: ["a", "b"], outputWithHeaders: true};

csv:ParseOptions op5 = {outputWithHeaders: true};

csv:ParseListsOptions op6 = {outputWithHeaders: true};
csv:ParseListsOptions op7 = {outputWithHeaders: true, headerRows: 1};
csv:ParseListsOptions op8 = {outputWithHeaders: true, headerRows: 2};
csv:ParseListsOptions op9 = {outputWithHeaders: true, headerRows: 2, customHeaders: ["a", "b", "c", "d"]};
csv:ParseListsOptions op10 = {outputWithHeaders: true, headerRows: 1, customHeaders: ["a", "b", "c", "d"]};
csv:ParseListsOptions op11 = {outputWithHeaders: true, customHeaders: ["a", "b", "c", "d"]};
csv:ParseListsOptions op12 = {headerRows: 1, customHeaders: ["a", "b", "c", "d"]};
csv:ParseListsOptions op13 = {customHeaders: ["a", "b", "c", "d"]};
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 op11 = {outputWithHeaders: true, customHeaders: ["a", "b", "c", "d"]};
csv:ParseListOptions op12 = {headerRows: 1, customHeaders: ["a", "b", "c", "d"]};
csv:ParseListOptions op13 = {customHeaders: ["a", "b", "c", "d"]};

type UnionType1 boolean[][]|string[][];

Expand Down Expand Up @@ -135,120 +135,120 @@ function testTransformWithListWithOutputHeaders() {

@test:Config
function testParseListWithMapWithOutputHeaders() {
UnionType3|csv:Error psu3 = csv:parseLists(csv1);
UnionType3|csv:Error psu3 = csv:parseList(csv1);
test:assertEquals(psu3, result7);

psu3 = csv:parseLists(csv1, op6);
psu3 = csv:parseList(csv1, op6);
test:assertEquals(psu3, result7);

psu3 = csv:parseLists(csv1, op7);
psu3 = csv:parseList(csv1, op7);
test:assertEquals(psu3, [{"1": 5, "2": 6, "3": 7, "4": 8}, {"1": 9, "2": 10, "3": 11, "4": 12}, {"1": 13, "2": 14, "3": 15, "4": 16}]);

psu3 = csv:parseLists(csv1, op9);
psu3 = csv:parseList(csv1, op9);
test:assertEquals(psu3, [{"a": 9, "b": 10, "c": 11, "d": 12}, {"a": 13, "b": 14, "c": 15, "d": 16}]);

UnionType4|csv:Error psu4 = csv:parseLists(csv1);
UnionType4|csv:Error psu4 = csv:parseList(csv1);
test:assertEquals(psu4, result8);

psu4 = csv:parseLists(csv1, op6);
psu4 = csv:parseList(csv1, op6);
test:assertEquals(psu4, result8);

psu4 = csv:parseLists(csv1, op7);
psu4 = csv:parseList(csv1, op7);
test:assertEquals(psu4, [{"1": "5", "2": "6", "3": "7", "4": "8"}, {"1": "9", "2": "10", "3": "11", "4": "12"}, {"1": "13", "2": "14", "3": "15", "4": "16"}]);

psu4 = csv:parseLists(csv1, op9);
psu4 = csv:parseList(csv1, op9);
test:assertEquals(psu4, [{"a": "9", "b": "10", "c": "11", "d": "12"}, {"a": "13", "b": "14", "c": "15", "d": "16"}]);
}

@test:Config
function testParseListWithListWithOutputHeaders() {
UnionType1|csv:Error psu1 = csv:parseLists(csv1);
UnionType1|csv:Error psu1 = csv:parseList(csv1);
test:assertEquals(psu1, result4);

psu1 = csv:parseLists(csv1, op6);
psu1 = csv:parseList(csv1, op6);
test:assertEquals(psu1, result4);

psu1 = csv:parseLists(csv1, op7);
psu1 = csv:parseList(csv1, op7);
test:assertEquals(psu1, result4);

psu1 = csv:parseLists(csv1, op9);
psu1 = csv:parseList(csv1, op9);
test:assertEquals(psu1, [["a","b","c","d"],["9","10","11","12"],["13","14","15","16"]]);

UnionType2|csv:Error psu2 = csv:parseLists(csv1);
UnionType2|csv:Error psu2 = csv:parseList(csv1);
test:assertEquals(psu2, result4);

psu2 = csv:parseLists(csv1, op6);
psu2 = csv:parseList(csv1, op6);
test:assertEquals(psu2, result4);

psu2 = csv:parseLists(csv1, op7);
psu2 = csv:parseList(csv1, op7);
test:assertEquals(psu2, result4);

psu2 = csv:parseLists(csv1, op9);
psu2 = csv:parseList(csv1, op9);
test:assertEquals(psu2, [["a","b","c","d"],["9","10","11","12"],["13","14","15","16"]]);
}

@test:Config
function testParseListWithMapWithOutputHeaders2() {
UnionType3|csv:Error psu3 = csv:parseLists(csv1);
UnionType3|csv:Error psu3 = csv:parseList(csv1);
test:assertEquals(psu3, result7);

psu3 = csv:parseLists(csv1, op10);
psu3 = csv:parseList(csv1, op10);
test:assertEquals(psu3, [{"a":5,"b":6,"c":7,"d":8},{"a":9,"b":10,"c":11,"d":12},{"a":13,"b":14,"c":15,"d":16}]);

psu3 = csv:parseLists(csv1, op11);
psu3 = csv:parseList(csv1, op11);
test:assertEquals(psu3, result2);

psu3 = csv:parseLists(csv1, op12);
psu3 = csv:parseList(csv1, op12);
test:assertEquals(psu3, [{"a":5,"b":6,"c":7,"d":8},{"a":9,"b":10,"c":11,"d":12},{"a":13,"b":14,"c":15,"d":16}]);

psu3 = csv:parseLists(csv1, op13);
psu3 = csv:parseList(csv1, op13);
test:assertEquals(psu3, result2);

UnionType4|csv:Error psu4 = csv:parseLists(csv1);
UnionType4|csv:Error psu4 = csv:parseList(csv1);
test:assertEquals(psu4, result8);

psu4 = csv:parseLists(csv1, op10);
psu4 = csv:parseList(csv1, op10);
test:assertEquals(psu4, [{a: "5", b: "6", c: "7", d: "8"}, {a: "9", b: "10", c: "11", d: "12"}, {a: "13", b: "14", c: "15", d: "16"}]);

psu4 = csv:parseLists(csv1, op11);
psu4 = csv:parseList(csv1, op11);
test:assertEquals(psu4, result3);

psu4 = csv:parseLists(csv1, op12);
psu4 = csv:parseList(csv1, op12);
test:assertEquals(psu4, [{a: "5", b: "6", c: "7", d: "8"}, {a: "9", b: "10", c: "11", d: "12"}, {a: "13", b: "14", c: "15", d: "16"}]);

psu4 = csv:parseLists(csv1, op13);
psu4 = csv:parseList(csv1, op13);
test:assertEquals(psu4, result3);
}

@test:Config
function testParseListWithListWithOutputHeaders2() {
UnionType1|csv:Error psu1 = csv:parseLists(csv1);
UnionType1|csv:Error psu1 = csv:parseList(csv1);
test:assertEquals(psu1, result4);

psu1 = csv:parseLists(csv1, op10);
psu1 = csv:parseList(csv1, op10);
test:assertEquals(psu1, [["a","b","c","d"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]);

psu1 = csv:parseLists(csv1, op11);
psu1 = csv:parseList(csv1, op11);
test:assertEquals(psu1, result6);

psu1 = csv:parseLists(csv1, op12);
psu1 = csv:parseList(csv1, op12);
test:assertEquals(psu1, [["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]);

psu1 = csv:parseLists(csv1, op13);
psu1 = csv:parseList(csv1, op13);
test:assertEquals(psu1, result4);

UnionType2|csv:Error psu2 = csv:parseLists(csv1);
UnionType2|csv:Error psu2 = csv:parseList(csv1);
test:assertEquals(psu2, result4);

psu2 = csv:parseLists(csv1, op10);
psu2 = csv:parseList(csv1, op10);
test:assertEquals(psu2, [["a","b","c","d"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]);

psu2 = csv:parseLists(csv1, op11);
psu2 = csv:parseList(csv1, op11);
test:assertEquals(psu2, [["a","b","c","d"], ["1", "2", "3", "4"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]);

psu2 = csv:parseLists(csv1, op12);
psu2 = csv:parseList(csv1, op12);
test:assertEquals(psu2, [["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]);

psu2 = csv:parseLists(csv1, op13);
psu2 = csv:parseList(csv1, op13);
test:assertEquals(psu2, [["1", "2", "3", "4"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]);
}
Loading

0 comments on commit 91d9a3d

Please sign in to comment.