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

Fix compile-time error logging for user defined module prefix for jsondata #22

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.jsondata"
version = "0.1.0"
version = "0.1.1"
authors = ["Ballerina"]
keywords = ["json", "json path", "json-transform", "json transform", "json to json", "json-convert", "json convert"]
repository = "https://github.com/ballerina-platform/module-ballerina-data.jsondata"
Expand All @@ -15,8 +15,8 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "jsondata-native"
version = "0.1.0"
path = "../native/build/libs/data.jsondata-native-0.1.0.jar"
version = "0.1.1"
path = "../native/build/libs/data.jsondata-native-0.1.1-SNAPSHOT.jar"

[[platform.java17.dependency]]
path = "./lib/json-path-2.9.0.jar"
Expand Down
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.jsondata.compiler.JsondataCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/data.jsondata-compiler-plugin-0.1.0.jar"
path = "../compiler-plugin/build/libs/data.jsondata-compiler-plugin-0.1.1-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.jsondata"
version = "0.1.0"
version = "0.1.1"
dependencies = [
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,32 @@ public void testComplexUnionTypeAsMemberOfIntersection() {
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
"unsupported union type: union type does not support multiple complex types");
}

@Test
public void testComplexUnionTypeCaseWhenUserDefinedModulePrefix1() {
DiagnosticResult diagnosticResult =
CompilerPluginTestUtils.loadPackage("sample_package_9").getCompilation().diagnosticResult();
List<Diagnostic> 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(),
"unsupported union type: union type does not support multiple complex types");
}

@Test
public void testComplexUnionTypeCaseWhenUserDefinedModulePrefix2() {
DiagnosticResult diagnosticResult =
CompilerPluginTestUtils.loadPackage("sample_package_10").getCompilation().diagnosticResult();
List<Diagnostic> 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(),
"unsupported union type: union type does not support multiple complex types");
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(),
"unsupported union type: union type does not support multiple complex types");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(),
"unsupported union type: union type does not support multiple complex types");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "jsondata_test"
name = "sample_10"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ballerina/data.jsondata as jd;

public function testFunc1() returns error? {
string str = string `{"a": 1, "b": "str"}`;
UnionType _ = check jd:parseString(str);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ballerina/data.jsondata as jd2;

public function testFunc2() returns error? {
string str = string `{"a": 1, "b": "str"}`;
UnionType _ = check jd2:parseString(str);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ballerina/data.jsondata;

public function testFunc3() returns error? {
string str = string `{"a": 1, "b": "str"}`;
UnionType _ = check jsondata:parseString(str);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type UnionType record {|int a;|}|record {|string b;|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "jsondata_test"
name = "sample_9"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ballerina/data.jsondata as jd;

type UnionType record {|int a;|}|record {|string b;|};

public function main() returns error? {
string str = string `{"a": 1, "b": "str"}`;
UnionType _ = check jd:parseString(str);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class Constants {
static final String PARSE_STREAM = "parseStream";
static final String NAME = "Name";
static final String JSONDATA = "jsondata";
static final String BALLERINA = "ballerina";
static final String DATA_JSONDATA = "data.jsondata";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.lib.data.jsondata.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;
Expand All @@ -27,6 +28,7 @@
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TupleTypeSymbol;
import io.ballerina.compiler.api.symbols.TypeDefinitionSymbol;
import io.ballerina.compiler.api.symbols.TypeDescKind;
Expand All @@ -39,6 +41,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;
Expand Down Expand Up @@ -73,6 +76,7 @@ public class JsondataTypeValidator implements AnalysisTask<SyntaxNodeAnalysisCon
private SemanticModel semanticModel;
private final HashMap<Location, DiagnosticInfo> allDiagnosticInfo = new HashMap<>();
Location currentLocation;
private String modulePrefix = Constants.JSONDATA;

@Override
public void perform(SyntaxNodeAnalysisContext ctx) {
Expand All @@ -81,10 +85,13 @@ public void perform(SyntaxNodeAnalysisContext ctx) {
boolean erroneousCompilation = diagnostics.stream()
.anyMatch(d -> d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR));
if (erroneousCompilation) {
reset();
return;
}

ModulePartNode rootNode = (ModulePartNode) ctx.node();
updateModulePrefix(rootNode);

for (ModuleMemberDeclarationNode member : rootNode.members()) {
switch (member.kind()) {
case FUNCTION_DEFINITION -> processFunctionDefinitionNode((FunctionDefinitionNode) member, ctx);
Expand All @@ -94,6 +101,28 @@ public void perform(SyntaxNodeAnalysisContext ctx) {
processTypeDefinitionNode((TypeDefinitionNode) member, ctx);
}
}

reset();
}

private void reset() {
semanticModel = null;
allDiagnosticInfo.clear();
currentLocation = null;
modulePrefix = Constants.JSONDATA;
}

private void updateModulePrefix(ModulePartNode rootNode) {
for (ImportDeclarationNode importDeclarationNode : rootNode.imports()) {
Optional<Symbol> symbol = semanticModel.symbol(importDeclarationNode);
if (symbol.isPresent() && symbol.get().kind() == SymbolKind.MODULE) {
ModuleSymbol moduleSymbol = (ModuleSymbol) symbol.get();
if (isJsondataImport(moduleSymbol)) {
modulePrefix = moduleSymbol.id().modulePrefix();
break;
}
}
}
}

private void processFunctionDefinitionNode(FunctionDefinitionNode functionDefinitionNode,
Expand Down Expand Up @@ -140,7 +169,7 @@ private boolean isParseFunctionOfStringSource(ExpressionNode expressionNode) {
return false;
}
String prefix = ((QualifiedNameReferenceNode) nameReferenceNode).modulePrefix().text();
if (!prefix.equals(Constants.JSONDATA)) {
if (!prefix.equals(modulePrefix)) {
return false;
}
String functionName = ((FunctionCallExpressionNode) expressionNode).functionName().toString().trim();
Expand Down Expand Up @@ -329,4 +358,10 @@ private String getAnnotModuleName(AnnotationSymbol annotation) {
Optional<String> moduleName = moduleSymbol.get().getName();
return moduleName.orElse("");
}

private boolean isJsondataImport(ModuleSymbol moduleSymbol) {
ModuleID moduleId = moduleSymbol.id();
return Constants.BALLERINA.equals(moduleId.orgName())
&& Constants.DATA_JSONDATA.equals(moduleId.moduleName());
}
}
Loading