From e60d6e9620683b6735939674eee501800fd343d0 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 29 May 2024 17:33:58 +0530 Subject: [PATCH 1/4] [Automated] Update the native jar versions --- ballerina/Ballerina.toml | 6 +++--- ballerina/CompilerPlugin.toml | 2 +- ballerina/Dependencies.toml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index e3597f6..1577886 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -1,7 +1,7 @@ [package] org = "ballerina" name = "data.xmldata" -version = "0.1.0" +version = "0.1.1" authors = ["Ballerina"] keywords = ["xml"] repository = "https://github.com/ballerina-platform/module-ballerina-data-xmldata" @@ -12,5 +12,5 @@ export = ["data.xmldata"] [[platform.java17.dependency]] groupId = "io.ballerina.lib" artifactId = "data-native" -version = "0.1.0" -path = "../native/build/libs/data.xmldata-native-0.1.0.jar" +version = "0.1.1" +path = "../native/build/libs/data.xmldata-native-0.1.1-SNAPSHOT.jar" diff --git a/ballerina/CompilerPlugin.toml b/ballerina/CompilerPlugin.toml index 7eef6a8..98592f5 100644 --- a/ballerina/CompilerPlugin.toml +++ b/ballerina/CompilerPlugin.toml @@ -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.0.jar" +path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.1-SNAPSHOT.jar" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 544d7c8..57bd1b1 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -10,7 +10,7 @@ distribution-version = "2201.9.0" [[package]] org = "ballerina" name = "data.xmldata" -version = "0.1.0" +version = "0.1.1" dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, From 97dc5ec2a72e8f5d5a7e05a000176816c98b87e7 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Wed, 29 May 2024 17:43:10 +0530 Subject: [PATCH 2/4] Fix error logging for user defined module prefix --- .../xmldata/compiler/CompilerPluginTest.java | 12 +++++++++ .../sample_package_11/Ballerina.toml | 4 +++ .../sample_package_11/main.bal | 8 ++++++ .../lib/data/xmldata/compiler/Constants.java | 2 ++ .../compiler/XmldataRecordFieldValidator.java | 26 ++++++++++++++++++- 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/Ballerina.toml create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/main.bal diff --git a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java index 956519d..cb83319 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java @@ -197,4 +197,16 @@ public void testCompilerPluginWithAProjectWithSubModule() { Assert.assertEquals(warningDiagnosticsList.get(0).diagnosticInfo().messageFormat(), "invalid annotation attachment: child record does not allow name annotation"); } + + @Test + public void testComplexUnionTypeCaseWhenUserDefinedModulePrefix() { + DiagnosticResult diagnosticResult = + CompilerPluginTestUtils.loadPackage("sample_package_11").getCompilation().diagnosticResult(); + List errorDiagnosticsList = diagnosticResult.diagnostics().stream() + .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) + .collect(Collectors.toList()); + Assert.assertEquals(errorDiagnosticsList.size(), 1); + Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + "invalid type: expected a record type"); + } } diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/Ballerina.toml b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/Ballerina.toml new file mode 100644 index 0000000..0d3fd35 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "xmldata_test" +name = "sample_11" +version = "0.1.0" diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/main.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/main.bal new file mode 100644 index 0000000..48ff508 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_11/main.bal @@ -0,0 +1,8 @@ +import ballerina/data.xmldata as xd; + +type UnionType record {|int a;|}|record {|string b;|}; + +public function main() returns error? { + string str = string `{"a": 1, "b": "str"}`; + UnionType _ = check xd:parseString(str); +} diff --git a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/Constants.java b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/Constants.java index 5f8c766..d630676 100644 --- a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/Constants.java +++ b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/Constants.java @@ -14,4 +14,6 @@ public class Constants { static final String NAME = "Name"; static final String NAMESPACE = "Namespace"; static final String XMLDATA = "xmldata"; + static final String BALLERINA = "ballerina"; + static final String DATA_XMLDATA = "data.xmldata"; } diff --git a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java index 00182e8..9ff8cf1 100644 --- a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java @@ -18,6 +18,7 @@ package io.ballerina.lib.data.xmldata.compiler; +import io.ballerina.compiler.api.ModuleID; import io.ballerina.compiler.api.SemanticModel; import io.ballerina.compiler.api.symbols.AnnotationAttachmentSymbol; import io.ballerina.compiler.api.symbols.AnnotationSymbol; @@ -38,6 +39,7 @@ import io.ballerina.compiler.syntax.tree.ExpressionNode; import io.ballerina.compiler.syntax.tree.FunctionCallExpressionNode; import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; +import io.ballerina.compiler.syntax.tree.ImportDeclarationNode; import io.ballerina.compiler.syntax.tree.ModuleMemberDeclarationNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; import io.ballerina.compiler.syntax.tree.ModuleVariableDeclarationNode; @@ -72,6 +74,7 @@ public class XmldataRecordFieldValidator implements AnalysisTask allDiagnosticInfo = new HashMap<>(); + private String modulePrefix = Constants.XMLDATA; @Override public void perform(SyntaxNodeAnalysisContext ctx) { @@ -84,6 +87,8 @@ public void perform(SyntaxNodeAnalysisContext ctx) { } ModulePartNode rootNode = (ModulePartNode) ctx.node(); + updateModulePrefix(rootNode); + for (ModuleMemberDeclarationNode member : rootNode.members()) { switch (member.kind()) { case FUNCTION_DEFINITION -> processFunctionDefinitionNode((FunctionDefinitionNode) member, ctx); @@ -94,6 +99,25 @@ public void perform(SyntaxNodeAnalysisContext ctx) { } } + private void updateModulePrefix(ModulePartNode rootNode) { + for (ImportDeclarationNode importDeclarationNode : rootNode.imports()) { + Optional symbol = semanticModel.symbol(importDeclarationNode); + if (symbol.isPresent() && symbol.get().kind() == SymbolKind.MODULE) { + ModuleSymbol moduleSymbol = (ModuleSymbol) symbol.get(); + if (isXmldataImport(moduleSymbol)) { + modulePrefix = moduleSymbol.id().modulePrefix(); + break; + } + } + } + } + + private boolean isXmldataImport(ModuleSymbol moduleSymbol) { + ModuleID moduleId = moduleSymbol.id(); + return Constants.BALLERINA.equals(moduleId.orgName()) + && Constants.DATA_XMLDATA.equals(moduleId.moduleName()); + } + private void processFunctionDefinitionNode(FunctionDefinitionNode functionDefinitionNode, SyntaxNodeAnalysisContext ctx) { ChildNodeList childNodeList = functionDefinitionNode.functionBody().children(); @@ -392,7 +416,7 @@ private boolean isParseFunctionFromXmldata(ExpressionNode expressionNode) { return false; } String prefix = ((QualifiedNameReferenceNode) nameReferenceNode).modulePrefix().text(); - if (!prefix.equals(Constants.XMLDATA)) { + if (!prefix.equals(modulePrefix)) { return false; } From 5430f78dd21dc5c95dd60817154572a47c081a34 Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Thu, 30 May 2024 11:27:22 +0530 Subject: [PATCH 3/4] Add more tests for user defined import prefix --- .../xmldata/compiler/CompilerPluginTest.java | 16 ++++++++++++++++ .../sample_package_12/Ballerina.toml | 4 ++++ .../sample_package_12/file_1.bal | 6 ++++++ .../sample_package_12/file_2.bal | 6 ++++++ .../sample_package_12/file_3.bal | 6 ++++++ .../sample_package_12/types.bal | 1 + 6 files changed, 39 insertions(+) create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/Ballerina.toml create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_1.bal create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_2.bal create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_3.bal create mode 100644 compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/types.bal diff --git a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java index cb83319..81c1040 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/lib/data/xmldata/compiler/CompilerPluginTest.java @@ -209,4 +209,20 @@ public void testComplexUnionTypeCaseWhenUserDefinedModulePrefix() { Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), "invalid type: expected a record type"); } + + @Test + public void testComplexUnionTypeCaseWhenUserDefinedModulePrefix2() { + DiagnosticResult diagnosticResult = + CompilerPluginTestUtils.loadPackage("sample_package_12").getCompilation().diagnosticResult(); + List errorDiagnosticsList = diagnosticResult.diagnostics().stream() + .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) + .collect(Collectors.toList()); + Assert.assertEquals(errorDiagnosticsList.size(), 3); + Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), + "invalid type: expected a record type"); + Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), + "invalid type: expected a record type"); + Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), + "invalid type: expected a record type"); + } } diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/Ballerina.toml b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/Ballerina.toml new file mode 100644 index 0000000..93df2bb --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "xmldata_test" +name = "sample_12" +version = "0.1.0" diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_1.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_1.bal new file mode 100644 index 0000000..4cced6f --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_1.bal @@ -0,0 +1,6 @@ +import ballerina/data.xmldata as xd; + +public function testFunc() returns error? { + string str = string `{"a": 1, "b": "str"}`; + UnionType _ = check xd:parseString(str); +} diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_2.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_2.bal new file mode 100644 index 0000000..710f15b --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_2.bal @@ -0,0 +1,6 @@ +import ballerina/data.xmldata as xd2; + +public function testFunc2() returns error? { + string str = string `{"a": 1, "b": "str"}`; + UnionType _ = check xd2:parseString(str); +} diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_3.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_3.bal new file mode 100644 index 0000000..ab8ee06 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/file_3.bal @@ -0,0 +1,6 @@ +import ballerina/data.xmldata; + +public function testFunc3() returns error? { + string str = string `{"a": 1, "b": "str"}`; + UnionType _ = check xmldata:parseString(str); +} diff --git a/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/types.bal b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/types.bal new file mode 100644 index 0000000..4383f4d --- /dev/null +++ b/compiler-plugin-test/src/test/resources/ballerina_sources/sample_package_12/types.bal @@ -0,0 +1 @@ +type UnionType record {|int a;|}|record {|string b;|}; From 17a279e4221c082d58f3a196995625bf708b6f8b Mon Sep 17 00:00:00 2001 From: prakanth <50439067+prakanth97@users.noreply.github.com> Date: Fri, 31 May 2024 16:11:36 +0530 Subject: [PATCH 4/4] Reset xmldata record type validator attributes --- .../xmldata/compiler/XmldataRecordFieldValidator.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java index 9ff8cf1..bea7ac4 100644 --- a/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/lib/data/xmldata/compiler/XmldataRecordFieldValidator.java @@ -83,6 +83,7 @@ public void perform(SyntaxNodeAnalysisContext ctx) { boolean erroneousCompilation = diagnostics.stream() .anyMatch(d -> d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)); if (erroneousCompilation) { + rest(); return; } @@ -97,6 +98,14 @@ public void perform(SyntaxNodeAnalysisContext ctx) { case TYPE_DEFINITION -> processTypeDefinitionNode((TypeDefinitionNode) member, ctx); } } + + rest(); + } + + private void rest() { + semanticModel = null; + allDiagnosticInfo.clear(); + modulePrefix = Constants.XMLDATA; } private void updateModulePrefix(ModulePartNode rootNode) {