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

Refactor the union type implementation #26

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 @@ -472,7 +472,7 @@ function testParseListsWithOutputHeaders() {
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"]], {headersRows: 1});
(boolean|int|string)[][]|csv:Error ct1bt1_2 = csv:parseList([["a", "b", "c"], ["a", "true", "1"]], {headersRows: 1});
test:assertEquals(ct1bt1_2, [
["a", true, 1]
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,14 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedType7() {
[["a", "1", "true", "0", "2.23", "null"], ["a", "1", "true", "2.23", "0", "()"]],
{customHeaders: ["f", "e", "d", "c", "b", "a"]});
test:assertEquals(ct1br9_2, [
{a: (), b: <float>2.23, c: <decimal>0, d: "true", e: 1, f: "a"},
{a: (), b: <float>0, c: <decimal>2.23, d: "true", e: 1, f: "a"}
]);

record{|int|() a; float b; decimal? c; boolean|string d; int|string e; string f; string...;|}[]|csv:Error ct1br9_3 = csv:parseList(
[["a", "1", "true", "0", "2.23", "null"], ["a", "1", "true", "2.23", "0", "()"]],
{customHeaders: ["f", "e", "d", "c", "b", "a"]});
test:assertEquals(ct1br9_3, [
{a: (), b: <float>2.23, c: <decimal>0, d: true, e: 1, f: "a"},
{a: (), b: <float>0, c: <decimal>2.23, d: true, e: 1, f: "a"}
]);
Expand Down Expand Up @@ -593,7 +601,7 @@ function testFromCsvWithTypeForTupleAndRecordAsExpectedTypeWithHeaders() {
test:assertTrue(ct1br4_5_2 is csv:Error);
test:assertEquals((<error>ct1br4_5).message(), "Custom headers should be provided");

map<int|string|boolean>[]|csv:Error ct2br4_3 = csv:parseList([["a", "1", "true"], ["a", "1", "true"], ["a", "1", "true"]], {headersRows: 1, customHeaders: ["a", "c", "b"], outputWithHeaders: true});
map<int|boolean|string>[]|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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function testArrayIndexes() {
5, 6, 7
7, 8, 9`;

record {}[2]|csv:Error rec = csv:parseString(csv);
record{}[2]|csv:Error rec = csv:parseString(csv);
test:assertEquals(rec, [
{a: 1, b: 2},
{a: 3, b: 4}
Expand Down Expand Up @@ -407,20 +407,27 @@ function testParseStringArrayAsExpectedTypeWithOutputHeaders() {
["true", "false", "true", "false"]
]);

(string|boolean)[][]|csv:Error cv2baa = csv:parseString(csvStringWithBooleanValues2, {outputWithHeaders: true});
(boolean|string)[][]|csv:Error cv2baa = csv:parseString(csvStringWithBooleanValues2, {outputWithHeaders: true});
test:assertEquals(cv2baa, [
["b1", "b2", "b3", "b4", "b5"],
[true, false, true, false, true],
[true, false, true, false, true]
]);

[string...][]|csv:Error cv2baa_2 = csv:parseString(csvStringWithBooleanValues2, {outputWithHeaders: true});
(string|boolean)[][]|csv:Error cv2baa_2 = csv:parseString(csvStringWithBooleanValues2, {outputWithHeaders: true});
test:assertEquals(cv2baa_2, [
["b1", "b2", "b3", "b4", "b5"],
["true", "false", "true", "false", "true"],
["true", "false", "true", "false", "true"]
]);

[string...][]|csv:Error cv2baa_2_2 = csv:parseString(csvStringWithBooleanValues2, {outputWithHeaders: true});
test:assertEquals(cv2baa_2_2, [
["b1", "b2", "b3", "b4", "b5"],
["true", "false", "true", "false", "true"],
["true", "false", "true", "false", "true"]
]);

[boolean|string...][]|csv:Error cv2baa_3 = csv:parseString(csvStringWithBooleanValues2, {outputWithHeaders: true});
test:assertEquals(cv2baa_3, [
["b1", "b2", "b3", "b4", "b5"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ function testFromCsvStringWithTypeForStringAndRecordAsExpectedType4() {
test:assertTrue(csvb1br12 is csv:Error);
test:assertEquals((<csv:Error>csvb1br12).message(), common:generateErrorMessageForMissingRequiredField("requiredField"));

BooleanRecord13_2[]|csv:Error csvb1br13_2 = csv:parseString(csvStringWithBooleanValues1, {});
test:assertEquals(csvb1br13_2, [
{b1: "true", b2: "false", b3: "true", b4: "false", defaultableField: "", nillableField: ()},
{b1: "true", b2: "false", b3: "true", b4: "false", defaultableField: "", nillableField: ()},
{b1: "true", b2: "false", b3: "true", b4: "false", defaultableField: "", nillableField: ()}
]);

BooleanRecord13Array|csv:Error csvb1br13 = csv:parseString(csvStringWithBooleanValues1, {});
test:assertEquals(csvb1br13, [
{b1: true, b2: false, b3: true, b4: false, defaultableField: "", nillableField: ()},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ type BooleanRecord12 record {|
|};

type BooleanRecord13 record {|
string defaultableField = "";
string? nillableField = ();
boolean|string...;
|};

type BooleanRecord13_2 record {|
string defaultableField = "";
string? nillableField = ();
string|boolean...;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
3,string3,true`);

test:assertEquals(r2a, [
{a: 1, b: "string", c: true},
{a: 2, b: "string2", c: false},
{a: 3, b: "string3", c: true}
{a: 1d, b: "string", c: "true"},
{a: 2d, b: "string2", c: "false"},
{a: 3d, b: "string3", c: "true"}
]);

record {A2 a; B2 b; C2 c;}[]|csv:Error r3a = csv:parseString(string `a,b,c
Expand Down Expand Up @@ -190,9 +190,9 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
3,string3,true`);

test:assertEquals(r17a, [
[1, "string", true],
[2, "string2", false],
[3, "string3", true]
[1d, "string", "true"],
[2d, "string2", "false"],
[3d, "string3", "true"]
]);

[A2, B2, C2][]|csv:Error r18a = csv:parseString(
Expand Down Expand Up @@ -299,9 +299,9 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
, {headersOrder: ["a", "b", "c"]});

test:assertEquals(rt7a, [
[<decimal>1, "string", true],
[<decimal>2, "string2", false],
[<decimal>3, "string3", true]
[1d, "string", true],
[2d, "string2", false],
[3d, "string3", true]
]);

[A2, B2, C2][]|csv:Error rt8a = csv:transform(
Expand Down Expand Up @@ -348,18 +348,27 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]], {customHeaders: ["a", "b", "c"]});

test:assertEquals(rt12a, [
{a: 1, b: "string", c: true},
{a: 2, b: "string2", c: false},
{a: 3, b: "string3", c: true}
{a: 1d, b: "string", c: "true"},
{a: 2d, b: "string2", c: "false"},
{a: 3d, b: "string3", c: "true"}
]);

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, [
{a: <decimal>1, b: "string", c: true},
{a: <decimal>2, b: "string2", c: false},
{a: <decimal>3, b: "string3", c: true}
{a: "1", b: "string", c: "true"},
{a: "2", b: "string2", c: "false"},
{a: "3", b: "string3", c: "true"}
]);

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

test:assertEquals(rt12a_4, [
{a: <decimal>1, b: "string", c: "true"},
{a: <decimal>2, b: "string2", c: "false"},
{a: <decimal>3, b: "string3", c: "true"}
]);

record {A2 a; B2 b; C2 c;}[]|csv:Error rt13a = csv:parseList(
Expand Down Expand Up @@ -409,9 +418,9 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
[["1", "string", "true"], ["2", "string2", "false"], ["3", "string3", "true"]]);

test:assertEquals(rt17a, [
[1, "string", true],
[2, "string2", false],
[3, "string3", true]
[1d, "string", "true"],
[2d, "string2", "false"],
[3d, "string3", "true"]
]);

[A2, B2, C2][]|csv:Error rt18a = csv:parseList(
Expand Down Expand Up @@ -441,3 +450,61 @@ function testFromCsvWithIntersectionTypeCompatibility2() {
["3", "string3", true, "string3"]
]);
}

@test:Config
function testSliceOperation() {
string[][] v = [["1", "2"], ["3", "4"], ["a", "b"]];
var v2 = [{a: 1, b: 2}, {a: 3, b: 4}, {a: "a", b: "b"}];
string v3 = string `a,b
1,2
3,4
a,b`;

int[2][]|error c = csv:parseList(v);
test:assertEquals(c, [[1, 2], [3, 4]]);

record{|int...;|}[2]|error c2 = csv:parseList(v, {customHeaders: ["a", "b"]});
test:assertEquals(c2, [{a: 1, b: 2}, {a: 3, b: 4}]);

int[2][]|error c3 = csv:transform(v2, {headersOrder: ["a", "b"]});
test:assertEquals(c3, [[1, 2], [3, 4]]);

record{|int...;|}[2]|error c4 = csv:transform(v2);
test:assertEquals(c4, [{a: 1, b: 2}, {a: 3, b: 4}]);

int[2][]|error c5 = csv:parseString(v3);
test:assertEquals(c5, [[1, 2], [3, 4]]);

record{|int...;|}[2]|error c6 = csv:parseString(v3);
test:assertEquals(c6, [{a: 1, b: 2}, {a: 3, b: 4}]);
}

@test:Config
function testSliceOperation2() {
string[][] v = [["c", "c", "c"], ["1", "2", "a"], ["c", "c", "c"], ["3", "4", "a"], ["a", "b", "a"]];
var v2 = [{a: "c", b: "c", c: "c"}, {a: 1, b: 2, c: "c"}, {a: "c", b: "c", c: "c"}, {a: 3, b: 4, c: "c"}, {a: "a", b: "b", c: "c"}];
string v3 = string `a,b, c
c,c,c
1,2,c
c,c,c
3,4,c
a,b,c`;

int[2][2]|error c = csv:parseList(v, {skipLines: [1, 3]});
test:assertEquals(c, [[1, 2], [3, 4]]);

record{|int...;|}[2]|error c2 = csv:parseList(v, {customHeaders: ["a", "b", "c"], skipLines: [1, 3]});
test:assertEquals(c2, [{a: 1, b: 2}, {a: 3, b: 4}]);

int[2][2]|error c3 = csv:transform(v2, {headersOrder: ["a", "b", "c"], skipLines: [1, 3]});
test:assertEquals(c3, [[1, 2], [3, 4]]);

record{|int...;|}[2]|error c4 = csv:transform(v2, {skipLines: [1, 3]});
test:assertEquals(c4, [{a: 1, b: 2}, {a: 3, b: 4}]);

int[2][2]|error c5 = csv:parseString(v3, {skipLines: [1, 3]});
test:assertEquals(c5, [[1, 2], [3, 4]]);

record{|int...;|}[2]|error c6 = csv:parseString(v3, {skipLines: [1, 3]});
test:assertEquals(c6, [{a: 1, b: 2}, {a: 3, b: 4}]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function testIntersectionExpectedTypes() returns error? {
1,2
a,a`);
test:assertTrue(a9 is (((int[] & readonly)|([string, string] & readonly)) & readonly)[]);
test:assertEquals(a9, [[1, 2], ["a", "a"]]);
test:assertEquals(a9, [["1", "2"], ["a", "a"]]);

((record {string a; string b;} & readonly)|(record {int a; int b;} & readonly))[]
& readonly|csv:Error a10 = csv:parseString(string `a,b
Expand Down Expand Up @@ -115,13 +115,22 @@ function testIntersectionExpectedTypes2() returns error? {
test:assertEquals(a8, [{a: "a", b: "a"}, {a: "c", b: "c"}]);

(((int[] & readonly)|([string, string] & readonly)) & readonly)[]|csv:Error a9 = csv:transform([{"a": 1, "b": 2}, {"a": "a", "b": "b"}], {headersOrder: ["a", "b"]});
test:assertTrue(a9 is (((int[] & readonly)|([string, string] & readonly)) & readonly)[]);
test:assertEquals(a9, [[1, 2], ["a", "b"]]);
test:assertTrue(a9 is error);
test:assertEquals((<error> a9).message(), "The CSV cannot be converted into any of the uniform union types in '((int[] & readonly)|([string,string] & readonly))[]'");

(((int[] & readonly)|([string, string] & readonly)) & readonly)[]|csv:Error a9_2 = csv:transform([{"a": "1", "b": "2"}, {"a": "a", "b": "b"}], {headersOrder: ["a", "b"]});
test:assertTrue(a9_2 is (((int[] & readonly)|([string, string] & readonly)) & readonly)[]);
test:assertEquals(a9_2, [["1", "2"], ["a", "b"]]);

((record {string a; string b;} & readonly)|(record {int a; int b;} & readonly))[]
& readonly|csv:Error a10 = csv:transform([{"a": "a", "b": "a"}, {"a": 1, "b": 2}], {});
& readonly|csv:Error a10 = csv:transform([{"a": "a", "b": "a"}, {"a": "1", "b": "2"}], {});
test:assertTrue(a10 is ((record {string a; string b;} & readonly)|(record {int a; int b;} & readonly))[] & readonly);
test:assertEquals(a10, [{a: "a", b: "a"}, {a: 1, b: 2}]);
test:assertEquals(a10, [{a: "a", b: "a"}, {a: "1", b: "2"}]);

((record {string a; string b;} & readonly)|(record {int a; int b;} & readonly))[]
& readonly|csv:Error a10_2 = csv:transform([{"a": "a", "b": "a"}, {"a": 1, "b": 2}], {});
test:assertTrue(a10_2 is error);
test:assertEquals((<error> a10_2).message(), "The CSV cannot be converted into any of the uniform union types in '((union_type_tests:record {| string a; string b; anydata...; |} & readonly)|(union_type_tests:record {| int a; int b; anydata...; |} & readonly))[]'");
}

@test:Config
Expand Down Expand Up @@ -160,7 +169,7 @@ function testIntersectionExpectedTypes3() returns error? {

(((int[] & readonly)|([string, string] & readonly)) & readonly)[]|csv:Error a9 = csv:parseList([["1", "2"], ["a", "b"]], {});
test:assertTrue(a9 is (((int[] & readonly)|([string, string] & readonly)) & readonly)[]);
test:assertEquals(a9, [[1, 2], ["a", "b"]]);
test:assertEquals(a9, [["1", "2"], ["a", "b"]]);

((record {string a; string b;} & readonly)|(record {int a; int b;} & readonly))[]
& readonly|csv:Error a10 = csv:parseList([["a", "a"], ["1", "2"]], {customHeaders: ["a", "b"]});
Expand All @@ -170,5 +179,5 @@ function testIntersectionExpectedTypes3() returns error? {
((record {int a; int b;} & readonly)|(record {string a; string b;} & readonly))[]
& readonly|csv:Error a11 = csv:parseList([["a", "a"], ["1", "2"]], {customHeaders: ["a", "b"]});
test:assertTrue(a11 is ((record {string a; string b;} & readonly)|(record {int a; int b;} & readonly))[] & readonly);
test:assertEquals(a11, [{a: "a", b: "a"}, {a: 1, b: 2}]);
test:assertEquals(a11, [{a: "a", b: "a"}, {a: "1", b: "2"}]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function testSingletonExpectedTypes2() returns error? {
test:assertEquals((<error>a8).message(), common:generateErrorMessageForInvalidCast("c", "(\"a\"|\"d\")"));
}

type SubType byte|int:Signed8|int:Signed16|int:Signed32|string:Char|int:Unsigned8|int:Unsigned16|int:Unsigned32;
type SubType byte|int:Signed8|int:Signed16|int:Signed32|int:Unsigned8|int:Unsigned16|int:Unsigned32|string:Char;

type SubtypeRecord record {
byte a; int:Signed8 c; int:Signed16 d; int:Signed32 e;
Expand Down Expand Up @@ -142,6 +142,14 @@ function testSubtypeExpectedTypes() returns error? {
["1", "1", "1", "1", "a", "1", "1", "1"]];
var value3 = [[1, 1, 1, 1, "a", 1, 1, 1],
[1, 1, 1, 1, "a", 1, 1, 1]];
var value4 = [[1, 1, 1, 1, "a", 1, 1, 1],
[1, 1, 1, 1, "a", 1, 1, 1]];
var value5 = [{a: 1, c: 1, d: 1, e: 1, f: "a", g: 1, h: 1, i: 1},
{a: 1, c: 1, d: 1, e: 1, f: "a", g: 1, h: 1, i: 1}];
var value6 = [[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1]];
var value7 = [{a: 1, c: 1, d: 1, e: 1, g: 1, h: 1, i: 1},
{a: 1, c: 1, d: 1, e: 1, g: 1, h: 1, i: 1}];

SubtypeRecord[]|csv:Error a = csv:parseString(string `a, c, d, e, f, g, h, i
1, 1, 1, 1, a, 1, 1, 1
Expand All @@ -154,10 +162,15 @@ function testSubtypeExpectedTypes() returns error? {

test:assertEquals(a2, [{a: 1, c: 1}, {a: 1, c: 1}]);

SubtypeRecord3[]|csv:Error a3 = csv:parseString(string `a, c, d, e, f, g, h, i
SubtypeRecord3[]|csv:Error a3 = csv:parseString(string `a, c, d, e, g, h, i
1, 1, 1, 1, 1, 1, 1
1, 1, 1, 1, 1, 1, 1 `);
test:assertEquals(a3, value7);

SubtypeRecord3[]|csv:Error a3_2 = csv:parseString(string `a, c, d, e, f, g, h, i
1, 1, 1, 1, a, 1, 1, 1
1, 1, 1, 1, a, 1, 1, 1 `);
test:assertEquals(a3, value1);
test:assertEquals(a3_2, value5);

SubtypeTuple[]|csv:Error a4 = csv:parseString(string `a, c, d, e, f, g, h, i
1, 1, 1, 1, a, 1, 1, 1
Expand All @@ -172,7 +185,12 @@ function testSubtypeExpectedTypes() returns error? {
SubtypeTuple3[]|csv:Error a6 = csv:parseString(string `a, c, d, e, f, g, h, i
1, 1, 1, 1, a, 1, 1, 1
1, 1, 1, 1, a, 1, 1, 1 `);
test:assertEquals(a6, value3);
test:assertEquals(a6, value4);

SubtypeTuple3[]|csv:Error a6_2 = csv:parseString(string `a, c, d, e, g, h, i
1, 1, 1, 1, 1, 1, 1
1, 1, 1, 1, 1, 1, 1 `);
test:assertEquals(a6_2, value6);

SubtypeRecord[]|csv:Error a7 = csv:transform(value1, {});
test:assertEquals(a7, value1);
Expand Down
Loading
Loading