Skip to content

Commit

Permalink
Fix plugin crash when variable is passed for Options
Browse files Browse the repository at this point in the history
  • Loading branch information
niveathika committed Dec 22, 2021
1 parent db51f63 commit dfb38a8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- Release OracleDB module on Ballerina 2201.0.0 distribution
- [Fix Compiler plugin crash when variable is passed for `sql:ConnectionPool` and `oracledb:Options`](https://github.com/ballerina-platform/ballerina-standard-library/issues/2536)

## [1.2.0] - 2021-12-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,18 @@ public void testOutParameterHint() {

}


@Test
public void testOptionsWithVariables() {
Package currentPackage = loadPackage("sample6");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
List<Diagnostic> diagnosticErrorStream = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
long availableErrors = diagnosticErrorStream.size();

Assert.assertEquals(availableErrors, 0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "oracledb_test"
name = "sample6"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2021, 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 ballerinax/oracledb;

public function main() {

decimal loginTimeout = 5;
decimal loginTimeoutInvalid = -5;

oracledb:Options options1 = {
loginTimeout: loginTimeout
};

oracledb:Options options2 = {
loginTimeout: loginTimeoutInvalid
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void validateOptions(SyntaxNodeAnalysisContext ctx, MappingConstru
case Constants.Options.CONNECT_TIMEOUT:
case Constants.Options.LOGIN_TIMEOUT:
case Constants.Options.SOCKET_TIMEOUT:
float timeoutVal = Float.parseFloat(getTerminalNodeValue(valueNode));
float timeoutVal = Float.parseFloat(getTerminalNodeValue(valueNode, "0"));
if (timeoutVal < 0) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(ORACLEDB_101.getCode(),
ORACLEDB_101.getMessage(), ORACLEDB_101.getSeverity());
Expand All @@ -108,15 +108,16 @@ public static void validateOptions(SyntaxNodeAnalysisContext ctx, MappingConstru
}
}

public static String getTerminalNodeValue(Node valueNode) {
String value = "";
public static String getTerminalNodeValue(Node valueNode, String defaultValue) {
String value = defaultValue;
if (valueNode instanceof BasicLiteralNode) {
value = ((BasicLiteralNode) valueNode).literalToken().text();
} else if (valueNode instanceof UnaryExpressionNode) {
UnaryExpressionNode unaryExpressionNode = (UnaryExpressionNode) valueNode;
value = unaryExpressionNode.unaryOperator() +
((BasicLiteralNode) unaryExpressionNode.expression()).literalToken().text();
}
// Currently, we cannot process values from variables, this needs code flow analysis
return value.replaceAll(UNNECESSARY_CHARS_REGEX, "");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
*/
package io.ballerina.stdlib.oracledb.compiler.analyzer;

import io.ballerina.compiler.syntax.tree.BasicLiteralNode;
import io.ballerina.compiler.syntax.tree.ExplicitNewExpressionNode;
import io.ballerina.compiler.syntax.tree.ExpressionNode;
import io.ballerina.compiler.syntax.tree.FunctionArgumentNode;
import io.ballerina.compiler.syntax.tree.ImplicitNewExpressionNode;
import io.ballerina.compiler.syntax.tree.MappingConstructorExpressionNode;
import io.ballerina.compiler.syntax.tree.MappingFieldNode;
import io.ballerina.compiler.syntax.tree.NamedArgumentNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.PositionalArgumentNode;
import io.ballerina.compiler.syntax.tree.SeparatedNodeList;
import io.ballerina.compiler.syntax.tree.SpecificFieldNode;
import io.ballerina.compiler.syntax.tree.UnaryExpressionNode;
import io.ballerina.projects.plugins.AnalysisTask;
import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext;
import io.ballerina.stdlib.oracledb.compiler.Constants;
Expand All @@ -48,6 +45,7 @@
import static io.ballerina.stdlib.oracledb.compiler.OracleDBDiagnosticsCode.SQL_101;
import static io.ballerina.stdlib.oracledb.compiler.OracleDBDiagnosticsCode.SQL_102;
import static io.ballerina.stdlib.oracledb.compiler.OracleDBDiagnosticsCode.SQL_103;
import static io.ballerina.stdlib.oracledb.compiler.Utils.getTerminalNodeValue;
import static io.ballerina.stdlib.oracledb.compiler.Utils.validateOptions;

/**
Expand Down Expand Up @@ -117,7 +115,7 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr
ExpressionNode valueNode = ((SpecificFieldNode) field).valueExpr().get();
switch (name) {
case Constants.ConnectionPool.MAX_OPEN_CONNECTIONS:
int maxOpenConnections = Integer.parseInt(getTerminalNodeValue(valueNode));
int maxOpenConnections = Integer.parseInt(getTerminalNodeValue(valueNode, "1"));
if (maxOpenConnections < 1) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(SQL_101.getCode(), SQL_101.getMessage(),
SQL_101.getSeverity());
Expand All @@ -128,7 +126,7 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr
}
break;
case Constants.ConnectionPool.MIN_IDLE_CONNECTIONS:
int minIdleConnection = Integer.parseInt(getTerminalNodeValue(valueNode));
int minIdleConnection = Integer.parseInt(getTerminalNodeValue(valueNode, "0"));
if (minIdleConnection < 0) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(SQL_102.getCode(), SQL_102.getMessage(),
SQL_102.getSeverity());
Expand All @@ -138,7 +136,7 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr
}
break;
case Constants.ConnectionPool.MAX_CONNECTION_LIFE_TIME:
float maxConnectionTime = Float.parseFloat(getTerminalNodeValue(valueNode));
float maxConnectionTime = Float.parseFloat(getTerminalNodeValue(valueNode, "30"));
if (maxConnectionTime < 30) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(SQL_103.getCode(), SQL_103.getMessage(),
SQL_103.getSeverity());
Expand All @@ -153,17 +151,4 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr
}
}
}

private String getTerminalNodeValue(Node valueNode) {
String value;
if (valueNode instanceof BasicLiteralNode) {
value = ((BasicLiteralNode) valueNode).literalToken().text();
} else {
UnaryExpressionNode unaryExpressionNode = (UnaryExpressionNode) valueNode;
value = unaryExpressionNode.unaryOperator() +
((BasicLiteralNode) unaryExpressionNode.expression()).literalToken().text();
}
return value.replaceAll(UNNECESSARY_CHARS_REGEX, "");
}

}

0 comments on commit dfb38a8

Please sign in to comment.