Skip to content

Commit 6873805

Browse files
authored
Merge pull request #840 from anuruddhal/master
Add support for NamedArg Listeners
2 parents d90af1f + 6ff1f14 commit 6873805

File tree

7 files changed

+68
-10
lines changed

7 files changed

+68
-10
lines changed

ballerina/Ballerina.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
org = "ballerina"
33
name = "cloud"
4-
version = "3.3.0"
4+
version = "3.3.1"
55
repository = "https://github.com/ballerina-platform/module-ballerina-c2c"
66
license = ["Apache-2.0"]
77
keywords = ["cloud", "kubernetes", "docker", "k8s", "c2c"]

ballerina/CompilerPlugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ id = "code2cloud"
33
class = "io.ballerina.c2c.C2CCompilerPlugin"
44

55
[[dependency]]
6-
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-3.3.0.jar"
6+
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-3.3.1-SNAPSHOT.jar"

ballerina/Dependencies.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ distribution-version = "2201.12.0"
1010
[[package]]
1111
org = "ballerina"
1212
name = "cloud"
13-
version = "3.3.0"
13+
version = "3.3.1"
1414
modules = [
1515
{org = "ballerina", packageName = "cloud", moduleName = "cloud"}
1616
]

cloud-util/src/main/java/io/ballerina/c2c/util/C2CVisitor.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import io.ballerina.compiler.syntax.tree.MappingFieldNode;
4343
import io.ballerina.compiler.syntax.tree.MetadataNode;
4444
import io.ballerina.compiler.syntax.tree.ModuleVariableDeclarationNode;
45+
import io.ballerina.compiler.syntax.tree.NamedArgumentNode;
4546
import io.ballerina.compiler.syntax.tree.Node;
4647
import io.ballerina.compiler.syntax.tree.NodeList;
4748
import io.ballerina.compiler.syntax.tree.NodeVisitor;
@@ -282,13 +283,27 @@ private Optional<ListenerInfo> extractListenerInitializer(String listenerName,
282283
}
283284
if (arguments.size() > paramNo) {
284285
FunctionArgumentNode functionArgumentNode = arguments.get(paramNo);
285-
ExpressionNode expression = ((PositionalArgumentNode) functionArgumentNode).expression();
286-
if (expression.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) {
287-
return getListenerInfo(listenerName, expression);
288-
} else if (expression instanceof BasicLiteralNode) {
289-
BasicLiteralNode basicLiteralNode = (BasicLiteralNode) expression;
290-
int port = Integer.parseInt(basicLiteralNode.literalToken().text());
291-
return Optional.of(new ListenerInfo(listenerName, port));
286+
if (functionArgumentNode.kind() == SyntaxKind.POSITIONAL_ARG) {
287+
// Listener with positional argument - on new http:Listener(9091)
288+
ExpressionNode expression = ((PositionalArgumentNode) functionArgumentNode).expression();
289+
if (expression.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) {
290+
return getListenerInfo(listenerName, expression);
291+
} else if (expression instanceof BasicLiteralNode basicLiteralNode) {
292+
int port = Integer.parseInt(basicLiteralNode.literalToken().text());
293+
return Optional.of(new ListenerInfo(listenerName, port));
294+
}
295+
} else if (functionArgumentNode.kind() == SyntaxKind.NAMED_ARG) {
296+
// Listener with named argument - on new http:Listener(port = 9091)
297+
NamedArgumentNode namedArgumentNode = (NamedArgumentNode) functionArgumentNode;
298+
if (namedArgumentNode.argumentName().name().text().equals("port")) {
299+
ExpressionNode expression = namedArgumentNode.expression();
300+
if (expression.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) {
301+
return getListenerInfo(listenerName, expression);
302+
} else if (expression instanceof BasicLiteralNode basicLiteralNode) {
303+
int port = Integer.parseInt(basicLiteralNode.literalToken().text());
304+
return Optional.of(new ListenerInfo(listenerName, port));
305+
}
306+
}
292307
}
293308
}
294309
return Optional.empty();

compiler-plugin-tests/src/test/java/io/ballerina/c2c/test/ServiceExtractionTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,14 @@ public void testMultiListenerExpose() {
352352
Assert.assertEquals(serviceList.get(0).getListeners().get(0).getPort(), 9091);
353353
Assert.assertEquals(serviceList.get(0).getListeners().get(1).getPort(), 9090);
354354
}
355+
356+
@Test
357+
public void testNamedArgListener() {
358+
Path projectPath = Paths.get("src", "test", "resources", "service", "named-param-port");
359+
BuildProject project = BuildProject.load(projectPath);
360+
ProjectServiceInfo projectServiceInfo = new ProjectServiceInfo(project);
361+
List<ServiceInfo> serviceList = projectServiceInfo.getServiceList();
362+
Assert.assertEquals(serviceList.size(), 1);
363+
Assert.assertEquals(serviceList.get(0).getListeners().get(0).getPort(), 8290);
364+
}
355365
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
org = "anuruddha"
3+
name = "namedparam"
4+
version = "0.1.0"
5+
6+
[build-options]
7+
observabilityIncluded = true
8+
cloud = "k8s"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2025 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2+
//
3+
// WSO2 Inc. licenses this file to you under the Apache License,
4+
// Version 2.0 (the "License"); you may not use this file except
5+
// in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing,
11+
// software distributed under the License is distributed on an
12+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
// KIND, either express or implied. See the License for the
14+
// specific language governing permissions and limitations
15+
// under the License.
16+
17+
import ballerina/http;
18+
19+
listener http:Listener healthListener = new (port = 8290);
20+
21+
service http:Service /probe on healthListener {
22+
resource function get readyz (http:Caller caller) returns error? {
23+
check caller->respond("Resource is Ready");
24+
}
25+
}

0 commit comments

Comments
 (0)