Skip to content

Commit

Permalink
Add comments in wrapper generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jan 12, 2024
1 parent a6475e4 commit 97d19b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions json_rpc/private/client_handler_wrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion json_rpc/private/server_handler_wrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 97d19b9

Please sign in to comment.