Skip to content

Commit

Permalink
Merge pull request #1036 from dilanSachi/allow-annotations-comp-plugin
Browse files Browse the repository at this point in the history
Update compiler plugin to allow annotations
  • Loading branch information
dilanSachi committed Aug 22, 2023
2 parents d6e8720 + 61973ac commit eddfe93
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 36 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ This file contains all the notable changes done to the Ballerina Email package t

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- [Changed disallowing service level annotations in the compiler plugin](https://github.com/ballerina-platform/ballerina-standard-library/issues/4741)

## [2.7.1] - 2023-06-01

- [Fixed log manager getting reset by `ftp` module](https://github.com/ballerina-platform/ballerina-standard-library/issues/4478)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private CodeActionInfo getExpectedCodeAction(String filePath, int line, int offs
LinePosition.from(line, offset));
CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION, lineRange);
CodeActionInfo codeAction = CodeActionInfo.from(actionName, List.of(locationArg));
codeAction.setProviderName("FTP_112/ballerina/ftp/" + templateName);
codeAction.setProviderName("FTP_111/ballerina/ftp/" + templateName);
return codeAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public void testValidService5() {
Assert.assertEquals(diagnosticResult.errors().size(), 0);
}

@Test(description = "Validation with service containing annotations")
public void testValidService6() {
Package currentPackage = loadPackage("valid_service_6");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.errors().size(), 0);
}

@Test(description = "Validation when no onFileChange function is defined")
public void testInvalidService1() {
Package currentPackage = loadPackage("invalid_service_1");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "ftp_test"
name = "valid_service_6"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023 WSO2 LLC. (http://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/ftp;

listener ftp:Listener remoteServer = check new({
protocol: ftp:FTP,
host: "localhost",
port: 21213,
pollingInterval: 2,
path: "/upload/",
fileNamePattern: "(.*).csv"
});

@display {
label: "FTP Listener Service"
}
service on remoteServer {
remote function onFileChange(ftp:WatchEvent & readonly event) returns error? {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@

package io.ballerina.stdlib.ftp.plugin;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.AnnotationSymbol;
import io.ballerina.compiler.api.symbols.MethodSymbol;
import io.ballerina.compiler.api.symbols.ServiceDeclarationSymbol;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NodeList;
import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext;
import io.ballerina.stdlib.ftp.plugin.PluginConstants.CompilationErrors;
import io.ballerina.tools.diagnostics.DiagnosticFactory;
import io.ballerina.tools.diagnostics.DiagnosticInfo;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;

import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.ftp.plugin.PluginConstants.CompilationErrors.INVALID_REMOTE_FUNCTION;
Expand Down Expand Up @@ -65,7 +59,6 @@ public void validate(SyntaxNodeAnalysisContext context) {
serviceDeclarationNode.location()));
}

validateAnnotation(context);
FunctionDefinitionNode onFileChange = null;

for (Node node : memberNodes) {
Expand All @@ -88,18 +81,4 @@ public void validate(SyntaxNodeAnalysisContext context) {
}
new FtpFunctionValidator(context, onFileChange).validate();
}

private void validateAnnotation(SyntaxNodeAnalysisContext context) {
SemanticModel semanticModel = context.semanticModel();
ServiceDeclarationNode serviceDeclarationNode = (ServiceDeclarationNode) context.node();
Optional<Symbol> symbol = semanticModel.symbol(serviceDeclarationNode);
if (symbol.isPresent()) {
ServiceDeclarationSymbol serviceDeclarationSymbol = (ServiceDeclarationSymbol) symbol.get();
List<AnnotationSymbol> symbolList = serviceDeclarationSymbol.annotations();
if (!symbolList.isEmpty()) {
context.reportDiagnostic(PluginUtils.getDiagnostic(CompilationErrors.INVALID_ANNOTATION_NUMBER,
DiagnosticSeverity.ERROR, serviceDeclarationNode.location()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,21 @@ public class PluginConstants {
public static final String CODE_TEMPLATE_NAME_WITHOUT_CALLER = "ADD_REMOTE_FUNCTION_CODE_SNIPPET_WITHOUT_CALLER";

enum CompilationErrors {
INVALID_ANNOTATION_NUMBER("No annotations are allowed for ftp services.", "FTP_101"),
INVALID_REMOTE_FUNCTION("Only onFileChange remote method is allowed for ftp services.", "FTP_102"),
METHOD_MUST_BE_REMOTE("onFileChange method must be remote.", "FTP_103"),
INVALID_REMOTE_FUNCTION("Only onFileChange remote method is allowed for ftp services.", "FTP_101"),
METHOD_MUST_BE_REMOTE("onFileChange method must be remote.", "FTP_102"),
RESOURCE_FUNCTION_NOT_ALLOWED("Resource functions are not allowed for ftp services.", "FTP_103"),
NO_ON_FILE_CHANGE("onFileChange method not found.", "FTP_105"),
NO_ON_FILE_CHANGE("onFileChange method not found.", "FTP_104"),
MUST_HAVE_WATCHEVENT("Must have the required parameter ftp:WatchEvent & readonly or ftp:WatchEvent.",
"FTP_106"),
"FTP_105"),
ONLY_PARAMS_ALLOWED("Invalid method parameter count. Only ftp:WatchEvent & readonly " +
"or ftp:WatchEvent is allowed.", "FTP_107"),
"or ftp:WatchEvent is allowed.", "FTP_106"),
INVALID_WATCHEVENT_PARAMETER("Invalid method parameter. Only ftp:WatchEvent & readonly or " +
"ftp:WatchEvent is allowed.", "FTP_108"),
INVALID_CALLER_PARAMETER("Invalid method parameter. Only ftp:Caller is allowed", "FTP_109"),
"ftp:WatchEvent is allowed.", "FTP_107"),
INVALID_CALLER_PARAMETER("Invalid method parameter. Only ftp:Caller is allowed", "FTP_108"),
INVALID_PARAMETERS("Invalid method parameters. Only ftp:WatchEvent & readonly or ftp:WatchEvent and " +
"ftp:Caller is allowed.", "FTP_110"),
INVALID_RETURN_TYPE_ERROR_OR_NIL("Invalid return type. Only error? or ftp:Error? is allowed.", "FTP_111"),
TEMPLATE_CODE_GENERATION_HINT("Template generation for empty service", "FTP_112");
"ftp:Caller is allowed.", "FTP_109"),
INVALID_RETURN_TYPE_ERROR_OR_NIL("Invalid return type. Only error? or ftp:Error? is allowed.", "FTP_110"),
TEMPLATE_CODE_GENERATION_HINT("Template generation for empty service", "FTP_111");
private final String error;
private final String errorCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package io.ballerina.stdlib.ftp.server;

import io.ballerina.runtime.api.Runtime;
import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.MethodType;
import io.ballerina.runtime.api.utils.StringUtils;
Expand Down Expand Up @@ -60,11 +60,11 @@ private FtpListenerHelper() {
* @param ftpListener Listener that places `ftp:WatchEvent` by Ballerina runtime
* @param serviceEndpointConfig FTP server endpoint configuration
*/
public static Object init(BObject ftpListener, BMap<BString, Object> serviceEndpointConfig) {
public static Object init(Environment env, BObject ftpListener, BMap<BString, Object> serviceEndpointConfig) {
try {
Map<String, String> paramMap = getServerConnectorParamMap(serviceEndpointConfig);
RemoteFileSystemConnectorFactory fileSystemConnectorFactory = new RemoteFileSystemConnectorFactoryImpl();
final FtpListener listener = new FtpListener(Runtime.getCurrentRuntime());
final FtpListener listener = new FtpListener(env.getRuntime());
RemoteFileSystemServerConnector serverConnector = fileSystemConnectorFactory
.createServerConnector(paramMap, listener);
ftpListener.addNativeData(FtpConstants.FTP_SERVER_CONNECTOR, serverConnector);
Expand Down

0 comments on commit eddfe93

Please sign in to comment.