Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support to ballerina table and tuple type #16

Merged
merged 6 commits into from
Jun 30, 2022
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
140 changes: 138 additions & 2 deletions ballerina/tests/errors_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type EmployeeTable table<map<anydata>>;

@test:Config{}
public isolated function testUnsupportedDataType() returns error? {
string expected = "Unsupported data type: table";
string expected = "Unsupported data type: anydata";

Proto3Schema|error ser = new(EmployeeTable);

Expand Down Expand Up @@ -92,17 +92,153 @@ public isolated function testMapArrayUnionMemberNotYetSupporteError() returns er
test:assertEquals(err.message(), expectedErrorMsg);
}

public isolated function getErrorMessageForNonReferenceTypes(string typeName) returns string {
return "Type `" + typeName + "` not supported, use a reference type instead: "
+ "`type MyType " + typeName + ";`";
}

type RecordWithNonReferencedMapField record {
map<int> ages;
};

@test:Config {}
public isolated function testRecordWithNonReferencedMapFieldError() returns error? {
string expectedErrorMsg = "Record field of map type only supported with reference map type";
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("map<int>");

Proto3Schema|error ser = new(RecordWithNonReferencedMapField);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type RecordWithNonReferencedTableField record {
table<map<int>> ages;
};

@test:Config {}
public isolated function testRecordWithNonReferencedTableFieldError() returns error? {
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("table<map<int>>");

Proto3Schema|error ser = new(RecordWithNonReferencedTableField);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type RecordWithNonReferencedArrayTuple record {
[int, int][] field1;
};

@test:Config {}
public isolated function testRecordNonReferencedArrayOfTuplesError() returns error? {
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("[int,int]");

Proto3Schema|error ser = new (RecordWithNonReferencedArrayTuple);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type TableA table<map<int>>;
type TableB map<float>;
type UnionOfTables TableA|TableA;

@test:Config {}
public function testTableUnionMemberNotYetSupporteError() returns error? {
string expectedErrorMsg = "Serdes not yet support table type as union member";

Proto3Schema|error ser = new(UnionOfTables);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type UnionWithArrayOfTables TableA[]|TableB[][];

@test:Config {}
public isolated function testTableArrayUnionMemberNotYetSupporteError() returns error? {
string expectedErrorMsg = "Serdes not yet support array of tables as union member";

Proto3Schema|error ser = new(UnionWithArrayOfTables);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type RecordWithMapArrayField record {
AgeMap[] ages;
MapRecord[] mapRecords;
};

@test:Config {}
public isolated function testRecordWithMapArrayNotYetSupportedError() returns error? {
string expectedErrorMsg = "Serdes not yet support array of maps as record field";

Proto3Schema|error ser = new(RecordWithMapArrayField);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type UnionWithNonReferenceTupleMembers [int, byte]|[string, decimal];

@test:Config {}
public isolated function testUnionWithNonReferencedTupleTypeError() returns error? {
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("[int,byte]");

Proto3Schema|error ser = new(UnionWithNonReferenceTupleMembers);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type UnionWithNonReferencedArrayOfTupleMembers [int, byte][]|[string, decimal][];

@test:Config {}
public isolated function testUnionWithNonReferencedArrayOfTuplesError() returns error? {
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("[int,byte]");

Proto3Schema|error ser = new (UnionWithNonReferencedArrayOfTupleMembers);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type TupleWithNonReferenceArrayOfTuple [[int, int][], [boolean, float][]];

@test:Config {}
public isolated function testTupleWithNonReferenceArrayOfTuplesTypeError() returns error? {
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("[int,int]");

Proto3Schema|error ser = new (TupleWithNonReferenceArrayOfTuple);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}

type RecordWithNonReferencedRecordField record {
record {
string name;
int age;
} person;
};

@test:Config {}
public isolated function testRecordWithNonReferencedRecordFieldError() returns error? {
string expectedErrorMsg = getErrorMessageForNonReferenceTypes("record {| string name; int age; anydata...; |}");

Proto3Schema|error ser = new(RecordWithNonReferencedRecordField);

test:assertTrue(ser is Error);
Error err = <Error> ser;
test:assertEquals(err.message(), expectedErrorMsg);
}
18 changes: 18 additions & 0 deletions ballerina/tests/map_type_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type RecordWithMapField record {
AgeMap ages;
};

type MapWithTuple map<TupleWithUnion>;

@test:Config{}
public isolated function testMapInt() returns error? {

Expand Down Expand Up @@ -234,3 +236,19 @@ public isolated function testMapFieldinRecord() returns error? {

test:assertEquals(decode, data);
}

@test:Config{}
public isolated function testMapWithTupleElement() returns error? {
MapWithTuple data = {
"first": ["serdes", 1.2],
"second": ["module", 2.4]
};

Proto3Schema ser = check new(MapWithTuple);
byte[] encode = check ser.serialize(data);

Proto3Schema des = check new(MapWithTuple);
MapWithTuple decode = check des.deserialize(encode);

test:assertEquals(decode, data);
}
14 changes: 14 additions & 0 deletions ballerina/tests/multi_dimentional_array_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type AgeMap map<int>;
type AgeMapArray AgeMap[];
type AgeMap2DArray AgeMapArray[];

type ArrayOfTuples TupleWithUnion[][];

@test:Config {}
public isolated function testInt2DArray() returns error? {
Int2DArray data = [
Expand Down Expand Up @@ -195,3 +197,15 @@ public isolated function test2DArrayofMaps() returns error? {
AgeMap2DArray decoded = check des.deserialize(encoded);
test:assertEquals(decoded, data);
}

@test:Config {}
public isolated function testArrayOfTuples() returns error? {
ArrayOfTuples value = [[["serdes", 1.2d]],[["module", 3.4]]];

Proto3Schema ser = check new (ArrayOfTuples);
byte[] encoded = check ser.serialize(value);

Proto3Schema des = check new (ArrayOfTuples);
ArrayOfTuples decoded = check des.deserialize(encoded);
test:assertEquals(decoded, value);
}
60 changes: 59 additions & 1 deletion ballerina/tests/record_type_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ type Nested3 record {
Nested1? nested;
};

type UnionField1 int|int[]|string;
type UnionField2 decimal|int[];
type RecordWithUnionArrayField record {
UnionField1[] feild1;
UnionField2[] feild2;
};

type RecordWithTuple record {
TupleWithUnion field1;
PrimitiveTuple field2;
};

type RecordWithArrayOfTuple record {
TupleWithUnion[][] field1;
PrimitiveTuple[] field2;
};

@test:Config {}
public isolated function testRecordWithPrimitives() returns error? {
Employee jhon = {
Expand Down Expand Up @@ -230,6 +247,18 @@ public isolated function testRecordWithUnionFields() returns error? {
test:assertEquals(decoded, rec);
}

@test:Config {}
public isolated function testRecordWithUnionArrayField() returns error? {
RecordWithUnionArrayField rec = {feild1: ["serdes"], feild2: [2.3e10]};

Proto3Schema ser = check new (RecordWithUnionArrayField);
byte[] encoded = check ser.serialize(rec);

Proto3Schema des = check new (RecordWithUnionArrayField);
RecordWithUnionArrayField decoded = check des.deserialize(encoded);
test:assertEquals(decoded, rec);
}

@test:Config {}
public function testRecordWithMultidimentionalArrays() returns error? {
Proto3Schema ser = check new Proto3Schema(RecordWithMultidimentionalArrays);
Expand Down Expand Up @@ -289,11 +318,40 @@ public function testNestedRecordWithCyclicReference() returns error? {
};

Proto3Schema ser = check new (Nested1);
check ser.generateProtoFile("Nested.proto");
byte[] encode = check ser.serialize(data);

Proto3Schema des = check new (Nested1);
Nested1 decoded = check des.deserialize(encode);

test:assertEquals(decoded, data);
}

@test:Config {}
public isolated function testRecordWithTupleField() returns error? {
RecordWithTuple value = {
field1: ["serdes", 4.5],
field2: [100, 30000, 0.0, false, "serdes", 4.999999999d]
};

Proto3Schema ser = check new (RecordWithTuple);
byte[] encoded = check ser.serialize(value);

Proto3Schema des = check new (RecordWithTuple);
RecordWithTuple decoded = check des.deserialize(encoded);
test:assertEquals(decoded, value);
}

@test:Config {}
public isolated function testRecordWithArrayOfTuple() returns error? {
RecordWithArrayOfTuple value = {
field1: [[["serdes", 4.5]]],
field2: [[100, 30000, 0.0, false, "serdes", 4.999999999d]]
};

Proto3Schema ser = check new (RecordWithArrayOfTuple);
byte[] encoded = check ser.serialize(value);

Proto3Schema des = check new (RecordWithArrayOfTuple);
RecordWithArrayOfTuple decoded = check des.deserialize(encoded);
test:assertEquals(decoded, value);
}
Loading