diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index e33b24f6..fa815ebf 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -1,7 +1,7 @@ [package] org = "ballerina" name = "cache" -version = "3.7.0" +version = "3.7.1" authors = ["Ballerina"] keywords = ["cache", "LRU"] repository = "https://github.com/ballerina-platform/module-ballerina-cache" @@ -15,5 +15,5 @@ graalvmCompatible = true [[platform.java17.dependency]] groupId = "io.ballerina.stdlib" artifactId = "cache-native" -version = "3.7.0" -path = "../native/build/libs/cache-native-3.7.0.jar" +version = "3.7.1" +path = "../native/build/libs/cache-native-3.7.1-SNAPSHOT.jar" diff --git a/ballerina/CompilerPlugin.toml b/ballerina/CompilerPlugin.toml index 74313999..25808855 100644 --- a/ballerina/CompilerPlugin.toml +++ b/ballerina/CompilerPlugin.toml @@ -3,4 +3,4 @@ id = "cache-compiler-plugin" class = "io.ballerina.stdlib.cache.compiler.CacheCompilerPlugin" [[dependency]] -path = "../compiler-plugin/build/libs/cache-compiler-plugin-3.7.0.jar" +path = "../compiler-plugin/build/libs/cache-compiler-plugin-3.7.1-SNAPSHOT.jar" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index f5fa64d4..0f5f50ab 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -10,7 +10,7 @@ distribution-version = "2201.8.0" [[package]] org = "ballerina" name = "cache" -version = "3.7.0" +version = "3.7.1" dependencies = [ {org = "ballerina", name = "constraint"}, {org = "ballerina", name = "jballerina.java"}, diff --git a/changelog.md b/changelog.md index dd754d4c..24ecca2f 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- [Fix the compilation failure when constants are used in cache config](https://github.com/ballerina-platform/ballerina-library/issues/5915) + +## [2.1.0-beta.1] - 2021-06-02 + ### Added - [Introduced the compiler plugin to validate cache configurations](https://github.com/ballerina-platform/ballerina-standard-library/issues/1435) diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/cache/compiler/CompilerPluginTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/cache/compiler/CompilerPluginTest.java index 81e24928..fd22083b 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/cache/compiler/CompilerPluginTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/cache/compiler/CompilerPluginTest.java @@ -108,6 +108,15 @@ public void testConfigWithOutVariables() { Assert.assertEquals(errorDiagnosticsList.size(), 0); } + @Test + public void testConfigWithConstants() { + DiagnosticResult diagnosticResult = loadPackage("sample6").getCompilation().diagnosticResult(); + List errorDiagnosticsList = diagnosticResult.diagnostics().stream() + .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) + .collect(Collectors.toList()); + Assert.assertEquals(errorDiagnosticsList.size(), 0); + } + private void assertValues(List errorDiagnosticsList) { long availableErrors = errorDiagnosticsList.size(); Assert.assertEquals(availableErrors, 5); diff --git a/compiler-plugin-tests/src/test/resources/diagnostics/sample6/Ballerina.toml b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/Ballerina.toml new file mode 100644 index 00000000..c794d6be --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "cache_test" +name = "sample6" +version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/diagnostics/sample6/modules/code/code.bal b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/modules/code/code.bal new file mode 100644 index 00000000..aef581d2 --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/modules/code/code.bal @@ -0,0 +1,25 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) 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/cache; +import sample6.config; + +final cache:CacheConfig cacheConfig = { + capacity: 10, + evictionFactor: config:EVICTION_FACTOR, + defaultMaxAge: config:DEFAULT_MAX_AGE, + cleanupInterval: config:CLEANUP_INTERVAL +}; diff --git a/compiler-plugin-tests/src/test/resources/diagnostics/sample6/modules/config/config.bal b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/modules/config/config.bal new file mode 100644 index 00000000..94859bc7 --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/modules/config/config.bal @@ -0,0 +1,20 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) 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. + +public const int CAPACITY = 10; +public const float EVICTION_FACTOR = 0.2; +public const decimal DEFAULT_MAX_AGE = 30; +public const decimal CLEANUP_INTERVAL = 60; diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/cache/compiler/CacheConfigValidator.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/cache/compiler/CacheConfigValidator.java index 669d9309..8f1a7d6a 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/cache/compiler/CacheConfigValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/cache/compiler/CacheConfigValidator.java @@ -17,6 +17,7 @@ */ package io.ballerina.stdlib.cache.compiler; +import io.ballerina.compiler.api.symbols.ConstantSymbol; import io.ballerina.compiler.api.symbols.ModuleSymbol; import io.ballerina.compiler.api.symbols.Symbol; import io.ballerina.compiler.api.symbols.TypeDescKind; @@ -107,7 +108,7 @@ public void perform(SyntaxNodeAnalysisContext ctx) { Optional expressionNode = fieldNode.valueExpr(); if (expressionNode.isPresent()) { ExpressionNode valueNode = expressionNode.get(); - String value = getTerminalNodeValue(valueNode); + String value = getTerminalNodeValue(valueNode, ctx); if (value != null) { validateConfig(name, value, ctx, valueNode.location()); } @@ -117,13 +118,17 @@ public void perform(SyntaxNodeAnalysisContext ctx) { } } - private String getTerminalNodeValue(Node valueNode) { + private String getTerminalNodeValue(Node valueNode, SyntaxNodeAnalysisContext ctx) { String value = null; if (valueNode instanceof BasicLiteralNode) { value = ((BasicLiteralNode) valueNode).literalToken().text(); } else if (valueNode instanceof QualifiedNameReferenceNode) { - QualifiedNameReferenceNode qualifiedNameReferenceNode = (QualifiedNameReferenceNode) valueNode; - value = qualifiedNameReferenceNode.toString(); + if (ctx.semanticModel().symbol(valueNode).get() instanceof ConstantSymbol constantSymbol) { + value = constantSymbol.constValue().toString(); + } else { + QualifiedNameReferenceNode qualifiedNameReferenceNode = (QualifiedNameReferenceNode) valueNode; + value = qualifiedNameReferenceNode.toString(); + } } else if (valueNode instanceof UnaryExpressionNode) { UnaryExpressionNode unaryExpressionNode = (UnaryExpressionNode) valueNode; value = unaryExpressionNode.unaryOperator() +