Skip to content

Commit

Permalink
Merge pull request #980 from dilanSachi/fix-comp-plugin-7.x
Browse files Browse the repository at this point in the history
[7.x] Update compiler plugin to allow annotationsFix comp plugin 7.x
  • Loading branch information
dilanSachi authored Aug 18, 2023
2 parents 65bfc2f + c0498bf commit 1bf2de2
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 45 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/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerinax"
name = "kafka"
version = "3.9.0"
version = "3.9.1"
authors = ["Ballerina"]
keywords = ["kafka", "event streaming", "network", "messaging"]
repository = "https://github.com/ballerina-platform/module-ballerinax-kafka"
Expand All @@ -15,8 +15,8 @@ graalvmCompatible = true
[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "kafka-native"
version = "3.9.0"
path = "../native/build/libs/kafka-native-3.9.0.jar"
version = "3.9.1"
path = "../native/build/libs/kafka-native-3.9.1-SNAPSHOT.jar"

[[platform.java11.dependency]]
groupId = "org.apache.kafka"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "kafka-compiler-plugin"
class = "io.ballerina.stdlib.kafka.plugin.KafkaCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/kafka-compiler-plugin-3.9.0.jar"
path = "../compiler-plugin/build/libs/kafka-compiler-plugin-3.9.1-SNAPSHOT.jar"
8 changes: 4 additions & 4 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ modules = [
[[package]]
org = "ballerina"
name = "file"
version = "1.8.0"
version = "1.8.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
Expand All @@ -71,7 +71,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.9.0"
version = "2.9.2"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -243,7 +243,7 @@ modules = [
[[package]]
org = "ballerina"
name = "log"
version = "2.8.0"
version = "2.8.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
Expand Down Expand Up @@ -389,7 +389,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "kafka"
version = "3.9.0"
version = "3.9.1"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
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
10 changes: 9 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ 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]
## 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)

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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.caching=true
group=io.ballerina.stdlib
version=3.9.0
version=3.9.1-SNAPSHOT
kafkaVersion=2.8.0
ballerinaLangVersion= 2201.7.0
ballerinaGradlePluginVersion=1.1.0
Expand Down

0 comments on commit 1bf2de2

Please sign in to comment.