Skip to content

Commit 8ac469e

Browse files
authored
grpc: Fix service names in the generated descriptor (#457)
* grpc: Fix the service name to be full-qualified Signed-off-by: Johannes Zottele <[email protected]> * grpc: Move CoreClientTest to commonTest and provide service server Signed-off-by: Johannes Zottele <[email protected]> * grpc: Add test to check if the service and client actually use the `package` option for the service name. Signed-off-by: Johannes Zottele <[email protected]> * grpc: Separate between kotlin FQ name and service (grpc) FQ name Signed-off-by: Johannes Zottele <[email protected]> * grpc: Address PR comments Signed-off-by: Johannes Zottele <[email protected]> * grpc: Avoid delay in this case Signed-off-by: Johannes Zottele <[email protected]> --------- Signed-off-by: Johannes Zottele <[email protected]>
1 parent 365872a commit 8ac469e

File tree

22 files changed

+703
-186
lines changed

22 files changed

+703
-186
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
@@ -4,14 +4,19 @@
44

55
package kotlinx.rpc.codegen.extension
66

7+
import kotlinx.rpc.codegen.common.RpcClassId
78
import kotlinx.rpc.codegen.common.RpcNames
89
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
910
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
1011
import org.jetbrains.kotlin.ir.declarations.IrClass
1112
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
1213
import org.jetbrains.kotlin.ir.declarations.IrProperty
1314
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
15+
import org.jetbrains.kotlin.ir.expressions.IrConst
16+
import org.jetbrains.kotlin.ir.expressions.IrConstKind
17+
import org.jetbrains.kotlin.ir.expressions.IrExpression
1418
import org.jetbrains.kotlin.ir.util.dumpKotlinLike
19+
import org.jetbrains.kotlin.ir.util.getAnnotation
1520
import org.jetbrains.kotlin.ir.util.hasDefaultValue
1621

1722
/**
@@ -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(RpcClassId.grpcAnnotation.asSingleFqName())
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/RpcStubGenerator.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.*
1818
import org.jetbrains.kotlin.ir.declarations.*
1919
import org.jetbrains.kotlin.ir.expressions.*
2020
import org.jetbrains.kotlin.ir.expressions.impl.*
21-
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
22-
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
23-
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
24-
import org.jetbrains.kotlin.ir.symbols.IrSymbol
25-
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
21+
import org.jetbrains.kotlin.ir.symbols.*
2622
import org.jetbrains.kotlin.ir.types.*
2723
import org.jetbrains.kotlin.ir.util.*
2824
import org.jetbrains.kotlin.name.Name
@@ -39,6 +35,8 @@ private object Stub {
3935
private object Descriptor {
4036
const val CALLABLE_MAP = "callableMap"
4137
const val CALLABLES = "callables"
38+
39+
// the kotlin class name
4240
const val FQ_NAME = "fqName"
4341
const val SIMPLE_NAME = "simpleName"
4442
const val GET_CALLABLE = "getCallable"
@@ -915,7 +913,8 @@ internal class RpcStubGenerator(
915913
}.apply {
916914
overriddenSymbols = listOf(ctx.properties.rpcServiceDescriptorCallables)
917915

918-
val collectionType = ctx.irBuiltIns.collectionClass.typeWith(ctx.rpcCallable.typeWith(declaration.serviceType))
916+
val collectionType =
917+
ctx.irBuiltIns.collectionClass.typeWith(ctx.rpcCallable.typeWith(declaration.serviceType))
919918

920919
addGetter {
921920
returnType = collectionType
@@ -1086,7 +1085,7 @@ internal class RpcStubGenerator(
10861085
*
10871086
* ```kotlin
10881087
* serviceDescriptor(
1089-
* name = MyService, // simpleName
1088+
* name = my.proto.package.MyService, // $declaration.serviceFqName
10901089
* methods = methodDescriptorMap.values, // Collection<MethodDescriptor<*, *>>
10911090
* schemaDescriptor = null, // for now, only null
10921091
* )
@@ -1105,7 +1104,7 @@ internal class RpcStubGenerator(
11051104
}.apply {
11061105
arguments {
11071106
values {
1108-
+stringConst(declaration.simpleName)
1107+
+stringConst(declaration.serviceFqName)
11091108

11101109
+irCallProperty(
11111110
receiver = IrGetValueImpl(
@@ -1130,7 +1129,7 @@ internal class RpcStubGenerator(
11301129
* // In scope: resolver: MessageCodecResolver
11311130
*
11321131
* methodDescriptor<<request-type>, <response-type>>(
1133-
* fullMethodName = "${descriptor.simpleName}/${callable.name}",
1132+
* fullMethodName = "${descriptor.serviceFqName}/${callable.name}",
11341133
* requestCodec = <request-codec>,
11351134
* responseCodec = <response-codec>,
11361135
* type = MethodType.<method-type>,
@@ -1185,7 +1184,7 @@ internal class RpcStubGenerator(
11851184

11861185
values {
11871186
// fullMethodName
1188-
+stringConst("${declaration.simpleName}/${callable.name}")
1187+
+stringConst("${declaration.serviceFqName}/${callable.name}")
11891188

11901189
// requestCodec
11911190
+irCodec(requestType, resolver)
@@ -1245,7 +1244,7 @@ internal class RpcStubGenerator(
12451244
val protobufMessage = owner.getAnnotation(ctx.withCodecAnnotation.owner.kotlinFqName)
12461245

12471246
return if (protobufMessage != null) {
1248-
val classReference = vsApi{ protobufMessage.argumentsVS }.single() as? IrClassReference
1247+
val classReference = vsApi { protobufMessage.argumentsVS }.single() as? IrClassReference
12491248
?: error("Expected IrClassReference for ${ctx.withCodecAnnotation.owner.kotlinFqName} parameter")
12501249

12511250
val codec = classReference.classType

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/Status.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public expect class Status {
3232

3333
public expect fun Status(code: StatusCode, description: String? = null, cause: Throwable? = null): Status
3434

35-
public expect val Status.code: StatusCode
35+
public expect val Status.statusCode: StatusCode
3636

3737
public enum class StatusCode(public val value: Int) {
3838
OK(0),

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 = "")

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/suspendClientCalls.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private fun <Response> channelResponseListener(
201201
},
202202
onClose = { status: Status, trailers: GrpcTrailers ->
203203
val cause = when {
204-
status.code == StatusCode.OK -> null
204+
status.statusCode == StatusCode.OK -> null
205205
status.getCause() is CancellationException -> status.getCause()
206206
else -> StatusException(status, trailers)
207207
}

0 commit comments

Comments
 (0)