Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

package kotlinx.rpc.codegen.extension

import kotlinx.rpc.codegen.common.RpcClassId
import kotlinx.rpc.codegen.common.RpcNames
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.util.dumpKotlinLike
import org.jetbrains.kotlin.ir.util.getAnnotation
import org.jetbrains.kotlin.ir.util.hasDefaultValue

/**
Expand All @@ -25,6 +30,9 @@ internal object RpcDeclarationScanner {
fun scanServiceDeclaration(service: IrClass, ctx: RpcIrContext, logger: MessageCollector): ServiceDeclaration {
var stubClass: IrClass? = null

val grpcAnnotation = service.getAnnotation(RpcClassId.grpcAnnotation.asSingleFqName())
val protoPackage = grpcAnnotation?.arguments?.getOrNull(0)?.asConstString() ?: ""

val declarations = service.declarations.memoryOptimizedMap { declaration ->
when (declaration) {
is IrSimpleFunction -> {
Expand Down Expand Up @@ -75,6 +83,7 @@ internal object RpcDeclarationScanner {
service = service,
stubClass = stubClassNotNull,
methods = declarations.filterNotNull(),
protoPackage = protoPackage.trim()
)
}
}
Expand All @@ -87,3 +96,7 @@ private fun unsupportedDeclaration(service: IrClass, declaration: IrDeclaration,

return null
}

fun IrExpression.asConstString(): String =
(this as? IrConst)?.takeIf { it.kind == IrConstKind.String }?.value as? String
?: error("Expected IrConst of kind String, got ${dumpKotlinLike()}")
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.Name
Expand All @@ -39,6 +35,8 @@ private object Stub {
private object Descriptor {
const val CALLABLE_MAP = "callableMap"
const val CALLABLES = "callables"

// the kotlin class name
const val FQ_NAME = "fqName"
const val SIMPLE_NAME = "simpleName"
const val GET_CALLABLE = "getCallable"
Expand Down Expand Up @@ -915,7 +913,8 @@ internal class RpcStubGenerator(
}.apply {
overriddenSymbols = listOf(ctx.properties.rpcServiceDescriptorCallables)

val collectionType = ctx.irBuiltIns.collectionClass.typeWith(ctx.rpcCallable.typeWith(declaration.serviceType))
val collectionType =
ctx.irBuiltIns.collectionClass.typeWith(ctx.rpcCallable.typeWith(declaration.serviceType))

addGetter {
returnType = collectionType
Expand Down Expand Up @@ -1086,7 +1085,7 @@ internal class RpcStubGenerator(
*
* ```kotlin
* serviceDescriptor(
* name = MyService, // simpleName
* name = my.proto.package.MyService, // $declaration.serviceFqName
* methods = methodDescriptorMap.values, // Collection<MethodDescriptor<*, *>>
* schemaDescriptor = null, // for now, only null
* )
Expand All @@ -1105,7 +1104,7 @@ internal class RpcStubGenerator(
}.apply {
arguments {
values {
+stringConst(declaration.simpleName)
+stringConst(declaration.serviceFqName)

+irCallProperty(
receiver = IrGetValueImpl(
Expand All @@ -1130,7 +1129,7 @@ internal class RpcStubGenerator(
* // In scope: resolver: MessageCodecResolver
*
* methodDescriptor<<request-type>, <response-type>>(
* fullMethodName = "${descriptor.simpleName}/${callable.name}",
* fullMethodName = "${descriptor.serviceFqName}/${callable.name}",
* requestCodec = <request-codec>,
* responseCodec = <response-codec>,
* type = MethodType.<method-type>,
Expand Down Expand Up @@ -1185,7 +1184,7 @@ internal class RpcStubGenerator(

values {
// fullMethodName
+stringConst("${declaration.simpleName}/${callable.name}")
+stringConst("${declaration.serviceFqName}/${callable.name}")

// requestCodec
+irCodec(requestType, resolver)
Expand Down Expand Up @@ -1245,7 +1244,7 @@ internal class RpcStubGenerator(
val protobufMessage = owner.getAnnotation(ctx.withCodecAnnotation.owner.kotlinFqName)

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

val codec = classReference.classType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ class ServiceDeclaration(
val service: IrClass,
val stubClass: IrClass,
val methods: List<Method>,
val protoPackage: String,
) {
// todo change to extension after KRPC-178
val isGrpc = service.hasAnnotation(RpcClassId.grpcAnnotation)
val simpleName = service.kotlinFqName.shortName().asString()
val fqName = service.kotlinFqName.asString()

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

sealed interface Callable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.rpc.codegen.checkers.util.functionParametersRecursionCheck
import kotlinx.rpc.codegen.common.RpcClassId
import kotlinx.rpc.codegen.vsApi
import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.descriptors.isAnnotationClass
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0
import org.jetbrains.kotlin.diagnostics.reportOn
Expand All @@ -26,7 +27,10 @@ object FirRpcStrictModeClassChecker {
context: CheckerContext,
reporter: DiagnosticReporter,
) {
if (!context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)) {
if (!context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)
// skip checks for annotation classes
|| declaration.classKind.isAnnotationClass
) {
return
}

Expand All @@ -37,7 +41,10 @@ object FirRpcStrictModeClassChecker {
}

is FirNamedFunctionSymbol -> {
fun reportOn(element: KtSourceElement?, checker: FirRpcStrictModeDiagnostics.() -> KtDiagnosticFactory0) {
fun reportOn(
element: KtSourceElement?,
checker: FirRpcStrictModeDiagnostics.() -> KtDiagnosticFactory0,
) {
reporter.reportOn(element, FirRpcStrictModeDiagnostics.checker(), context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public expect class Status {

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

public expect val Status.code: StatusCode
public expect val Status.statusCode: StatusCode

public enum class StatusCode(public val value: Int) {
OK(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
package kotlinx.rpc.grpc.annotations

import kotlinx.rpc.annotations.Rpc
import kotlinx.rpc.internal.utils.InternalRpcApi

/**
* Annotation used for marking gRPC services.
*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.TYPE_PARAMETER)
@Rpc
public annotation class Grpc
public annotation class Grpc(val protoPackage: String = "")
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private fun <Response> channelResponseListener(
},
onClose = { status: Status, trailers: GrpcTrailers ->
val cause = when {
status.code == StatusCode.OK -> null
status.statusCode == StatusCode.OK -> null
status.getCause() is CancellationException -> status.getCause()
else -> StatusException(status, trailers)
}
Expand Down
Loading
Loading