Skip to content

Commit

Permalink
Refactor data utils to make function early returns
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Nov 22, 2024
1 parent 85891cf commit 0044bf0
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 172 deletions.
17 changes: 9 additions & 8 deletions ballerina/xml_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ public type ElementConfig record {|
|};

# Annotation to define schema rules for an XML element in Ballerina.
public const annotation ElementConfig Element on type, record field;
public const annotation ElementConfig Element on record field;

# Defines the configuration for an XML sequence in the XML schema (XSD).
public type SequenceConfig record {|
*ParticleOccurrence;
|};

# Annotation to define schema rules for an XML sequence in Ballerina.
public const annotation SequenceConfig Sequence on type, record field;
public const annotation SequenceConfig Sequence on record field;

# Defines the configuration for an XML choice in the XML schema (XSD).
public type ChoiceConfig record {|
*ParticleOccurrence;
|};

# Annotation to define schema rules for an XML choice in Ballerina.
public const annotation ChoiceConfig Choice on type, record field;
public const annotation ChoiceConfig Choice on record field;

# Defines the configuration for the sequence order in the XML schema (XSD).
public type SequenceOrderConfig record {|
Expand All @@ -61,7 +61,7 @@ public type SequenceOrderConfig record {|
|};

# Annotation to define schema rules for the sequence order in Ballerina.
public const annotation SequenceOrderConfig SequenceOrder on type, record field;
public const annotation SequenceOrderConfig SequenceOrder on record field;

# Defines the name of the XML element.
public type NameConfig record {|
Expand Down Expand Up @@ -470,14 +470,15 @@ isolated function addNamespaces(map<string> allNamespaces, map<string> namespace
}
}

# Validates an XML document against a provided XML schema.
# Validates an XML document against an XML schema.
#
# The schema can either be a file path to the `.xsd` file or a Ballerina record type that represents
# the XSD structure. The function checks if the `xmlValue` conforms to the provided schema.
# the schema defined by the XSD. The function checks if the XML value conforms to the specified schema.
#
# + schema - A `string` representing the file path to the `.xsd` file or a Ballerina record type representing the XSD.
# + schema - A string representing the file path to the `.xsd` file or a
# Ballerina record type representing the schema defined by the XSD.
# + xmlValue - The XML document that needs to be validated against the schema.
# + return - Returns `()` if the XML is valid according to the schema, otherwise returns `Error`.
# + return - Returns `()` if the XML value is valid according to the schema, otherwise returns `Error`.
public function validate(xml xmlValue, string|typedesc<record {}> schema)
returns Error? = @java:Method {'class: "io.ballerina.lib.data.xmldata.xml.Native"} external;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static io.ballerina.lib.data.xmldata.compiler.CompilerPluginTestUtils.getErrorMessage;

/**
* This class includes tests for Ballerina Xmldata compiler plugin.
*/
Expand All @@ -39,7 +41,7 @@ public void testDuplicateFieldNegative1() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 1);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid field: duplicate field found");
}

Expand All @@ -51,7 +53,7 @@ public void testDuplicateFieldNegative2() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 1);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid field: duplicate field found");
}

Expand All @@ -63,7 +65,7 @@ public void testChildRecordWithNameAnnotNegative() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.WARNING))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 1);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid annotation attachment: child record does not allow name annotation");
}

Expand All @@ -75,13 +77,13 @@ public void testDuplicateFieldInInlineRecordsNegative() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 4);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1),
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2),
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3),
"invalid field: duplicate field found");
}

Expand All @@ -93,17 +95,17 @@ public void testUnionTypeNegative() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 6);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid type: expected a record type");
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1),
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2),
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3),
"invalid type: expected a record type");
Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4),
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5),
"invalid field: duplicate field found");
}

Expand All @@ -115,7 +117,7 @@ public void testCompilerPluginWithAProjectWithSubModule() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 1);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid field: duplicate field found");

List<Diagnostic> warningDiagnosticsList = diagnosticResult.diagnostics().stream()
Expand All @@ -134,53 +136,53 @@ public void testCompilerPluginWithXsdAnnotation() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 24);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4),
"Invalid sequence member: Sequence members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5),
"Invalid sequence member: Sequence members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(6).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 6),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(7).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 7),
"Invalid sequence member: Sequence members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(8).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 8),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(9).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 9),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(10).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 10),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(11).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 11),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(12).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 12),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(13).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 13),
"Invalid choice member: Choice members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(14).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 14),
"Invalid choice member: Choice members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(15).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 15),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(16).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 16),
"Invalid choice member: Choice members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(17).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 17),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(18).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 18),
"A record field cannot contains sequence/choice/element/attribute annotations simultaneously");
Assert.assertEquals(errorDiagnosticsList.get(19).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 19),
"A record field cannot contains sequence/choice/element/attribute annotations simultaneously");
Assert.assertEquals(errorDiagnosticsList.get(20).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 20),
"A record field cannot contains sequence/choice/element/attribute annotations simultaneously");
Assert.assertEquals(errorDiagnosticsList.get(20).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 21),
"A record field cannot contains sequence/choice/element/attribute annotations simultaneously");
Assert.assertEquals(errorDiagnosticsList.get(13).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 22),
"Invalid choice member: Choice members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(20).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 23),
"A record field cannot contains sequence/choice/element/attribute annotations simultaneously");
}

Expand All @@ -192,17 +194,17 @@ public void testCompilerPluginWithXsdAnnotation2() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 6);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5),
"invalid xsd annotation: record type or record array type expected");
}

Expand All @@ -214,23 +216,23 @@ public void testCompilerPluginWithXsdAnnotation3() {
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 9);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 0),
"Invalid sequence member: Order should be defined in in all fields");
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 1),
"Invalid sequence member: Sequence members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 2),
"Invalid sequence member: Order should be defined in in all fields");
Assert.assertEquals(errorDiagnosticsList.get(3).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 3),
"Invalid sequence member: Sequence members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(4).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 4),
"Invalid sequence member: Order should be defined in in all fields");
Assert.assertEquals(errorDiagnosticsList.get(5).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 5),
"Invalid sequence member: Order should be defined in in all fields");
Assert.assertEquals(errorDiagnosticsList.get(6).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 6),
"Invalid choice member: Choice members should be defined in a closed record");
Assert.assertEquals(errorDiagnosticsList.get(7).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 7),
"invalid xsd annotation: record type or record array type expected");
Assert.assertEquals(errorDiagnosticsList.get(8).diagnosticInfo().messageFormat(),
Assert.assertEquals(getErrorMessage(errorDiagnosticsList, 8),
"Invalid choice member: Choice members should be defined in a closed record");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.environment.Environment;
import io.ballerina.projects.environment.EnvironmentBuilder;
import io.ballerina.tools.diagnostics.Diagnostic;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
* Utility functions related to compiler plugins tests.
Expand All @@ -43,4 +45,8 @@ static Package loadPackage(String path) {
BuildProject project = BuildProject.load(projectEnvironmentBuilder, projectDirPath);
return project.currentPackage();
}

static String getErrorMessage(List<Diagnostic> errorDiagnosticsList, int index) {
return errorDiagnosticsList.get(index).diagnosticInfo().messageFormat();
}
}
Loading

0 comments on commit 0044bf0

Please sign in to comment.