Skip to content

Commit 0b0ec6c

Browse files
Jozott00Mr3zee
authored andcommitted
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 e42bb43 commit 0b0ec6c

File tree

22 files changed

+700
-180
lines changed

22 files changed

+700
-180
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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ private object Stub {
8787

8888
private object Descriptor {
8989
const val CALLABLES = "callables"
90+
91+
// the kotlin class name
9092
const val FQ_NAME = "fqName"
9193
const val SIMPLE_NAME = "simpleName"
9294
const val GET_CALLABLE = "getCallable"
@@ -1097,7 +1099,7 @@ internal class RpcStubGenerator(
10971099
*
10981100
* ```kotlin
10991101
* serviceDescriptor(
1100-
* name = MyService, // simpleName
1102+
* name = my.proto.package.MyService, // $declaration.serviceFqName
11011103
* methods = methodDescriptorMap.values, // Collection<MethodDescriptor<*, *>>
11021104
* schemaDescriptor = null, // for now, only null
11031105
* )
@@ -1116,7 +1118,7 @@ internal class RpcStubGenerator(
11161118
}.apply {
11171119
arguments {
11181120
values {
1119-
+stringConst(declaration.simpleName)
1121+
+stringConst(declaration.serviceFqName)
11201122

11211123
+irCallProperty(
11221124
receiver = IrGetValueImpl(
@@ -1141,7 +1143,7 @@ internal class RpcStubGenerator(
11411143
* // In scope: resolver: MessageCodecResolver
11421144
*
11431145
* methodDescriptor<<request-type>, <response-type>>(
1144-
* fullMethodName = "${descriptor.simpleName}/${callable.name}",
1146+
* fullMethodName = "${descriptor.serviceFqName}/${callable.name}",
11451147
* requestCodec = <request-codec>,
11461148
* responseCodec = <response-codec>,
11471149
* type = MethodType.<method-type>,
@@ -1196,7 +1198,7 @@ internal class RpcStubGenerator(
11961198

11971199
values {
11981200
// fullMethodName
1199-
+stringConst("${declaration.simpleName}/${callable.name}")
1201+
+stringConst("${declaration.serviceFqName}/${callable.name}")
12001202

12011203
// requestCodec
12021204
+irCodec(requestType, resolver)
@@ -1256,7 +1258,7 @@ internal class RpcStubGenerator(
12561258
val protobufMessage = owner.getAnnotation(ctx.withCodecAnnotation.owner.kotlinFqName)
12571259

12581260
return if (protobufMessage != null) {
1259-
val classReference = vsApi{ protobufMessage.argumentsVS }.single() as? IrClassReference
1261+
val classReference = vsApi { protobufMessage.argumentsVS }.single() as? IrClassReference
12601262
?: error("Expected IrClassReference for ${ctx.withCodecAnnotation.owner.kotlinFqName} parameter")
12611263

12621264
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)