From 4edaa49198d61bf782030b8bac097c04d97bed8f Mon Sep 17 00:00:00 2001 From: anuruddhal Date: Tue, 26 Sep 2023 12:53:33 +0530 Subject: [PATCH] Fix parsing inline listeners --- .../io/ballerina/c2c/util/C2CVisitor.java | 9 ++-- .../c2c/test/ServiceExtractionTest.java | 11 ++++ .../service/listener-variable/Ballerina.toml | 8 +++ .../service/listener-variable/service.bal | 52 +++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 compiler-plugin-tests/src/test/resources/service/listener-variable/Ballerina.toml create mode 100644 compiler-plugin-tests/src/test/resources/service/listener-variable/service.bal diff --git a/cloud-util/src/main/java/io/ballerina/c2c/util/C2CVisitor.java b/cloud-util/src/main/java/io/ballerina/c2c/util/C2CVisitor.java index 9ed7404c..948b72be 100644 --- a/cloud-util/src/main/java/io/ballerina/c2c/util/C2CVisitor.java +++ b/cloud-util/src/main/java/io/ballerina/c2c/util/C2CVisitor.java @@ -302,7 +302,7 @@ public void visit(ServiceDeclarationNode serviceDeclarationNode) { List typeSymbols = symbol.listenerTypes(); if (typeSymbols.isEmpty()) { return; - } + } String servicePath = toAbsoluteServicePath(serviceDeclarationNode.absoluteResourcePath()); if (!isC2CNativelySupportedListener(typeSymbols)) { processCustomExposedAnnotatedListeners(typeSymbols, servicePath, serviceDeclarationNode); @@ -325,7 +325,7 @@ public void visit(ServiceDeclarationNode serviceDeclarationNode) { } services.add(serviceInfo); } - + private List extractListeners(ServiceDeclarationNode serviceDeclarationNode, String servicePath) { SeparatedNodeList expressions = serviceDeclarationNode.expressions(); List listeners = new ArrayList<>(); @@ -344,6 +344,9 @@ private List extractListeners(ServiceDeclarationNode serviceDeclar } listenerInfo = httpsListener.get(); } else { + if (expressionNode.kind() != SyntaxKind.EXPLICIT_NEW_EXPRESSION) { + return Collections.emptyList(); + } //Inline Listener ExplicitNewExpressionNode refNode = (ExplicitNewExpressionNode) expressionNode; FunctionArgumentNode functionArgumentNode = refNode.parenthesizedArgList().arguments().get(0); @@ -354,7 +357,7 @@ private List extractListeners(ServiceDeclarationNode serviceDeclar return Collections.emptyList(); } listenerInfo = newListenerInfo.get(); - + //Inline Http config if (refNode.parenthesizedArgList().arguments().size() > 1) { FunctionArgumentNode secondParamExpression = refNode.parenthesizedArgList().arguments().get(1); diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/ServiceExtractionTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/ServiceExtractionTest.java index 564ba99f..ad2361c7 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/ServiceExtractionTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/ServiceExtractionTest.java @@ -53,6 +53,17 @@ public void testSimpleHttpNoServiceDecl() { Assert.assertEquals(listener.getPort(), 9090); } + @Test + public void testVariableHttpServiceListenerDecl() { + Path projectPath = Paths.get("src", "test", "resources", "service", "listener-variable"); + + BuildProject project = BuildProject.load(projectPath); + ProjectServiceInfo projectServiceInfo = new ProjectServiceInfo(project); + List serviceList = projectServiceInfo.getServiceList(); + + Assert.assertEquals(serviceList.size(), 1); + } + @Test public void testExposeOnListenerDeclaration() { Path projectPath = Paths.get("src", "test", "resources", "service", "expose-on-listener"); diff --git a/compiler-plugin-tests/src/test/resources/service/listener-variable/Ballerina.toml b/compiler-plugin-tests/src/test/resources/service/listener-variable/Ballerina.toml new file mode 100644 index 00000000..7ca13d36 --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/service/listener-variable/Ballerina.toml @@ -0,0 +1,8 @@ +[package] +org = "ballerina" +name = "clientconfig" +version = "0.1.0" + +[build-options] +observabilityIncluded = true +cloud = "k8s" diff --git a/compiler-plugin-tests/src/test/resources/service/listener-variable/service.bal b/compiler-plugin-tests/src/test/resources/service/listener-variable/service.bal new file mode 100644 index 00000000..425fee8a --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/service/listener-variable/service.bal @@ -0,0 +1,52 @@ +// Copyright (c) 2023 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 ballerina/log; +import ballerina/http; + +isolated service /registerAPI on internalLs { + + isolated resource function post .(string apiName, string candidateGateway) + returns http:Created|http:InternalServerError { + do { + log:printInfo("Registering API: " + apiName + " with gateway: " + candidateGateway); + } on fail { + return http:INTERNAL_SERVER_ERROR; + } + log:printInfo("Registered API: " + apiName + " with gateway: " + candidateGateway); + return http:CREATED; + } +} + +configurable int INTERNAL_SERVICE_PORT = 9090; +configurable string ADMIN_SERVICE_HOST = "localhost:9443"; +configurable string CERT_PATH = "./resources/wso2carbon.cer"; +configurable string ADMIN_SERVICE_SCOPES = "apim:tenant_theme_manage apim:tier_manage apim:tier_view openid"; +configurable string TOKEN_URL = "https://localhost:9443/oauth2/token"; +configurable string CLIENT_ID = "xxxxxxx"; +configurable string CLIENT_SECRET = "xxxxxxx"; + +final http:Client apimAdmin = check new (ADMIN_SERVICE_HOST, auth = { + tokenUrl: TOKEN_URL, + clientId: CLIENT_ID, + clientSecret: CLIENT_SECRET, + scopes: ADMIN_SERVICE_SCOPES, + clientConfig: { + secureSocket: {cert: CERT_PATH} + } +}, secureSocket = {cert: CERT_PATH}); + +public listener http:Listener internalLs = new (INTERNAL_SERVICE_PORT);