Skip to content

Commit 746c245

Browse files
committed
Added pb idempotency generator
1 parent 36e9d43 commit 746c245

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
3+
package kotlinx.rpc.grpc.test;
4+
5+
message Message {
6+
string message = 1;
7+
}
8+
9+
service Idempotency {
10+
rpc Idempotent(Message) returns (Message) {
11+
option idempotency_level = IDEMPOTENT;
12+
}
13+
rpc Safe(Message) returns (Message) {
14+
option idempotency_level = NO_SIDE_EFFECTS;
15+
}
16+
}

protoc-gen/grpc/src/main/kotlin/kotlinx/rpc/protoc/gen/grpc/ModelToGrpcKotlinCommonGenerator.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
package kotlinx.rpc.protoc.gen.grpc
66

7+
import com.google.protobuf.DescriptorProtos
78
import kotlinx.rpc.protoc.gen.core.AModelToKotlinCommonGenerator
89
import kotlinx.rpc.protoc.gen.core.CodeGenerator
910
import kotlinx.rpc.protoc.gen.core.model.FileDeclaration
11+
import kotlinx.rpc.protoc.gen.core.model.FqName
1012
import kotlinx.rpc.protoc.gen.core.model.Model
1113
import kotlinx.rpc.protoc.gen.core.model.ServiceDeclaration
1214
import org.slf4j.Logger
@@ -21,15 +23,15 @@ class ModelToGrpcKotlinCommonGenerator(
2123

2224
override fun CodeGenerator.generatePublicDeclaredEntities(fileDeclaration: FileDeclaration) {
2325
additionalPublicImports.add("kotlinx.coroutines.flow.Flow")
24-
fileDeclaration.serviceDeclarations.forEach { generatePublicService(it) }
26+
fileDeclaration.serviceDeclarations.forEach { generatePublicService(it, fileDeclaration.packageName) }
2527
}
2628

2729
override fun CodeGenerator.generateInternalDeclaredEntities(fileDeclaration: FileDeclaration) {}
2830

2931
@Suppress("detekt.LongMethod")
30-
private fun CodeGenerator.generatePublicService(service: ServiceDeclaration) {
32+
private fun CodeGenerator.generatePublicService(service: ServiceDeclaration, packageName: FqName.Package) {
3133
val pkg = service.dec.file.`package`.orEmpty()
32-
val annotationParams = if (pkg.isNotEmpty()) """(protoPackage = "$pkg")""" else ""
34+
val annotationParams = if (pkg.isNotEmpty() && pkg != packageName.safeFullName()) """(protoPackage = "$pkg")""" else ""
3335

3436
clazz(
3537
name = service.name.simpleName,
@@ -39,10 +41,17 @@ class ModelToGrpcKotlinCommonGenerator(
3941
service.methods.forEach { method ->
4042
val inputType = method.inputType
4143
val outputType = method.outputType
44+
val annotations = when (method.dec.options.idempotencyLevel) {
45+
null, DescriptorProtos.MethodOptions.IdempotencyLevel.IDEMPOTENCY_UNKNOWN -> emptyList()
46+
DescriptorProtos.MethodOptions.IdempotencyLevel.IDEMPOTENT -> listOf("@kotlinx.rpc.grpc.annotations.Grpc.Method(idempotent = true)")
47+
DescriptorProtos.MethodOptions.IdempotencyLevel.NO_SIDE_EFFECTS -> listOf("@kotlinx.rpc.grpc.annotations.Grpc.Method(idempotent = true, safe = true)")
48+
}
49+
4250
function(
4351
name = method.name,
4452
modifiers = if (method.dec.isServerStreaming) "" else "suspend",
4553
args = "message: ${inputType.name.safeFullName().wrapInFlowIf(method.dec.isClientStreaming)}",
54+
annotations = annotations,
4655
returnType = outputType.name.safeFullName().wrapInFlowIf(method.dec.isServerStreaming),
4756
)
4857
}

0 commit comments

Comments
 (0)