From d5247ea8722ca1c23cdd04dcda55024171222fad Mon Sep 17 00:00:00 2001 From: Haritha Hasathcharu Date: Tue, 12 Mar 2024 16:55:52 +0530 Subject: [PATCH] Add test case for compilation error --- .../sql/compiler/CompilerPluginTest.java | 6 +++++ .../persist/compilationerror.bal | 25 +++++++++++++++++++ .../PersistSqlModelDefinitionValidator.java | 5 ++-- .../persist/sql/compiler/utils/Utils.java | 9 ------- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 compiler-plugin-test/src/test/resources/modelvalidator/persist/compilationerror.bal diff --git a/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java index 54b46bc..591c12c 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java @@ -75,6 +75,12 @@ private Package loadPersistModelFile(String directory, String name) { return project.currentPackage(); } + @Test(enabled = true) + public void testHasCompilationErrors() { + List diagnostics = getErrorDiagnostics("modelvalidator", "compilationerror.bal", 1); + Assert.assertTrue(diagnostics.size() == 1); + } + @Test(enabled = true) public void validateCharAnnotations() { List diagnostics = getErrorDiagnostics("modelvalidator", "char.bal", 3); diff --git a/compiler-plugin-test/src/test/resources/modelvalidator/persist/compilationerror.bal b/compiler-plugin-test/src/test/resources/modelvalidator/persist/compilationerror.bal new file mode 100644 index 0000000..2464dce --- /dev/null +++ b/compiler-plugin-test/src/test/resources/modelvalidator/persist/compilationerror.bal @@ -0,0 +1,25 @@ +// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com) All Rights Reserved. +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/persist as _; + +public type Person record {| + readonly int nic; + string name; + int age; + string city; + any 'type; +|}; \ No newline at end of file diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/PersistSqlModelDefinitionValidator.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/PersistSqlModelDefinitionValidator.java index 233c945..6668960 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/PersistSqlModelDefinitionValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/PersistSqlModelDefinitionValidator.java @@ -98,7 +98,6 @@ import static io.ballerina.stdlib.persist.sql.compiler.DiagnosticsCodes.PERSIST_SQL_619; import static io.ballerina.stdlib.persist.sql.compiler.DiagnosticsCodes.PERSIST_SQL_620; import static io.ballerina.stdlib.persist.sql.compiler.DiagnosticsCodes.PERSIST_SQL_621; -import static io.ballerina.stdlib.persist.sql.compiler.utils.Utils.getTypeName; import static io.ballerina.stdlib.persist.sql.compiler.utils.Utils.hasCompilationErrors; import static io.ballerina.stdlib.persist.sql.compiler.utils.Utils.isAnnotationPresent; import static io.ballerina.stdlib.persist.sql.compiler.utils.Utils.readStringArrayValueFromAnnotation; @@ -405,7 +404,9 @@ private void processEntityFields(Entity entity) { isSimpleType = true; } } else { - fieldType = getTypeName(processedTypeNode); + // types like rest, anydata, any etc are not supported fields. hence ignored as they will be picked up + // by persist compiler + continue; } if (isIdentityField) { diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/utils/Utils.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/utils/Utils.java index ccf4508..6222bb5 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/utils/Utils.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/persist/sql/compiler/utils/Utils.java @@ -22,7 +22,6 @@ import io.ballerina.compiler.syntax.tree.ExpressionNode; import io.ballerina.compiler.syntax.tree.MappingConstructorExpressionNode; import io.ballerina.compiler.syntax.tree.MappingFieldNode; -import io.ballerina.compiler.syntax.tree.Node; import io.ballerina.compiler.syntax.tree.SpecificFieldNode; import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; import io.ballerina.tools.diagnostics.Diagnostic; @@ -54,14 +53,6 @@ public static String stripEscapeCharacter(String name) { return name.startsWith("'") ? name.substring(1) : name; } - public static String getTypeName(Node processedTypeNode) { - String typeName = processedTypeNode.kind().stringValue(); - if (typeName.isBlank()) { - typeName = processedTypeNode.kind().name(); - } - return typeName; - } - public static String readStringValueFromAnnotation (List annotationNodes, String annotation, String field) {