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

Support default values for record and fix usage of value creator apis #20

Merged
merged 14 commits into from
Jul 25, 2024
Merged
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "data.xmldata"
version = "0.1.3"
version = "0.1.4"
authors = ["Ballerina"]
keywords = ["xml"]
repository = "https://github.com/ballerina-platform/module-ballerina-data-xmldata"
Expand All @@ -12,5 +12,5 @@ export = ["data.xmldata"]
[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "data-native"
version = "0.1.3"
path = "../native/build/libs/data.xmldata-native-0.1.3.jar"
version = "0.1.4"
path = "../native/build/libs/data.xmldata-native-0.1.4-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "constraint-compiler-plugin"
class = "io.ballerina.lib.data.xmldata.compiler.XmldataCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.3.jar"
path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.4-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.9.0"
[[package]]
org = "ballerina"
name = "data.xmldata"
version = "0.1.3"
version = "0.1.4"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down
284 changes: 210 additions & 74 deletions ballerina/tests/fromXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1620,52 +1620,6 @@ function testCommentMiddleInContent2() returns error? {
test:assertEquals(rec2.A, "John Doe");
}

@test:Config
function testRegexAsFieldTypeWithParseString() returns error? {
string xmlStr = string `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|} rec1 = check parseString(xmlStr);
test:assertEquals(rec1.length(), 3);
test:assertEquals(rec1.A, [1, 2]);
test:assertEquals(rec1.B, "Code");
test:assertEquals(rec1.C.name, "Kanth");
}

@test:Config
function testRegexAsFieldTypeWithParseAsType() returns error? {
xml xmlVal = xml `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|} rec1 = check parseAsType(xmlVal);
test:assertEquals(rec1.length(), 3);
test:assertEquals(rec1.A, [1, 2]);
test:assertEquals(rec1.B, "Code");
test:assertEquals(rec1.C.name, "Kanth");
}

@test:Config
function testAnydataAsRestFieldWithParseString() returns error? {
string xmlStr = string `<Data>
Expand Down Expand Up @@ -2812,6 +2766,128 @@ function testProjectionWithXmlAttributeForParseAsType() returns error? {
test:assertEquals(rec.A, "2");
}

type DataN3 record {|
int[4] A;
|};

@test:Config {
groups: ["fromXmlString"]
}
function testXmlStringToRecordWithArrayAsFieldType() returns error? {
string xmlStr1 = "<Data><A>1</A><A>2</A><A>3</A></Data>";
DataN3 rec1 = check parseString(xmlStr1);
test:assertEquals(rec1.A, [1, 2, 3, 0]);
}

@test:Config {
groups: ["fromXml"]
}
function testXmlToRecordWithArrayAsFieldType() returns error? {
xml xmlVal1 = xml `<Data><A>1</A><A>2</A><A>3</A></Data>`;
DataN3 rec1 = check parseAsType(xmlVal1);
test:assertEquals(rec1.A, [1, 2, 3, 0]);
}

type Student record {|
string name;
int age;
string code = "Admitted";
|};

@test:Config
function testXmlToRecordWithDefaultValuesForParseString1() returns error? {
string xmlStr = string `
<Student>
<name>Walter</name>
<age>27</age>
</Student>
`;

Student student = check parseString(xmlStr);
test:assertEquals(student.name, "Walter");
test:assertEquals(student.age, 27);
test:assertEquals(student.code, "Admitted");
}

@test:Config
function testXmlToRecordWithDefaultValuesForPasrseAsType1() returns error? {
xml xmlVal = xml `
<Student>
<name>Walter</name>
<age>27</age>
</Student>
`;

Student student = check parseAsType(xmlVal);
test:assertEquals(student.name, "Walter");
test:assertEquals(student.age, 27);
test:assertEquals(student.code, "Admitted");
}

type University record {|
Student[] student;
string name;
string category = "State";
|};

@test:Config
function testXmlToRecordWithDefaultValuesForParseString2() returns error? {
string xmlStr = string `
<University>
<student>
<name>Walter</name>
<age>27</age>
</student>
<student>
<name>Jessy</name>
<age>18</age>
</student>
<name>Standford</name>
</University>
`;

University university = check parseString(xmlStr);
test:assertEquals(university.student[0].name, "Walter");
test:assertEquals(university.student[0].age, 27);
test:assertEquals(university.student[0].code, "Admitted");

test:assertEquals(university.student[1].name, "Jessy");
test:assertEquals(university.student[1].age, 18);
test:assertEquals(university.student[1].code, "Admitted");

test:assertEquals(university.name, "Standford");
test:assertEquals(university.category, "State");
}

@test:Config
function testXmlToRecordWithDefaultValuesForParseAsType2() returns error? {
xml xmlVal = xml `
<University>
<student>
<name>Walter</name>
<age>27</age>
</student>
<student>
<name>Jessy</name>
<age>18</age>
</student>
<name>Standford</name>
</University>
`;

University university = check parseAsType(xmlVal);
test:assertEquals(university.student[0].name, "Walter");
test:assertEquals(university.student[0].age, 27);
test:assertEquals(university.student[0].code, "Admitted");

test:assertEquals(university.student[1].name, "Jessy");
test:assertEquals(university.student[1].age, 18);
test:assertEquals(university.student[1].code, "Admitted");

test:assertEquals(university.name, "Standford");
test:assertEquals(university.category, "State");
}

type RecType1 record {
string name;
@Name {
Expand All @@ -2838,15 +2914,15 @@ isolated function testElementAndAttributeInSameScopeHaveSameName() returns error
<Data name="Kevin">
<name>Kanth</name>
</Data>
`;
`;
RecType1 rec11 = check parseString(xmlStr);
test:assertEquals(rec11.name, "Kanth");
test:assertEquals(rec11.duplicateName, "Kevin");

RecType2 rec12 = check parseString(xmlStr);
test:assertEquals(rec12.name.\#content, "Kanth");
test:assertEquals(rec12.duplicateName, "Kevin");
test:assertEquals(rec12.duplicateName, "Kevin");

xml xmlVal = xml `
<Data name="Kevin">
<name>Kanth</name>
Expand All @@ -2858,7 +2934,7 @@ isolated function testElementAndAttributeInSameScopeHaveSameName() returns error

RecType2 rec22 = check parseAsType(xmlVal);
test:assertEquals(rec22.name.\#content, "Kanth");
test:assertEquals(rec22.duplicateName, "Kevin");
test:assertEquals(rec22.duplicateName, "Kevin");
}

type RecNs3 record {|
Expand Down Expand Up @@ -2888,7 +2964,7 @@ isolated function testElementWithDifferentNamespace() returns error? {
RecNs3 rec = check parseString(xmlStr);
test:assertEquals(rec.name, "Kevin");
test:assertEquals(rec.duplicateName, "Kanth");

xml xmlVal = xml `
<Data xmlns:ns1="example1.com" xmlns:ns2="example2.com">
<ns1:name>Kevin</ns1:name>
Expand Down Expand Up @@ -2959,28 +3035,6 @@ function testXmlToRecordNegative4() {
test:assertEquals((<error>rec1).message(), "expected 'int' value for the field 'A' found 'array' value");
}

type DataN3 record {|
int[4] A;
|};

@test:Config {
groups: ["fromXmlString"]
}
function testXmlStringToRecordNegative5() {
string xmlStr1 = "<Data><A>1</A><A>2</A><A>3</A></Data>";
DataN3|error rec1 = parseString(xmlStr1);
test:assertEquals((<error>rec1).message(), "array size is not compatible with the expected size");
}

@test:Config {
groups: ["fromXml"]
}
function testXmlToRecordNegative5() {
xml xmlVal1 = xml `<Data><A>1</A><A>2</A><A>3</A></Data>`;
DataN3|error rec1 = parseAsType(xmlVal1);
test:assertEquals((<error>rec1).message(), "array size is not compatible with the expected size");
}

type DataN4 record {|
string...;
|};
Expand Down Expand Up @@ -3329,6 +3383,88 @@ function testInvalidNamespaceInOpenRecordForParseAsType2() {
test:assertEquals((<error>err).message(), "undefined field 'name' in record 'data.xmldata:AuthorOpen'");
}

@test:Config
function testRegexAsFieldTypeWithParseStringNegative1() {
string xmlStr = string `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|}|Error err = parseString(xmlStr);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

@test:Config
function testRegexAsFieldTypeWithParseStringNegative2() {
string xmlStr = string `<Data>
<A>1</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp A;
string B;
record {|
string name;
|} C;
|}|Error err = parseString(xmlStr);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

@test:Config
function testRegexAsFieldTypeWithParseAsType() {
xml xmlVal = xml `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|}|error err = parseAsType(xmlVal);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

@test:Config
function testRegexAsFieldTypeWithParseAsType2() {
xml xmlVal = xml `<Data>
<A>1</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp A;
string B;
record {|
string name;
|} C;
|}|error err = parseAsType(xmlVal);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

type RecTypeDup1 record {
string name;
@Name {
Expand Down Expand Up @@ -3377,7 +3513,7 @@ isolated function testDuplicateField() {
RecTypeDup2|Error err3 = parseString(xmlStr2);
test:assertTrue(err3 is Error);
test:assertEquals((<Error> err3).message(), "duplicate field 'name'");

xml xmlVal2 = xml `
<Data name="Kevin">
<name>Kanth</name>
Expand Down
2 changes: 1 addition & 1 deletion ballerina/tests/from_xml_with_options.bal
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function testDisableProjectionInArrayForParseStringNegative() {
test:assertEquals((<error>rec).message(), "array size is not compatible with the expected size");
}

@test:Config
@test:Config
function testDisableProjectionInArrayForParseAsTypeNegative() {
xml xmlVal1 = xml `<DataProj>
<A><B>1</B></A>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ private Constants() {}
public static final BString ALLOW_DATA_PROJECTION = StringUtils.fromString("allowDataProjection");
public static final String RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX = "[^a-zA-Z\\d\s_]";
public static final String NS_ANNOT_NOT_DEFINED = "$$ns_annot_not_defined$$";
public static final String REGEXP_MODULE_NAME = "lang.regexp";
public static final String REGEXP_TYPE_NAME = "RegExp";
}
Loading
Loading