Skip to content

Commit 13b3968

Browse files
authored
Fixed probable name clashing issues in generated clients (#57)
1 parent 75fb1d2 commit 13b3968

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

ksp-plugin/src/main/kotlin/kotlinx/rpc/codegen/RPCClientServiceGenerator.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class RPCClientServiceGenerator(private val codegen: CodeGenerator) {
1515
private const val REGISTER_PLAIN_FLOW_FIELD_METHOD = "registerPlainFlowField"
1616
private const val REGISTER_SHARED_FLOW_FIELD_METHOD = "registerSharedFlowField"
1717
private const val REGISTER_STATE_FLOW_FIELD_METHOD = "registerStateFlowField"
18+
19+
private const val ID_PROPERTY_NAME = "__rpc_stub_id"
20+
private const val CLIENT_PROPERTY_NAME = "__rpc_client"
21+
private const val SCOPE_PROPERTY_NAME = "__rpc_scope"
1822
}
1923

2024
fun generate(service: RPCServiceDeclaration) {
@@ -49,21 +53,21 @@ class RPCClientServiceGenerator(private val codegen: CodeGenerator) {
4953
writer.write("class ${service.simpleName.withClientImplSuffix()}(")
5054
writer.newLine()
5155
with(writer.nested()) {
52-
write("override val id: Long,")
56+
write("private val $ID_PROPERTY_NAME: Long,")
5357
newLine()
54-
write("private val client: RPCClient,")
58+
write("private val $CLIENT_PROPERTY_NAME: RPCClient,")
5559
newLine()
5660
}
57-
writer.write(") : ${service.fullName}, RPCClientService {")
61+
writer.write(") : ${service.fullName} {")
5862
writer.newLine()
5963

6064
val nested = writer.nested()
6165

62-
nested.write("override val coroutineContext: CoroutineContext = client.provideStubContext(id)")
66+
nested.write("override val coroutineContext: CoroutineContext = $CLIENT_PROPERTY_NAME.provideStubContext($ID_PROPERTY_NAME)")
6367
nested.newLine()
6468
nested.newLine()
6569

66-
nested.write("private val scope: CoroutineScope = this")
70+
nested.write("private val $SCOPE_PROPERTY_NAME: CoroutineScope = this")
6771
nested.newLine()
6872
nested.newLine()
6973

@@ -105,7 +109,7 @@ class RPCClientServiceGenerator(private val codegen: CodeGenerator) {
105109
generateFunctionClass(writer)
106110

107111
val returnTypeGenerated = if (returnType.isUnit()) ": Unit" else ": ${returnType.toCode()}"
108-
writer.write("override suspend fun ${name}(${argumentTypes.joinToString { it.toCode() }})$returnTypeGenerated = scopedClientCall(scope, id) {")
112+
writer.write("override suspend fun ${name}(${argumentTypes.joinToString { it.toCode() }})$returnTypeGenerated = scopedClientCall($SCOPE_PROPERTY_NAME) {")
109113
writer.newLine()
110114
generateBody(serviceType, writer.nested())
111115
writer.write("}")
@@ -132,9 +136,9 @@ class RPCClientServiceGenerator(private val codegen: CodeGenerator) {
132136
else -> "by lazy {" to " }"
133137
}
134138

135-
val rpcFiled = "RPCField(\"$serviceType\", id, \"$name\", typeOf<$codeType>())"
139+
val rpcFiled = "RPCField(\"$serviceType\", $ID_PROPERTY_NAME, \"$name\", typeOf<$codeType>())"
136140

137-
val codeDeclaration = "override val $name: $codeType $prefix client.$method(scope, $rpcFiled)$suffix"
141+
val codeDeclaration = "override val $name: $codeType $prefix $CLIENT_PROPERTY_NAME.$method($SCOPE_PROPERTY_NAME, $rpcFiled)$suffix"
138142

139143
writer.write(codeDeclaration)
140144
writer.newLine()
@@ -182,8 +186,8 @@ class RPCClientServiceGenerator(private val codegen: CodeGenerator) {
182186
})"
183187
}"
184188

185-
val rpcCall = "RPCCall(\"$serviceType\", id, \"$name\", RPCCall.Type.Method, $data, dataType, returnType)"
186-
writer.write("client.call($rpcCall)")
189+
val rpcCall = "RPCCall(\"$serviceType\", $ID_PROPERTY_NAME, \"$name\", RPCCall.Type.Method, $data, dataType, returnType)"
190+
writer.write("$CLIENT_PROPERTY_NAME.call($rpcCall)")
187191
writer.newLine()
188192
}
189193

runtime/src/commonMain/kotlin/kotlinx/rpc/internal/RPCClientService.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

runtime/src/commonMain/kotlin/kotlinx/rpc/internal/scopedClientCall.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import kotlinx.coroutines.*
1414
@InternalRPCApi
1515
@OptIn(InternalCoroutinesApi::class)
1616
@Suppress("unused")
17-
public suspend inline fun <T> scopedClientCall(serviceScope: CoroutineScope, serviceId: Long, body: () -> T): T {
17+
public suspend inline fun <T> scopedClientCall(serviceScope: CoroutineScope, body: () -> T): T {
1818
val requestJob = currentCoroutineContext().job
1919
val handle = serviceScope.coroutineContext.job.invokeOnCompletion(onCancelling = true) {
2020
requestJob.cancel(it as CancellationException)

runtime/src/jvmTest/kotlin/kotlinx/rpc/test/api/WireSamplingTestScope.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import kotlinx.coroutines.test.TestScope
1313
import kotlinx.coroutines.test.runTest
1414
import kotlinx.rpc.*
1515
import kotlinx.rpc.client.withService
16-
import kotlinx.rpc.internal.RPCClientService
1716
import kotlinx.rpc.internal.hex.hexToByteArrayInternal
1817
import kotlinx.rpc.internal.hex.hexToReadableBinary
1918
import kotlinx.rpc.internal.logging.CommonLogger
@@ -244,7 +243,7 @@ private class WireToolkit(scope: CoroutineScope, format: SamplingFormat, val log
244243
val clazz = this::class.java
245244
val prop = clazz
246245
.declaredFields
247-
.single { it.name == RPCClientService::id.name }
246+
.single { it.name == "__rpc_stub_id" }
248247
.apply { isAccessible = true }
249248

250249
prop.set(this, 1L)

0 commit comments

Comments
 (0)