4
4
5
5
package kotlinx.rpc.protoc.gen.grpc
6
6
7
+ import com.google.protobuf.DescriptorProtos
7
8
import kotlinx.rpc.protoc.gen.core.AModelToKotlinCommonGenerator
8
9
import kotlinx.rpc.protoc.gen.core.CodeGenerator
9
10
import kotlinx.rpc.protoc.gen.core.model.FileDeclaration
11
+ import kotlinx.rpc.protoc.gen.core.model.FqName
10
12
import kotlinx.rpc.protoc.gen.core.model.Model
11
13
import kotlinx.rpc.protoc.gen.core.model.ServiceDeclaration
12
14
import org.slf4j.Logger
@@ -21,15 +23,15 @@ class ModelToGrpcKotlinCommonGenerator(
21
23
22
24
override fun CodeGenerator.generatePublicDeclaredEntities (fileDeclaration : FileDeclaration ) {
23
25
additionalPublicImports.add(" kotlinx.coroutines.flow.Flow" )
24
- fileDeclaration.serviceDeclarations.forEach { generatePublicService(it) }
26
+ fileDeclaration.serviceDeclarations.forEach { generatePublicService(it, fileDeclaration.packageName ) }
25
27
}
26
28
27
29
override fun CodeGenerator.generateInternalDeclaredEntities (fileDeclaration : FileDeclaration ) {}
28
30
29
31
@Suppress(" detekt.LongMethod" )
30
- private fun CodeGenerator.generatePublicService (service : ServiceDeclaration ) {
32
+ private fun CodeGenerator.generatePublicService (service : ServiceDeclaration , packageName : FqName . Package ) {
31
33
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 " "
33
35
34
36
clazz(
35
37
name = service.name.simpleName,
@@ -39,10 +41,17 @@ class ModelToGrpcKotlinCommonGenerator(
39
41
service.methods.forEach { method ->
40
42
val inputType = method.inputType
41
43
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
+
42
50
function(
43
51
name = method.name,
44
52
modifiers = if (method.dec.isServerStreaming) " " else " suspend" ,
45
53
args = " message: ${inputType.name.safeFullName().wrapInFlowIf(method.dec.isClientStreaming)} " ,
54
+ annotations = annotations,
46
55
returnType = outputType.name.safeFullName().wrapInFlowIf(method.dec.isServerStreaming),
47
56
)
48
57
}
0 commit comments