Add comments in wrapper generator

This commit is contained in:
jangko 2024-01-12 08:33:29 +07:00
parent a6475e49b2
commit 97d19b9583
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 21 additions and 1 deletions

View File

@ -46,6 +46,17 @@ proc setupConversion(reqParams, params: NimNode): NimNode =
`reqParams`.positional.add encode(JrpcConv, `parName`).JsonString
proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimNode =
## This procedure will generate something like this:
## - Currently it always send posisitional parameters to the server
##
## proc rpcApi(client: RpcClient; paramA: TypeA; paramB: TypeB): Future[RetType] =
## {.gcsafe.}:
## var reqParams = RequestParamsTx(kind: rpPositional)
## reqParams.positional.add encode(JrpcConv, paramA).JsonString
## reqParams.positional.add encode(JrpcConv, paramB).JsonString
## let res = await client.call("rpcApi", reqParams)
## result = decode(JrpcConv, res.string, typeof RetType)
# Each input parameter in the rpc signature is converted
# to json using JrpcConv.encode.
# Return types are then converted back to native Nim types.

View File

@ -241,7 +241,13 @@ proc wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode
## rpcVar.paramA = params.unpack(paramA of ParamAType)
## rpcVar.paramB = params.unpack(paramB of ParamBType)
## else:
## rpcVar = params.unpack(named of RpcType)
## # missing parameters is ok in named mode
## # the default value will be used
## for x in params.named:
## case x.name
## of "paramA": rpcVar.paramA = params.unpack(paramA of ParamAType)
## of "paramB": rpcVar.paramB = params.unpack(paramB of ParamBType)
## else: discard
##
## let res = await rpcHandler(rpcVar.paramA, rpcVar.paramB)
## return JrpcConv.encode(res).JsonString
@ -285,6 +291,9 @@ proc wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode
else:
`named`
else:
# even though there is no parameters expected
# but the numbers of received params should
# still be checked (RPC spec)
setup.add quote do:
if `paramsIdent`.kind == rpPositional:
`posSetup`