mirror of
https://github.com/codex-storage/nim-json-rpc.git
synced 2025-02-24 00:48:12 +00:00
Now allows return types in on macro
This commit is contained in:
parent
d96f984d5c
commit
7355543147
@ -125,14 +125,34 @@ macro on*(server: var RpcServer, path: string, body: untyped): untyped =
|
|||||||
var
|
var
|
||||||
setup = setupParams(parameters, paramsIdent)
|
setup = setupParams(parameters, paramsIdent)
|
||||||
procBody: NimNode
|
procBody: NimNode
|
||||||
|
bodyWrapper = newStmtList()
|
||||||
|
|
||||||
if body.kind == nnkStmtList: procBody = body
|
if body.kind == nnkStmtList: procBody = body
|
||||||
else: procBody = body.body
|
else: procBody = body.body
|
||||||
|
|
||||||
# wrapping async proc
|
if parameters.len > 0 and parameters[0] != nil:
|
||||||
|
# when a return type is specified, shadow async's result
|
||||||
|
# and pass it back jsonified
|
||||||
|
let
|
||||||
|
returnType = parameters[0]
|
||||||
|
res = ident"result"
|
||||||
|
template doMain(body: untyped): untyped =
|
||||||
|
# create a new scope to allow shadowing result
|
||||||
|
block:
|
||||||
|
body
|
||||||
|
bodyWrapper = quote do:
|
||||||
|
`res` = `doMain`:
|
||||||
|
var `res`: `returnType`
|
||||||
|
`procBody`
|
||||||
|
%`res`
|
||||||
|
else:
|
||||||
|
bodyWrapper = quote do: `procBody`
|
||||||
|
|
||||||
|
# async proc wrapper around body
|
||||||
result = quote do:
|
result = quote do:
|
||||||
proc `procName`*(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
proc `procName`*(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
||||||
`setup`
|
`setup`
|
||||||
`procBody`
|
`bodyWrapper`
|
||||||
`server`.register(`path`, `procName`)
|
`server`.register(`path`, `procName`)
|
||||||
when defined(nimDumpRpcs):
|
when defined(nimDumpRpcs):
|
||||||
echo "\n", pathStr, ": ", result.repr
|
echo "\n", pathStr, ": ", result.repr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user