Skip to content

Commit

Permalink
Merge pull request #979 from dilanSachi/fix-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 21, 2023
2 parents c1b4fa4 + c7a62da commit 2cc3d82
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The Kafka consumer can be used as a listener to a set of topics without the need

You can use the `Caller` to manually commit the offsets of the messages that are read by the service. The following code snippet shows how to initialize and define the listener and how to commit the offsets manually.
```ballerina
kafka:ConsumerConfiguration consumerConfigs = {
kafka:ConsumerConfiguration consumerConfiguration = {
groupId: "group-id",
topics: ["kafka-topic-1"],
pollingInterval: 1,
Expand All @@ -58,7 +58,7 @@ kafka:ConsumerConfiguration consumerConfigs = {
listener kafka:Listener kafkaListener = new (kafka:DEFAULT_URL, consumerConfiguration);
service kafka:Service on kafkaListener {
service on kafkaListener {
remote function onConsumerRecord(kafka:Caller caller, kafka:ConsumerRecord[] records) {
// processes the records
...
Expand Down Expand Up @@ -104,7 +104,7 @@ Topic partitions are assigned to consumers automatically or you can manually ass

The following code snippet joins a consumer to the `consumer-group` and assigns it to a topic partition manually.
```ballerina
kafka:ConsumerConfiguration consumerConfigs = {
kafka:ConsumerConfiguration consumerConfiguration = {
// `groupId` determines the consumer group
groupId: "consumer-group",
pollingInterval: 1,
Expand Down
6 changes: 3 additions & 3 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The Kafka consumer can be used as a listener to a set of topics without the need

You can use the `Caller` to manually commit the offsets of the messages that are read by the service. The following code snippet shows how to initialize and define the listener and how to commit the offsets manually.
```ballerina
kafka:ConsumerConfiguration consumerConfigs = {
kafka:ConsumerConfiguration consumerConfiguration = {
groupId: "group-id",
topics: ["kafka-topic-1"],
pollingInterval: 1,
Expand All @@ -51,7 +51,7 @@ kafka:ConsumerConfiguration consumerConfigs = {
listener kafka:Listener kafkaListener = new (kafka:DEFAULT_URL, consumerConfiguration);
service kafka:Service on kafkaListener {
service on kafkaListener {
remote function onConsumerRecord(kafka:Caller caller, kafka:ConsumerRecord[] records) {
// processes the records
...
Expand Down Expand Up @@ -97,7 +97,7 @@ Topic partitions are assigned to consumers automatically or you can manually ass

The following code snippet joins a consumer to the `consumer-group` and assigns it to a topic partition manually.
```ballerina
kafka:ConsumerConfiguration consumerConfigs = {
kafka:ConsumerConfiguration consumerConfiguration = {
// `groupId` determines the consumer group
groupId: "consumer-group",
pollingInterval: 1,
Expand Down
6 changes: 3 additions & 3 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The Kafka consumer can be used as a listener to a set of topics without the need

You can use the `Caller` to manually commit the offsets of the messages that are read by the service. The following code snippet shows how to initialize and define the listener and how to commit the offsets manually.
```ballerina
kafka:ConsumerConfiguration consumerConfigs = {
kafka:ConsumerConfiguration consumerConfiguration = {
groupId: "group-id",
topics: ["kafka-topic-1"],
pollingInterval: 1,
Expand All @@ -50,7 +50,7 @@ kafka:ConsumerConfiguration consumerConfigs = {
listener kafka:Listener kafkaListener = new (kafka:DEFAULT_URL, consumerConfiguration);
service kafka:Service on kafkaListener {
service on kafkaListener {
remote function onConsumerRecord(kafka:Caller caller, kafka:ConsumerRecord[] records) {
// processes the records
...
Expand Down Expand Up @@ -96,7 +96,7 @@ Topic partitions are assigned to consumers automatically or you can manually ass

The following code snippet joins a consumer to the `consumer-group` and assigns it to a topic partition manually.
```ballerina
kafka:ConsumerConfiguration consumerConfigs = {
kafka:ConsumerConfiguration consumerConfiguration = {
// `groupId` determines the consumer group
groupId: "consumer-group",
pollingInterval: 1,
Expand Down
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ This file contains all the notable changes done to the Ballerina Kafka 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/4731)

## [3.8.0] - 2023-06-01

### Fixed

- [Fixed log manager getting reset by `kafka` module](https://github.com/ballerina-platform/ballerina-standard-library/issues/4493)

## [3.7.1] - 2023-05-09
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("KAFKA_112/ballerinax/kafka/" + templateName);
codeAction.setProviderName("KAFKA_111/ballerinax/kafka/" + templateName);
return codeAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public void testValidService13() {
Assert.assertEquals(diagnosticResult.errors().size(), 0);
}

@Test(enabled = true, description = "Validate `kafka:Service` with `display` annotation")
public void testValidService14() {
Package currentPackage = loadPackage("valid_service_14");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.errors().size(), 0);
}

@Test(enabled = true, description = "Validate no remote method")
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 = "kafka_test"
name = "valid_service_14"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 ballerinax/kafka;

kafka:ConsumerConfiguration consumerConfigs = {
groupId: "group-id",
topics: ["test-kafka-topic"],
pollingInterval: 1,
autoCommit: false
};

listener kafka:Listener kafkaListener = new (kafka:DEFAULT_URL, consumerConfigs);

@display {
label: "kafkaService"
}
service on kafkaListener {

private final string var1 = "Kafka Service";
private final int var2 = 54;

remote function onConsumerRecord(kafka:ConsumerRecord[] records, kafka:Caller caller) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@

package io.ballerina.stdlib.kafka.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;
Expand All @@ -34,7 +30,6 @@
import io.ballerina.tools.diagnostics.DiagnosticInfo;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;

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

/**
Expand All @@ -60,7 +55,6 @@ public void validate(SyntaxNodeAnalysisContext context) {
serviceDeclarationNode.location()));
}

validateAnnotation(context);
FunctionDefinitionNode onConsumerRecord = null;
FunctionDefinitionNode onError = null;

Expand All @@ -86,18 +80,4 @@ public void validate(SyntaxNodeAnalysisContext context) {
}
new KafkaFunctionValidator(context, onConsumerRecord, onError).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 @@ -73,11 +73,10 @@ enum CompilationErrors {
"KAFKA_109"),
INVALID_MULTIPLE_LISTENERS("Multiple listener attachments. Only one kafka:Listener is allowed.",
"KAFKA_110"),
INVALID_ANNOTATION_NUMBER("No annotations are allowed for kafka services.", "KAFKA_111"),
TEMPLATE_CODE_GENERATION_HINT("Template generation for empty service", "KAFKA_112"),
MUST_HAVE_ERROR("Must have the required parameter kafka:Error", "KAFKA_113"),
ONLY_ERROR_ALLOWED("Invalid method parameter. Only kafka:Error or error is allowed", "KAFKA_114"),
ONLY_CALLER_ALLOWED("Invalid method parameter. Only kafka:Caller is allowed", "KAFKA_115");
TEMPLATE_CODE_GENERATION_HINT("Template generation for empty service", "KAFKA_111"),
MUST_HAVE_ERROR("Must have the required parameter kafka:Error", "KAFKA_112"),
ONLY_ERROR_ALLOWED("Invalid method parameter. Only kafka:Error or error is allowed", "KAFKA_113"),
ONLY_CALLER_ALLOWED("Invalid method parameter. Only kafka:Caller is allowed", "KAFKA_114");

private final String error;
private final String errorCode;
Expand Down

0 comments on commit 2cc3d82

Please sign in to comment.