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 (fix): Fix RPCMethod definition for Rx[A] return type #3832

Merged
merged 1 commit into from
Feb 13, 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 @@ -184,6 +184,17 @@ object HttpClientIR extends LogSupport {
)
.map(arg => HttpClientGenerator.fullTypeNameOf(arg))
.mkString(", ")

def rpcReturnElementType: Surface = {
val isRxResponse = returnType.rawType.isAssignableFrom(classOf[Rx[_]]) && returnType.typeArgs.size == 1
if (isRxResponse) {
// for methods returning Rx[A], extract A
returnType.typeArgs(0)
} else {
returnType
}
}

def clientMethodName = {
val methodName = httpMethod.toString.toLowerCase(Locale.ENGLISH)
if (isOpsRequest) s"${methodName}Ops"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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
import wvlet.airframe.surface.Surface

/**
* The default RPC client generator using Http.client.Sync/AsyncClient
Expand Down Expand Up @@ -71,7 +72,7 @@ object RPCClientGenerator extends HttpClientGenerator {
def rpcMethodDefs(svc: ClientServiceDef): String = {
svc.methods
.map { m =>
s"""lazy val __m_${m.name} = RPCMethod("${m.path}", "${svc.interfaceName}", "${m.name}", Surface.of[${m.requestModelClassType}], Surface.of[${m.returnType.fullTypeName}])"""
s"""lazy val __m_${m.name} = RPCMethod("${m.path}", "${svc.interfaceName}", "${m.name}", Surface.of[${m.requestModelClassType}], Surface.of[${m.rpcReturnElementType.fullTypeName}])"""
}.mkString("\n")
}

Expand Down Expand Up @@ -142,13 +143,7 @@ object RPCClientGenerator extends HttpClientGenerator {
m.inputParameters
.map(x => s"${x.name}: ${x.surface.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 returnElementType = m.rpcReturnElementType.fullTypeName
val returnType =
if (isAsync)
s"Rx[${returnElementType}]"
Expand Down
Loading