Skip to content

Commit

Permalink
rpc (feature): Support RPC method returning Rx[A] (#3829)
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial authored Feb 12, 2025
1 parent 9477c34 commit 0ee8b7a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import wvlet.airframe.http.{Http, HttpMethod}
import wvlet.airframe.http.codegen.HttpClientIR
import wvlet.airframe.http.codegen.HttpClientIR.{ClientMethodDef, ClientServiceDef}
import wvlet.airframe.http.codegen.client.HttpClientGenerator.RichSurface
import wvlet.airframe.rx.Rx

/**
* The default RPC client generator using Http.client.Sync/AsyncClient
Expand Down Expand Up @@ -141,7 +142,19 @@ object RPCClientGenerator extends HttpClientGenerator {
m.inputParameters
.map(x => s"${x.name}: ${x.surface.fullTypeName}")

val returnType = if (isAsync) s"Rx[${m.returnType.fullTypeName}]" else m.returnType.fullTypeName
val isRxResponse = m.returnType.rawType.isAssignableFrom(classOf[Rx[_]]) && m.returnType.typeArgs.size == 1
val returnElementType = if (isRxResponse) {
// for methods returning Rx[A], extract A
m.returnType.typeArgs(0).fullTypeName
} else {
m.returnType.fullTypeName
}
val returnType =
if (isAsync)
s"Rx[${returnElementType}]"
else
returnElementType

if (m.isRPC) {
s"""def ${m.name}(${inputArgs.mkString(", ")}): ${returnType} = {
| client.rpc[${m.typeArgString}](${sendRequestArgs(m)})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package example.rpc
import wvlet.airframe.http.{RPC, RxRouter, RxRouterProvider}
import wvlet.airframe.rx.Rx

/**
*/
Expand Down Expand Up @@ -41,6 +42,8 @@ trait RPCExample {
def rpcWithOption(p1: Option[String]): Unit
def rpcWithPrimitiveAndOption(p1: String, p2: Option[String]): Unit
def rpcWithOptionOfComplexType(p1: Option[RPCRequest]): Unit

def rpcWithRxResponse(p1: Int): Rx[RPCResponse]
}

object RPCExample extends RxRouterProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package wvlet.airframe.test.api

import wvlet.airframe.http.*
import wvlet.airframe.rx.Rx

@RPC
trait HelloRPC:
Expand All @@ -23,6 +24,7 @@ trait HelloRPC:
def serverStatus: Status
def ackStatus(status: Status): Status
def variousParams(params: VariousParams): VariousParams
def ackStatusAsync(name: String): Rx[Status]

object HelloRPC extends RxRouterProvider:
override def router: RxRouter = RxRouter.of[HelloRPC]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package wvlet.airframe.test.api

import wvlet.airframe.rx.Rx
import wvlet.airframe.test.api.HelloRPC.VariousParams
import wvlet.log.LogSupport

Expand All @@ -28,3 +29,6 @@ class HelloRPCImpl extends HelloRPC with LogSupport:
override def variousParams(params: VariousParams): VariousParams =
info(s"received: ${params}")
params

override def ackStatusAsync(name: String): Rx[Status] =
Rx.const(Status.OK)

0 comments on commit 0ee8b7a

Please # to comment.