Skip to content

Commit a610c49

Browse files
committed
grpc: Separate between kotlin FQ name and service (grpc) FQ name
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 047cc2c commit a610c49

File tree

11 files changed

+436
-45
lines changed

11 files changed

+436
-45
lines changed

compiler-plugin/compiler-plugin-backend/src/main/kotlin/kotlinx/rpc/codegen/extension/RpcDeclarationScanner.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ import org.jetbrains.kotlin.ir.declarations.IrClass
1111
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
1212
import org.jetbrains.kotlin.ir.declarations.IrProperty
1313
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
14+
import org.jetbrains.kotlin.ir.expressions.IrConst
15+
import org.jetbrains.kotlin.ir.expressions.IrConstKind
16+
import org.jetbrains.kotlin.ir.expressions.IrExpression
1417
import org.jetbrains.kotlin.ir.util.dumpKotlinLike
18+
import org.jetbrains.kotlin.ir.util.getAnnotation
1519
import org.jetbrains.kotlin.ir.util.hasDefaultValue
20+
import org.jetbrains.kotlin.ir.util.kotlinFqName
1621

1722
/**
1823
* This class scans user declared RPC service
@@ -25,6 +30,9 @@ internal object RpcDeclarationScanner {
2530
fun scanServiceDeclaration(service: IrClass, ctx: RpcIrContext, logger: MessageCollector): ServiceDeclaration {
2631
var stubClass: IrClass? = null
2732

33+
val grpcAnnotation = service.getAnnotation(ctx.grpcAnnotation.owner.kotlinFqName)
34+
val protoPackage = grpcAnnotation?.arguments?.getOrNull(0)?.asConstString() ?: ""
35+
2836
val declarations = service.declarations.memoryOptimizedMap { declaration ->
2937
when (declaration) {
3038
is IrSimpleFunction -> {
@@ -75,6 +83,7 @@ internal object RpcDeclarationScanner {
7583
service = service,
7684
stubClass = stubClassNotNull,
7785
methods = declarations.filterNotNull(),
86+
protoPackage = protoPackage.trim()
7887
)
7988
}
8089
}
@@ -87,3 +96,7 @@ private fun unsupportedDeclaration(service: IrClass, declaration: IrDeclaration,
8796

8897
return null
8998
}
99+
100+
fun IrExpression.asConstString(): String =
101+
(this as? IrConst)?.takeIf { it.kind == IrConstKind.String }?.value as? String
102+
?: error("Expected IrConst of kind String, got ${dumpKotlinLike()}")

compiler-plugin/compiler-plugin-backend/src/main/kotlin/kotlinx/rpc/codegen/extension/RpcIrContext.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ internal class RpcIrContext(
6464
getRpcIrClassSymbol("RpcServiceDescriptor", "descriptor")
6565
}
6666

67+
val grpcAnnotation by lazy {
68+
getIrClassSymbol("kotlinx.rpc.grpc.annotations", "Grpc")
69+
}
70+
6771
val grpcServiceDescriptor by lazy {
6872
getIrClassSymbol("kotlinx.rpc.grpc.descriptor", "GrpcServiceDescriptor")
6973
}

compiler-plugin/compiler-plugin-backend/src/main/kotlin/kotlinx/rpc/codegen/extension/RpcStubGenerator.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ private object Stub {
3535
private object Descriptor {
3636
const val CALLABLE_MAP = "callableMap"
3737
const val CALLABLES = "callables"
38+
39+
// the kotlin class name (derived from java_package and name)
3840
const val FQ_NAME = "fqName"
3941
const val SIMPLE_NAME = "simpleName"
4042
const val GET_CALLABLE = "getCallable"
@@ -1102,7 +1104,7 @@ internal class RpcStubGenerator(
11021104
}.apply {
11031105
arguments {
11041106
values {
1105-
+stringConst(declaration.fqName)
1107+
+stringConst(declaration.serviceFqName)
11061108

11071109
+irCallProperty(
11081110
receiver = IrGetValueImpl(
@@ -1127,7 +1129,7 @@ internal class RpcStubGenerator(
11271129
* // In scope: resolver: MessageCodecResolver
11281130
*
11291131
* methodDescriptor<<request-type>, <response-type>>(
1130-
* fullMethodName = "${descriptor.fqName}/${callable.name}",
1132+
* fullMethodName = "${descriptor.serviceFqName}/${callable.name}",
11311133
* requestCodec = <request-codec>,
11321134
* responseCodec = <response-codec>,
11331135
* type = MethodType.<method-type>,
@@ -1182,7 +1184,7 @@ internal class RpcStubGenerator(
11821184

11831185
values {
11841186
// fullMethodName
1185-
+stringConst("${declaration.fqName}/${callable.name}")
1187+
+stringConst("${declaration.serviceFqName}/${callable.name}")
11861188

11871189
// requestCodec
11881190
+irCodec(requestType, resolver)

compiler-plugin/compiler-plugin-backend/src/main/kotlin/kotlinx/rpc/codegen/extension/ServiceDeclaration.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ class ServiceDeclaration(
1717
val service: IrClass,
1818
val stubClass: IrClass,
1919
val methods: List<Method>,
20+
val protoPackage: String,
2021
) {
2122
// todo change to extension after KRPC-178
2223
val isGrpc = service.hasAnnotation(RpcClassId.grpcAnnotation)
2324
val simpleName = service.kotlinFqName.shortName().asString()
2425
val fqName = service.kotlinFqName.asString()
2526

27+
// the name of the service based on the proto file (or @Grpc annotation)
28+
val serviceFqName = if (protoPackage.isNotEmpty()) "$protoPackage.$simpleName" else simpleName
2629
val serviceType = service.defaultType
2730

2831
sealed interface Callable {

compiler-plugin/compiler-plugin-k2/src/main/kotlin/kotlinx/rpc/codegen/checkers/FirRpcStrictModeClassChecker.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlinx.rpc.codegen.checkers.util.functionParametersRecursionCheck
1010
import kotlinx.rpc.codegen.common.RpcClassId
1111
import kotlinx.rpc.codegen.vsApi
1212
import org.jetbrains.kotlin.KtSourceElement
13+
import org.jetbrains.kotlin.descriptors.isAnnotationClass
1314
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
1415
import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0
1516
import org.jetbrains.kotlin.diagnostics.reportOn
@@ -26,7 +27,10 @@ object FirRpcStrictModeClassChecker {
2627
context: CheckerContext,
2728
reporter: DiagnosticReporter,
2829
) {
29-
if (!context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)) {
30+
if (!context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)
31+
// skip checks for annotation classes
32+
|| declaration.classKind.isAnnotationClass
33+
) {
3034
return
3135
}
3236

@@ -37,7 +41,10 @@ object FirRpcStrictModeClassChecker {
3741
}
3842

3943
is FirNamedFunctionSymbol -> {
40-
fun reportOn(element: KtSourceElement?, checker: FirRpcStrictModeDiagnostics.() -> KtDiagnosticFactory0) {
44+
fun reportOn(
45+
element: KtSourceElement?,
46+
checker: FirRpcStrictModeDiagnostics.() -> KtDiagnosticFactory0,
47+
) {
4148
reporter.reportOn(element, FirRpcStrictModeDiagnostics.checker(), context)
4249
}
4350

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/annotations/Grpc.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
package kotlinx.rpc.grpc.annotations
66

77
import kotlinx.rpc.annotations.Rpc
8-
import kotlinx.rpc.internal.utils.InternalRpcApi
98

109
/**
1110
* Annotation used for marking gRPC services.
1211
*/
1312
@Target(AnnotationTarget.CLASS, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.TYPE_PARAMETER)
1413
@Rpc
15-
public annotation class Grpc
14+
public annotation class Grpc(val protoPackage: String = "")

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ class ModelToGrpcKotlinCommonGenerator(
2828

2929
@Suppress("detekt.LongMethod")
3030
private fun CodeGenerator.generatePublicService(service: ServiceDeclaration) {
31+
val pkg = service.dec.file.`package`.orEmpty()
32+
val annotationParams = if (pkg.isNotEmpty()) """(protoPackage = "$pkg")""" else ""
33+
3134
clazz(
3235
name = service.name.simpleName,
3336
declarationType = CodeGenerator.DeclarationType.Interface,
34-
annotations = listOf("@kotlinx.rpc.grpc.annotations.Grpc")
37+
annotations = listOf("@kotlinx.rpc.grpc.annotations.Grpc$annotationParams")
3538
) {
3639
service.methods.forEach { method ->
3740
val inputType = method.inputType

tests/compiler-plugin-tests/src/main/kotlin/kotlinx/rpc/codegen/test/Grpc.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlin.reflect.KType
1818
* To check in methods:
1919
* ```
2020
* bareMethodName
21+
* fullMethodName
2122
* type
2223
* serviceName
2324
* isSafe
@@ -43,6 +44,7 @@ inline fun <@Grpc reified T : Any> grpcDelegate(): GrpcServiceDelegate {
4344

4445
fun MethodDescriptor<*, *>.checkMethod(
4546
expectedMethodName: String,
47+
expectedFullMethodName: String,
4648
expectedMethodType: MethodDescriptor.MethodType,
4749
expectedServiceName: String,
4850
expectedIsSafe: Boolean = true,
@@ -51,6 +53,7 @@ fun MethodDescriptor<*, *>.checkMethod(
5153
): String? {
5254
return when {
5355
bareMethodName != expectedMethodName -> "wrong bareMethodName: $bareMethodName"
56+
fullMethodName != expectedFullMethodName -> "wrong bareMethodName: $fullMethodName"
5457
type != expectedMethodType -> "wrong type: $type"
5558
serviceName != expectedServiceName -> "wrong service name: $fullMethodName"
5659
isSafe != expectedIsSafe -> "wrong isSafe: $isSafe"

0 commit comments

Comments
 (0)