From e07e0e74597c03cfc839e32cb1c0eb182ff1fd1b Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 May 2024 16:56:42 +0530 Subject: [PATCH] Add support for named-args and rest-args --- .../ballerina_sources/sample_25/service.bal | 20 +++++++++++++++++++ .../websub/task/ListenerInitAnalysisTask.java | 16 +++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_25/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_25/service.bal index e48e9f12..00c45eea 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_25/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_25/service.bal @@ -27,6 +27,26 @@ listener websub:Listener ln = check new(port, { } }); +listener websub:Listener ln2 = check new(listenTo = port, config = { + secureSocket: { + key: { + path: "tests/resources/ballerinaKeystore.pkcs12", + password: "ballerina" + } + } +}); + +listener websub:Listener ln3 = check new(listenTo = port); + +listener websub:Listener ln4 = check new(listenTo = 9090, config = { + secureSocket: { + key: { + path: "tests/resources/ballerinaKeystore.pkcs12", + password: "ballerina" + } + } +}); + @websub:SubscriberServiceConfig { } service /sample on new websub:Listener(port, { diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/websub/task/ListenerInitAnalysisTask.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/websub/task/ListenerInitAnalysisTask.java index 8419848e..8eb137f1 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/websub/task/ListenerInitAnalysisTask.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/websub/task/ListenerInitAnalysisTask.java @@ -18,6 +18,7 @@ package io.ballerina.stdlib.websub.task; +import io.ballerina.compiler.api.SemanticModel; import io.ballerina.compiler.api.symbols.Symbol; import io.ballerina.compiler.api.symbols.SymbolKind; import io.ballerina.compiler.api.symbols.TypeDescKind; @@ -28,7 +29,9 @@ import io.ballerina.compiler.syntax.tree.FunctionArgumentNode; import io.ballerina.compiler.syntax.tree.ImplicitNewExpressionNode; import io.ballerina.compiler.syntax.tree.ListenerDeclarationNode; +import io.ballerina.compiler.syntax.tree.NamedArgumentNode; import io.ballerina.compiler.syntax.tree.PositionalArgumentNode; +import io.ballerina.compiler.syntax.tree.RestArgumentNode; import io.ballerina.compiler.syntax.tree.SeparatedNodeList; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.TypeDescriptorNode; @@ -112,8 +115,7 @@ private void verifyListenerArgType(SyntaxNodeAnalysisContext context, SeparatedNodeList functionArgs) { // two args are valid only if the first arg is numeric (i.e, port and config) if (functionArgs.size() > 1) { - PositionalArgumentNode firstArg = (PositionalArgumentNode) functionArgs.get(0); - Optional firstArgSymbolOpt = context.semanticModel().symbol(firstArg.expression()); + Optional firstArgSymbolOpt = getFirstListenerArg(context.semanticModel(), functionArgs.get(0)); if (firstArgSymbolOpt.isEmpty()) { return; } @@ -128,4 +130,14 @@ private void verifyListenerArgType(SyntaxNodeAnalysisContext context, updateContext(context, WebSubDiagnosticCodes.WEBSUB_109, secondArg.location()); } } + + private Optional getFirstListenerArg(SemanticModel semanticModel, FunctionArgumentNode firstArg) { + if (SyntaxKind.POSITIONAL_ARG.equals(firstArg.kind())) { + return semanticModel.symbol(((PositionalArgumentNode) firstArg).expression()); + } else if (SyntaxKind.NAMED_ARG.equals(firstArg.kind())) { + return semanticModel.symbol(((NamedArgumentNode) firstArg).expression()); + } else { + return semanticModel.symbol(((RestArgumentNode) firstArg).expression()); + } + } }