Skip to content

Commit

Permalink
work around Nim 2.0.10 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Oct 4, 2024
1 parent 9da5b60 commit c559054
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions json_rpc/private/server_handler_wrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ template rpc_isOptional[T](_: options.Option[T]): bool = true
# Run time helpers
# ------------------------------------------------------------------------------

proc unpackArg(args: JsonString, argName: string, argType: type): argType
func unpackArg(args: JsonString, argName: string, argType: type): argType
{.gcsafe, raises: [JsonRpcError].} =
## This where input parameters are decoded from JSON into
## Nim data types
Expand Down Expand Up @@ -89,12 +89,25 @@ template expectOptionalParamsLen(params: RequestParamsRx,

template expectParamsLen(params: RequestParamsRx, length: static[int]) =
## Make sure positional params meets the handler expectation
let
expected = "Expected " & $length & " Json parameter(s) but got "

if params.positional.len != length:
raise newException(RequestDecodeError,
expected & $params.positional.len)
when (NimMajor, NimMinor) < (2, 2):
# https://github.com/nim-lang/Nim/issues/24228 in Nim 2.0.10 (not 2.0.8 and
# not 2.2.x) means that `expectParamsLen` in a Chronos async context, along
# with `{.compileTime.}` `$` overloads for `int`, `uint64`, and `int64`, at
# https://github.com/nim-lang/Nim/blob/v2.0.10/lib/system/dollars.nim#L28-L34
# can fail to compile. Explicitly choose the non-CTFE overloads which don't
# trigger this bug.
#
# This can be removed once Nim v2.0.10 isn't supported by nim-json-rpc.
let nonConstLength = length

let expected = "Expected " & $nonConstLength & " JSON parameter(s) but got "

if params.positional.len != length:
raise newException(RequestDecodeError,
expected & $params.positional.len)
else:
let expected = "Expected " & $length & " JSON parameter(s) but got "

template setupPositional(setup: static[RpcSetup], params: RequestParamsRx) =
## Generate code to check positional params length
Expand Down Expand Up @@ -141,11 +154,11 @@ template unpackPositional(params: RequestParamsRx,
else:
# mandatory args
# A and C fall into this category
# unpack Nim type and assign from json
# unpack Nim type and assign from JSON
if params.notNull(pos):
innerNode()

proc makeType(typeName, params: NimNode): NimNode =
func makeType(typeName, params: NimNode): NimNode =
## Generate type section contains an object definition
## with fields of handler params
let typeSec = quote do:
Expand All @@ -159,14 +172,14 @@ proc makeType(typeName, params: NimNode): NimNode =
obj[2] = recList
typeSec

proc makeParams(retType: NimNode, params: NimNode): seq[NimNode] =
func makeParams(retType: NimNode, params: NimNode): seq[NimNode] =
## Convert rpc params into handler params
result.add retType
if params.len > 1:
for i in 1..<params.len:
result.add params[i]

proc makeHandler(procName, params, procBody, returnInner: NimNode): NimNode =
func makeHandler(procName, params, procBody, returnInner: NimNode): NimNode =
## Generate rpc handler proc
let
returnType = quote do: Future[`returnInner`]
Expand All @@ -180,15 +193,15 @@ proc makeHandler(procName, params, procBody, returnInner: NimNode): NimNode =
pragmas = pragmas
)

proc ofStmt(x, paramsObj, paramName, paramType: NimNode): NimNode =
func ofStmt(x, paramsObj, paramName, paramType: NimNode): NimNode =
let caseStr = $paramName
result = nnkOfBranch.newTree(
quote do: `caseStr`,
quote do:
`paramsObj`.`paramName` = unpackArg(`x`.value, `caseStr`, `paramType`)
)

proc setupNamed(paramsObj, paramsIdent, params: NimNode): NimNode =
func setupNamed(paramsObj, paramsIdent, params: NimNode): NimNode =
let x = ident"x"

var caseStmt = nnkCaseStmt.newTree(
Expand All @@ -206,7 +219,7 @@ proc setupNamed(paramsObj, paramsIdent, params: NimNode): NimNode =
for `x` in `paramsIdent`.named:
`caseStmt`

proc wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode): NimNode =
func wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode): NimNode =
## This proc generate something like this:
##
## proc rpcHandler(paramA: ParamAType, paramB: ParamBType): Future[ReturnType] =
Expand Down

0 comments on commit c559054

Please sign in to comment.