diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample1/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample1/Ballerina.toml deleted file mode 100644 index 112e60f1..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample1/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample1" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample1/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample1/main.bal deleted file mode 100644 index 6967c59f..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample1/main.bal +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Foo record { - Bar? foo; -}; - -type Bar record { - int? bar = 2; - string car; -}; - -public function main() returns error? { - xml x = xml `2`; - Foo actual = check xmldata:toRecord(x); -} diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample10/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample10/Ballerina.toml deleted file mode 100644 index d945d518..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample10/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample10" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample10/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample10/main.bal deleted file mode 100644 index e8edb750..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample10/main.bal +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Test record { - Bar|Bar1 foo; -}; - -type Foo record { - Bar|Bar1[]|string|int foo; -}; - -type Bar record { - int? bar; - string|Bar|map car; -}; - -type Bar1 record { - int bar; - string|int|float car; -}; - -xml x1 = xml `2`; -Foo actual = check xmldata:fromXml(x1); diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample11/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample11/Ballerina.toml deleted file mode 100644 index bec3efd9..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample11/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample11" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample11/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample11/main.bal deleted file mode 100644 index 5c2ad8ed..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample11/main.bal +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2023, WSO2 LLC. (https://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/xmldata; - -@xmldata:Name{ - value: "Foo1" -} -type Foo record { - Bar foo; -}; - -@xmldata:Name{ - value: "Bar1" -} -type Bar record { - int bar; - Bar2 bar2; -}; - -@xmldata:Name{ - value: "Bar4" -} -@xmldata:Namespace { - prefix: "ns", - uri: "http://sdf.com" -} -type Bar2 record { - int bar; - string car; -}; - -public function main() returns error? { - xml x1 = xml `2`; - Foo actual = check xmldata:fromXml(x1); - Bar result = check xmldata:fromXml(x1); -} diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample2/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample2/Ballerina.toml deleted file mode 100644 index a422cdca..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample2/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample2" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample2/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample2/main.bal deleted file mode 100644 index 417346fd..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample2/main.bal +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Foo record { - Bar? foo; -}; - -type Bar record { - int? bar; - string car; -}; - -type Foo1 record { - Bar1? foo; -}; - -type Bar1 record { - int? bar; - string car; -}; - -public function main() returns error? { - xml x = xml `2`; - _ = check getValue(x); -} - -function getValue(xml x) returns Foo|error { - Foo actual = check xmldata:toRecord(x); - Foo1 actual1 = check xmldata:toRecord(x); - return actual; -} diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample3/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample3/Ballerina.toml deleted file mode 100644 index 8bcceb0a..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample3/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample3" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample3/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample3/main.bal deleted file mode 100644 index c1044749..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample3/main.bal +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Foo record { - Bar? foo; -}; - -type Bar record { - int? bar; - string car; -}; - -public function main() returns error? { - xml x = xml `2`; - _ = check getValue(x); -} - -type Foo1 record { - string|Bar1? foo; -}; - -type Bar1 record { - int? bar; - Foo1|string car; -}; - -function getValue(xml x) returns Foo|error { - Foo actual = check xmldata:toRecord(x); - Foo1 actual1 = check xmldata:toRecord(x); - return actual; -} diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample4/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample4/Ballerina.toml deleted file mode 100644 index 739ff020..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample4/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample4" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample4/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample4/main.bal deleted file mode 100644 index 36e907bc..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample4/main.bal +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Foo2 record { - Bar? foo; -}; - -type Foo record { - Bar? foo; -}; - -type Bar record { - int? bar; - string car; -}; - -public function main() returns error? { - xml x = xml `2`; - _ = check getValue(x); -} - -type Foo1 record { - string|Bar1? foo; -}; - -type Bar1 record { - int? bar; - Foo1|string car; -}; - -function getValue(xml x) returns Foo|error { - Foo actual = check xmldata:toRecord(x); - Foo1 actual1 = check xmldata:toRecord(x); - return actual; -} diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample5/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample5/Ballerina.toml deleted file mode 100644 index fc0c6c52..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample5/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample5" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample5/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample5/main.bal deleted file mode 100644 index d3bd8181..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample5/main.bal +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Test record { - Bar|Bar1? foo; -}; - -type Foo record { - Bar|Bar1? foo; -}; - -type Bar record { - int? bar; - Bar2 bar2; -}; - -type Bar2 record { - int? bar; - Bar3 car; -}; - -type Bar1 record { - int? bar; - string car; -}; - -type Bar3 record { - int? bar; - string car; -}; - -type Bar4 record { - int? bar; - string car; -}; - -type Test5 record { - int? bar; - string car; -}; - -public function main() returns error? { - xml x1 = xml `2`; - Foo actual = check xmldata:toRecord(x1); -} diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample6/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample6/Ballerina.toml deleted file mode 100644 index e2a9add4..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample6/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample6" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample6/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample6/main.bal deleted file mode 100644 index 75ff4632..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample6/main.bal +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Test record { - Bar|Bar1? foo; -}; - -type Foo record { - Bar|Bar1? foo; -}; - -type Bar record { - int? bar; - string car; -}; - -type Bar1 record { - int? bar; - string car; -}; - -xml x1 = xml `2`; -Foo actual = check xmldata:toRecord(x1); diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample7/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample7/Ballerina.toml deleted file mode 100644 index 326d7220..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample7/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample7" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample7/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample7/main.bal deleted file mode 100644 index 5974392f..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample7/main.bal +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Test record { - Bar|Bar1? foo; -}; - -type Foo record { - Bar|Bar1[]|string foo; -}; - -type Bar record { - int? bar; - Bar1[] car; -}; - -type Bar1 record { - int? bar; - string car; -}; - -xml x1 = xml `2`; -Foo actual = check xmldata:toRecord(x1); diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample8/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample8/Ballerina.toml deleted file mode 100644 index 040d1eec..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample8/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample8" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample8/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample8/main.bal deleted file mode 100644 index 956c3c7f..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample8/main.bal +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Test record { - Bar|Bar1 foo; -}; - -type Foo record { - Bar|Bar1[]|string|() foo; -}; - -type Bar record { - int? bar; - () car; -}; - -type Bar1 record { - int bar; - string|() car; -}; - -xml x1 = xml `2`; -Foo actual = check xmldata:toRecord(x1); diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample9/Ballerina.toml b/compiler-plugin-tests/bin/test/diagnostics/sample9/Ballerina.toml deleted file mode 100644 index 1d3e071a..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample9/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "xmldata_test" -name = "sample9" -version = "0.1.0" diff --git a/compiler-plugin-tests/bin/test/diagnostics/sample9/main.bal b/compiler-plugin-tests/bin/test/diagnostics/sample9/main.bal deleted file mode 100644 index 7080a426..00000000 --- a/compiler-plugin-tests/bin/test/diagnostics/sample9/main.bal +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. 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/xmldata; - -type Test record { - Bar|Bar1 foo; -}; - -type Foo record { - Bar|Bar1[]|string|() foo; -}; - -type Bar record { - int? bar; - () car; -}; - -type Bar1 record { - int bar; - string|() car; -}; - -xml x1 = xml `2`; -Foo actual = check xmldata:fromXml(x1); diff --git a/compiler-plugin-tests/bin/test/testng.xml b/compiler-plugin-tests/bin/test/testng.xml deleted file mode 100644 index 92c532a2..00000000 --- a/compiler-plugin-tests/bin/test/testng.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - diff --git a/compiler-plugin-tests/src/test/resources/diagnostics/sample11/main.bal b/compiler-plugin-tests/src/test/resources/diagnostics/sample11/main.bal index 5c2ad8ed..7232ae1d 100644 --- a/compiler-plugin-tests/src/test/resources/diagnostics/sample11/main.bal +++ b/compiler-plugin-tests/src/test/resources/diagnostics/sample11/main.bal @@ -16,14 +16,14 @@ import ballerina/xmldata; -@xmldata:Name{ +@xmldata:Name { value: "Foo1" } type Foo record { Bar foo; }; -@xmldata:Name{ +@xmldata:Name { value: "Bar1" } type Bar record { @@ -31,7 +31,7 @@ type Bar record { Bar2 bar2; }; -@xmldata:Name{ +@xmldata:Name { value: "Bar4" } @xmldata:Namespace { diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/XmldataRecordFieldValidator.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/XmldataRecordFieldValidator.java index 00bf8054..8e761d2b 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/XmldataRecordFieldValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/XmldataRecordFieldValidator.java @@ -19,8 +19,10 @@ import io.ballerina.compiler.syntax.tree.AnnotationNode; import io.ballerina.compiler.syntax.tree.ArrayTypeDescriptorNode; +import io.ballerina.compiler.syntax.tree.CheckExpressionNode; import io.ballerina.compiler.syntax.tree.ChildNodeList; 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.ModuleMemberDeclarationNode; import io.ballerina.compiler.syntax.tree.ModulePartNode; @@ -95,17 +97,8 @@ private void processFunctionDefinitionNode(FunctionDefinitionNode functionDefini VariableDeclarationNode variableDeclarationNode = (VariableDeclarationNode) node; Optional initializer = variableDeclarationNode.initializer(); if (initializer.isPresent()) { - ExpressionNode expressionNode = initializer.get(); - String functionName = expressionNode.toString(); - if (functionName.contains(TO_RECORD) || functionName.contains(FROM_XML)) { - TypeDescriptorNode typeDescriptor = variableDeclarationNode.typedBindingPattern(). - typeDescriptor(); - if (typeDescriptor.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) { - String returnTypeName = typeDescriptor.toString().trim(); - if (!this.recordNamesUsedInFunction.contains(returnTypeName)) { - this.recordNamesUsedInFunction.add(returnTypeName); - } - } + if (isValidFunctionName(initializer.get())) { + addRecordName(variableDeclarationNode.typedBindingPattern().typeDescriptor()); } } } @@ -115,17 +108,30 @@ private void processFunctionDefinitionNode(FunctionDefinitionNode functionDefini private void processModuleVariableDeclarationNode(ModuleVariableDeclarationNode moduleVariableDeclarationNode) { Optional initializer = moduleVariableDeclarationNode.initializer(); if (initializer.isPresent()) { - ExpressionNode expressionNode = initializer.get(); - String functionName = expressionNode.toString(); - if (functionName.contains(TO_RECORD) || functionName.contains(FROM_XML)) { - TypeDescriptorNode typeDescriptor = moduleVariableDeclarationNode.typedBindingPattern(). - typeDescriptor(); - if (typeDescriptor.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) { - String returnTypeName = typeDescriptor.toString().trim(); - if (!this.recordNamesUsedInFunction.contains(returnTypeName)) { - this.recordNamesUsedInFunction.add(returnTypeName); - } - } + if (isValidFunctionName(initializer.get())) { + addRecordName(moduleVariableDeclarationNode.typedBindingPattern().typeDescriptor()); + } + } + } + + private boolean isValidFunctionName(ExpressionNode expressionNode) { + if (expressionNode instanceof CheckExpressionNode) { + expressionNode = ((CheckExpressionNode) expressionNode).expression(); + } + if (expressionNode instanceof FunctionCallExpressionNode) { + FunctionCallExpressionNode functionCallExpressionNode = + (FunctionCallExpressionNode) expressionNode; + String functionName = functionCallExpressionNode.functionName().toSourceCode().trim(); + return functionName.equals(TO_RECORD) || functionName.equals(FROM_XML); + } + return false; + } + + private void addRecordName(TypeDescriptorNode typeDescriptor) { + if (typeDescriptor.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) { + String returnTypeName = typeDescriptor.toString().trim(); + if (!this.recordNamesUsedInFunction.contains(returnTypeName)) { + this.recordNamesUsedInFunction.add(returnTypeName); } } } @@ -138,7 +144,7 @@ private void processTypeDefinitionNode(TypeDefinitionNode typeDefinitionNode) { typeDefinitionNode.metadata().ifPresent(metadataNode -> { NodeList annotations = metadataNode.annotations(); for (AnnotationNode annotationNode : annotations) { - if (annotationNode.annotReference().toString().equals(NAME_ANNOTATION)) { + if (annotationNode.annotReference().toSourceCode().trim().equals(NAME_ANNOTATION)) { record.setNameAnnotation(); } } @@ -160,11 +166,11 @@ private void processTypeDefinitionNode(TypeDefinitionNode typeDefinitionNode) { private void processFieldType(Node type, Record record) { if (type instanceof OptionalTypeDescriptorNode) { - record.addLocationOfOptionalsFields(type.location()); + record.addOptionalFieldLocations(type.location()); type = ((OptionalTypeDescriptorNode) type).typeDescriptor(); } if (type instanceof NilTypeDescriptorNode) { - record.addLocationOfOptionalsFields(type.location()); + record.addOptionalFieldLocations(type.location()); } if (type instanceof UnionTypeDescriptorNode) { processUnionType((UnionTypeDescriptorNode) type, 0, record, type); @@ -185,11 +191,11 @@ private void processUnionType(UnionTypeDescriptorNode unionTypeDescriptorNode, i processUnionType((UnionTypeDescriptorNode) unionType, noOfSimpleNamesType, record, type); } if (unionType instanceof OptionalTypeDescriptorNode) { - record.addLocationOfOptionalsFields(unionType.location()); + record.addOptionalFieldLocations(unionType.location()); unionType = ((OptionalTypeDescriptorNode) unionType).typeDescriptor(); } if (unionType instanceof NilTypeDescriptorNode) { - record.addLocationOfOptionalsFields(unionType.location()); + record.addOptionalFieldLocations(unionType.location()); } if (unionType instanceof ArrayTypeDescriptorNode) { unionType = ((ArrayTypeDescriptorNode) unionType).memberTypeDesc(); @@ -201,16 +207,16 @@ private void processUnionType(UnionTypeDescriptorNode unionTypeDescriptorNode, i } } if (noOfSimpleNamesType > 1) { - record.addLocationOfMultipleNonPrimitiveTypes(type.location()); + record.addMultipleNonPrimitiveTypeLocations(type.location()); } } private void validateRecord(SyntaxNodeAnalysisContext ctx, Record record) { this.validatedRecords.add(record.getName()); - for (Location location : record.getLocationOfMultipleNonPrimitiveTypes()) { + for (Location location : record.getMultipleNonPrimitiveTypeLocations()) { reportDiagnosticInfo(ctx, location, DiagnosticsCodes.XMLDATA_102); } - for (Location location : record.getLocationOfOptionalsFields()) { + for (Location location : record.getOptionalFieldLocations()) { reportDiagnosticInfo(ctx, location, DiagnosticsCodes.XMLDATA_101); } for (String childRecordName : record.getChildRecordNames()) { diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/object/Record.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/object/Record.java index 0c8430c5..ef6484d9 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/object/Record.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/xmldata/compiler/object/Record.java @@ -33,9 +33,9 @@ public class Record { private final String name; private final Location location; private Boolean nameAnnotation = false; - private final List locationOfOptionalsFields = new ArrayList<>(); + private final List optionalFieldLocations = new ArrayList<>(); private final List childRecordNames = new ArrayList<>(); - private final List locationOfMultipleNonPrimitiveTypes = new ArrayList<>(); + private final List multipleNonPrimitiveTypeLocations = new ArrayList<>(); public Record(String name, Location location) { this.name = name; @@ -46,20 +46,20 @@ public String getName() { return name; } - public void addLocationOfOptionalsFields(NodeLocation optionalsField) { - locationOfOptionalsFields.add(optionalsField); + public void addOptionalFieldLocations(NodeLocation optionalsField) { + optionalFieldLocations.add(optionalsField); } - public List getLocationOfOptionalsFields() { - return locationOfOptionalsFields; + public List getOptionalFieldLocations() { + return optionalFieldLocations; } - public void addLocationOfMultipleNonPrimitiveTypes(NodeLocation optionalsField) { - locationOfMultipleNonPrimitiveTypes.add(optionalsField); + public void addMultipleNonPrimitiveTypeLocations(NodeLocation optionalsField) { + multipleNonPrimitiveTypeLocations.add(optionalsField); } - public List getLocationOfMultipleNonPrimitiveTypes() { - return locationOfMultipleNonPrimitiveTypes; + public List getMultipleNonPrimitiveTypeLocations() { + return multipleNonPrimitiveTypeLocations; } public void addChildRecordNames(String name) {