rpcObject

expect inline fun <T : RpcService> rpcObject(): RpcObject<T>

Helper to get RpcObject for a given RpcService

Samples

import com.monkopedia.ksrpc.RpcService
import com.monkopedia.ksrpc.annotation.KsMethod
import com.monkopedia.ksrpc.annotation.KsService
import com.monkopedia.ksrpc.ksrpcEnvironment
import com.monkopedia.ksrpc.rpcObject
import com.monkopedia.ksrpc.serialized
import com.monkopedia.ksrpc.toStub
import kotlinx.serialization.Serializable

fun main() { 
   //sampleStart 
   // Define a service interface with @KsService and @KsMethod annotations.
// The compiler plugin generates a companion RpcObject and stub automatically.
val rpcObj = rpcObject<GreetingService>()
val endpoint = rpcObj.findEndpoint("greet") 
   //sampleEnd
}
import com.monkopedia.ksrpc.RpcService
import com.monkopedia.ksrpc.annotation.KsMethod
import com.monkopedia.ksrpc.annotation.KsService
import com.monkopedia.ksrpc.ksrpcEnvironment
import com.monkopedia.ksrpc.rpcObject
import com.monkopedia.ksrpc.serialized
import com.monkopedia.ksrpc.toStub
import kotlinx.serialization.Serializable

fun main() { 
   //sampleStart 
   // Any @Serializable class can be used as input or output to @KsMethod.
// The compiler plugin handles serializer plumbing automatically.
val rpcObj = rpcObject<UserService>()
val endpoint = rpcObj.findEndpoint("create") 
   //sampleEnd
}
actual inline fun <T : RpcService> rpcObject(): RpcObject<T>
actual inline fun <T : RpcService> rpcObject(): RpcObject<T>

Resolve the RpcObject for T.

Resolution order:

  1. If T::class has a companion that is an RpcObject, return it directly.

  2. If T::class has a companion that is an RpcObjectFactory, use the type arguments from typeOf<T>() to build the concrete RpcObject.

  3. Otherwise walk the supertypes of T (preserving type arguments) and return the first supertype whose classifier's companion is an RpcObject or an RpcObjectFactory. For a factory, the type arguments used are taken from THAT supertype's kotlin.reflect.KType (so e.g. interface TypedStream : KsStream<Info> correctly supplies <Info> to the parent's factory even though TypedStream itself has no type parameters).

actual inline fun <T : RpcService> rpcObject(): RpcObject<T>
actual inline fun <T : RpcService> rpcObject(): RpcObject<T>