Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc (feature): Support RPC method returning Rx[A] #3829

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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)
Loading