Skip to content

Commit

Permalink
Merge pull request #1031 from Bhashinee/master
Browse files Browse the repository at this point in the history
Fix the compiler plugin issue when using constants as cache congigs
  • Loading branch information
Bhashinee authored Jan 10, 2024
2 parents 05e45fd + 545b1a4 commit e334488
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 9 deletions.
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 = "cache"
version = "3.7.0"
version = "3.7.1"
authors = ["Ballerina"]
keywords = ["cache", "LRU"]
repository = "https://github.com/ballerina-platform/module-ballerina-cache"
Expand All @@ -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"
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 = "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"
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.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"},
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public void testConfigWithOutVariables() {
Assert.assertEquals(errorDiagnosticsList.size(), 0);
}

@Test
public void testConfigWithConstants() {
DiagnosticResult diagnosticResult = loadPackage("sample6").getCompilation().diagnosticResult();
List<Diagnostic> errorDiagnosticsList = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 0);
}

private void assertValues(List<Diagnostic> errorDiagnosticsList) {
long availableErrors = errorDiagnosticsList.size();
Assert.assertEquals(availableErrors, 5);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "cache_test"
name = "sample6"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -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
};
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void perform(SyntaxNodeAnalysisContext ctx) {
Optional<ExpressionNode> 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());
}
Expand All @@ -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() +
Expand Down

0 comments on commit e334488

Please sign in to comment.