From 76dd352ace689fbcf306ba92a63bc64811ac9b68 Mon Sep 17 00:00:00 2001 From: jangko Date: Thu, 4 Jan 2024 07:49:19 +0700 Subject: [PATCH] Remove StringOfJson --- README.md | 6 +++--- json_rpc/client.nim | 8 ++++---- json_rpc/clients/httpclient.nim | 10 ++++++++-- json_rpc/clients/socketclient.nim | 4 ++-- json_rpc/clients/websocketclientimpl.nim | 4 ++-- json_rpc/private/client_handler_wrapper.nim | 4 ++-- json_rpc/private/jrpc_conv.nim | 3 --- json_rpc/private/server_handler_wrapper.nim | 8 ++++---- json_rpc/private/shared_wrapper.nim | 2 +- json_rpc/router.nim | 16 ++++++++-------- json_rpc/rpcproxy.nim | 2 +- json_rpc/server.nim | 4 ++-- tests/private/helpers.nim | 6 +++--- tests/test_router_rpc.nim | 1 - tests/testethcalls.nim | 4 ++-- 15 files changed, 42 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index d1ff4ce..3c1127a 100644 --- a/README.md +++ b/README.md @@ -241,9 +241,9 @@ This `route` variant allows you to invoke a call if possible, without raising an `router: RpcRouter`: The router object that contains the RPCs. -`data: StringOfJson`: A raw `Json document` that matches the expected format as defined above. +`data: JsonString`: A raw `Json document` that matches the expected format as defined above. -`fut: var Future[StringOfJson]`: The stringified JSON RPC result or a JSON wrapped error. +`fut: var Future[JsonString]`: The stringified JSON RPC result or a JSON wrapped error. #### Returns @@ -397,7 +397,7 @@ Additionally, the following two procedures are useful: `name: string`: the method to be called `params: JsonNode`: The parameters to the RPC call Returning - `Future[StringOfJson]`: A wrapper for the result `Json document` and a flag to indicate if this contains an error. + `Future[JsonString]`: A wrapper for the result `Json document` and a flag to indicate if this contains an error. Note: Although `call` isn't necessary for a client to function, it allows RPC signatures to be used by the `createRpcSigs`. diff --git a/json_rpc/client.nim b/json_rpc/client.nim index 75d361c..e1cff0a 100644 --- a/json_rpc/client.nim +++ b/json_rpc/client.nim @@ -28,7 +28,7 @@ export type RpcClient* = ref object of RootRef - awaiting*: Table[RequestId, Future[StringOfJson]] + awaiting*: Table[RequestId, Future[JsonString]] lastId: int onDisconnect*: proc() {.gcsafe, raises: [].} @@ -53,12 +53,12 @@ proc getNextId*(client: RpcClient): RequestId = RequestId(kind: riNumber, num: client.lastId) method call*(client: RpcClient, name: string, - params: RequestParamsTx): Future[StringOfJson] + params: RequestParamsTx): Future[JsonString] {.base, gcsafe, async.} = doAssert(false, "`RpcClient.call` not implemented") method call*(client: RpcClient, name: string, - params: JsonNode): Future[StringOfJson] + params: JsonNode): Future[JsonString] {.base, gcsafe, async.} = await client.call(name, params.paramsTx) @@ -78,7 +78,7 @@ proc processMessage*(client: RpcClient, line: string): Result[void, string] = if response.id.isNone: return err("missing or invalid response id") - var requestFut: Future[StringOfJson] + var requestFut: Future[JsonString] let id = response.id.get if not client.awaiting.pop(id, requestFut): return err("Cannot find message id \"" & $id & "\"") diff --git a/json_rpc/clients/httpclient.nim b/json_rpc/clients/httpclient.nim index 4ca646f..097375c 100644 --- a/json_rpc/clients/httpclient.nim +++ b/json_rpc/clients/httpclient.nim @@ -54,7 +54,7 @@ proc newRpcHttpClient*( RpcHttpClient.new(maxBodySize, secure, getHeaders, flags) method call*(client: RpcHttpClient, name: string, - params: RequestParamsTx): Future[StringOfJson] + params: RequestParamsTx): Future[JsonString] {.async, gcsafe.} = doAssert client.httpSession != nil if client.httpAddress.isErr: @@ -74,6 +74,10 @@ method call*(client: RpcHttpClient, name: string, var req: HttpClientRequestRef var res: HttpClientResponseRef + template used(x: typed) = + # silence unused warning + discard + template closeRefs() = # We can't trust try/finally in async/await in all nim versions, so we # do it manually instead @@ -81,11 +85,13 @@ method call*(client: RpcHttpClient, name: string, try: await req.closeWait() except CatchableError as exc: # shouldn't happen + used(exc) debug "Error closing JSON-RPC HTTP resuest/response", err = exc.msg if res != nil: try: await res.closeWait() except CatchableError as exc: # shouldn't happen + used(exc) debug "Error closing JSON-RPC HTTP resuest/response", err = exc.msg debug "Sending message to RPC server", @@ -131,7 +137,7 @@ method call*(client: RpcHttpClient, name: string, # completed by processMessage - the flow is quite weird here to accomodate # socket and ws clients, but could use a more thorough refactoring - var newFut = newFuture[StringOfJson]() + var newFut = newFuture[JsonString]() # add to awaiting responses client.awaiting[id] = newFut diff --git a/json_rpc/clients/socketclient.nim b/json_rpc/clients/socketclient.nim index 97d85be..dee5a45 100644 --- a/json_rpc/clients/socketclient.nim +++ b/json_rpc/clients/socketclient.nim @@ -36,7 +36,7 @@ proc newRpcSocketClient*: RpcSocketClient = RpcSocketClient.new() method call*(self: RpcSocketClient, name: string, - params: RequestParamsTx): Future[StringOfJson] {.async, gcsafe.} = + params: RequestParamsTx): Future[JsonString] {.async, gcsafe.} = ## Remotely calls the specified RPC method. let id = self.getNextId() var value = requestTxEncode(name, params, id) & "\r\n" @@ -45,7 +45,7 @@ method call*(self: RpcSocketClient, name: string, "Transport is not initialised (missing a call to connect?)") # completed by processMessage. - var newFut = newFuture[StringOfJson]() + var newFut = newFuture[JsonString]() # add to awaiting responses self.awaiting[id] = newFut diff --git a/json_rpc/clients/websocketclientimpl.nim b/json_rpc/clients/websocketclientimpl.nim index 13b3d19..67c7598 100644 --- a/json_rpc/clients/websocketclientimpl.nim +++ b/json_rpc/clients/websocketclientimpl.nim @@ -39,7 +39,7 @@ proc newRpcWebSocketClient*( RpcWebSocketClient.new(getHeaders) method call*(self: RpcWebSocketClient, name: string, - params: RequestParamsTx): Future[StringOfJson] {.async, gcsafe.} = + params: RequestParamsTx): Future[JsonString] {.async, gcsafe.} = ## Remotely calls the specified RPC method. let id = self.getNextId() var value = requestTxEncode(name, params, id) & "\r\n" @@ -48,7 +48,7 @@ method call*(self: RpcWebSocketClient, name: string, "Transport is not initialised (missing a call to connect?)") # completed by processMessage. - var newFut = newFuture[StringOfJson]() + var newFut = newFuture[JsonString]() # add to awaiting responses self.awaiting[id] = newFut diff --git a/json_rpc/private/client_handler_wrapper.nim b/json_rpc/private/client_handler_wrapper.nim index caddbd6..f856c9b 100644 --- a/json_rpc/private/client_handler_wrapper.nim +++ b/json_rpc/private/client_handler_wrapper.nim @@ -42,7 +42,7 @@ proc setupConversion(reqParams, params: NimNode): NimNode = for parName, parType in paramsIter(params): result.add quote do: - `reqParams`.positional.add encode(JrpcConv, `parName`).StringOfJson + `reqParams`.positional.add encode(JrpcConv, `parName`).JsonString proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimNode = # Each input parameter in the rpc signature is converted @@ -83,7 +83,7 @@ proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimN # populate request params `setup` - # `rpcResult` is of type `StringOfJson` + # `rpcResult` is of type `JsonString` let `rpcResult` = await `clientIdent`.call(`pathStr`, `reqParams`) `maybeWrap` diff --git a/json_rpc/private/jrpc_conv.nim b/json_rpc/private/jrpc_conv.nim index dda589c..cf6acc8 100644 --- a/json_rpc/private/jrpc_conv.nim +++ b/json_rpc/private/jrpc_conv.nim @@ -13,9 +13,6 @@ import export json_serialization -type - StringOfJson* = JsonString - createJsonFlavor JrpcConv, requireAllFields = false diff --git a/json_rpc/private/server_handler_wrapper.nim b/json_rpc/private/server_handler_wrapper.nim index d7116be..c58f981 100644 --- a/json_rpc/private/server_handler_wrapper.nim +++ b/json_rpc/private/server_handler_wrapper.nim @@ -220,7 +220,7 @@ proc wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode ## procBody ## return retVal ## - ## proc rpcWrapper(params: RequestParamsRx): Future[StringOfJson] = + ## proc rpcWrapper(params: RequestParamsRx): Future[JsonString] = ## type ## RpcType = object ## paramA: ParamAType @@ -237,7 +237,7 @@ proc wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode ## rpcVar = params.unpack(named of RpcType) ## ## let res = await rpcHandler(rpcVar.paramA, rpcVar.paramB) - ## return JrpcConv.encode(res).StringOfJson + ## return JrpcConv.encode(res).JsonString let params = params.ensureReturnType() @@ -287,13 +287,13 @@ proc wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode doEncode = quote do: encode(JrpcConv, `awaitedResult`) maybeWrap = if returnType.noWrap: awaitedResult - else: ident"StringOfJson".newCall doEncode + else: ident"JsonString".newCall doEncode executeCall = newCall(handlerName, executeParams) result = newStmtList() result.add handler result.add quote do: - proc `procWrapper`(`paramsIdent`: RequestParamsRx): Future[StringOfJson] {.async, gcsafe.} = + proc `procWrapper`(`paramsIdent`: RequestParamsRx): Future[JsonString] {.async, gcsafe.} = # Avoid 'yield in expr not lowered' with an intermediate variable. # See: https://github.com/nim-lang/Nim/issues/17849 `setup` diff --git a/json_rpc/private/shared_wrapper.nim b/json_rpc/private/shared_wrapper.nim index 43ad61a..cf07049 100644 --- a/json_rpc/private/shared_wrapper.nim +++ b/json_rpc/private/shared_wrapper.nim @@ -33,7 +33,7 @@ func ensureReturnType*(params: NimNode): NimNode = func noWrap*(returnType: NimNode): bool = ## Condition when return type should not be encoded ## to Json - returnType.repr == "StringOfJson" or + returnType.repr == "JsonString" or returnType.repr == "JsonString" func paramsTx*(params: JsonNode): RequestParamsTx = diff --git a/json_rpc/router.nim b/json_rpc/router.nim index 2c24faf..caed7b6 100644 --- a/json_rpc/router.nim +++ b/json_rpc/router.nim @@ -22,7 +22,7 @@ export type # Procedure signature accepted as an RPC call by server - RpcProc* = proc(params: RequestParamsRx): Future[StringOfJson] + RpcProc* = proc(params: RequestParamsRx): Future[JsonString] {.gcsafe, raises: [CatchableError].} RpcRouter* = object @@ -51,7 +51,7 @@ func invalidRequest(msg: string): ResponseError = func methodNotFound(msg: string): ResponseError = ResponseError(code: METHOD_NOT_FOUND, message: msg) -func serverError(msg: string, data: StringOfJson): ResponseError = +func serverError(msg: string, data: JsonString): ResponseError = ResponseError(code: SERVER_ERROR, message: msg, data: Opt.some(data)) func somethingError(code: int, msg: string): ResponseError = @@ -92,7 +92,7 @@ proc wrapError(code: int, msg: string, id: RequestId): ResponseTx = error: somethingError(code, msg), ) -proc wrapReply(res: StringOfJson, id: RequestId): ResponseTx = +proc wrapReply(res: JsonString, id: RequestId): ResponseTx = ResponseTx( id: id, kind: rkResult, @@ -134,12 +134,12 @@ proc route*(router: RpcRouter, req: RequestRx): debug "Error occurred within RPC", methodName = methodName, err = err.msg return serverError(methodName & " raised an exception", - escapeJson(err.msg).StringOfJson). + escapeJson(err.msg).JsonString). wrapError(req.id) proc wrapErrorAsync*(code: int, msg: string): - Future[StringOfJson] {.gcsafe, async: (raises: []).} = - return wrapError(code, msg).StringOfJson + Future[JsonString] {.gcsafe, async: (raises: []).} = + return wrapError(code, msg).JsonString proc route*(router: RpcRouter, data: string): Future[string] {.gcsafe, async: (raises: []).} = @@ -172,8 +172,8 @@ proc route*(router: RpcRouter, data: string): return reply -proc tryRoute*(router: RpcRouter, data: StringOfJson, - fut: var Future[StringOfJson]): Result[void, string] = +proc tryRoute*(router: RpcRouter, data: JsonString, + fut: var Future[JsonString]): Result[void, string] = ## Route to RPC, returns false if the method or params cannot be found. ## Expects json input and returns json output. when defined(nimHasWarnBareExcept): diff --git a/json_rpc/rpcproxy.nim b/json_rpc/rpcproxy.nim index 47f4e10..c0df44d 100644 --- a/json_rpc/rpcproxy.nim +++ b/json_rpc/rpcproxy.nim @@ -54,7 +54,7 @@ proc getWebSocketClientConfig*( ClientConfig(kind: WebSocket, wsUri: uri, compression: compression, flags: flags) proc proxyCall(client: RpcClient, name: string): RpcProc = - return proc (params: RequestParamsRx): Future[StringOfJson] {.gcsafe, async.} = + return proc (params: RequestParamsRx): Future[JsonString] {.gcsafe, async.} = let res = await client.call(name, params.toTx) return res diff --git a/json_rpc/server.nim b/json_rpc/server.nim index 35c0ef2..a204051 100644 --- a/json_rpc/server.nim +++ b/json_rpc/server.nim @@ -46,7 +46,7 @@ template hasMethod*(server: RpcServer, methodName: string): bool = proc executeMethod*(server: RpcServer, methodName: string, - params: RequestParamsTx): Future[StringOfJson] + params: RequestParamsTx): Future[JsonString] {.gcsafe, raises: [JsonRpcError].} = let @@ -58,7 +58,7 @@ proc executeMethod*(server: RpcServer, proc executeMethod*(server: RpcServer, methodName: string, - args: JsonNode): Future[StringOfJson] + args: JsonNode): Future[JsonString] {.gcsafe, raises: [JsonRpcError].} = let params = paramsTx(args) diff --git a/tests/private/helpers.nim b/tests/private/helpers.nim index 9a61225..09eb815 100644 --- a/tests/private/helpers.nim +++ b/tests/private/helpers.nim @@ -10,10 +10,10 @@ import ../../json_rpc/router -converter toStr*(value: distinct (string|StringOfJson)): string = string(value) +converter toStr*(value: distinct (string|JsonString)): string = string(value) -template `==`*(a: StringOfJson, b: JsonNode): bool = +template `==`*(a: JsonString, b: JsonNode): bool = parseJson(string a) == b -template `==`*(a: JsonNode, b: StringOfJson): bool = +template `==`*(a: JsonNode, b: JsonString): bool = a == parseJson(string b) diff --git a/tests/test_router_rpc.nim b/tests/test_router_rpc.nim index 71824aa..0846efe 100644 --- a/tests/test_router_rpc.nim +++ b/tests/test_router_rpc.nim @@ -1,7 +1,6 @@ import unittest2, ../json_rpc/router, - json_serialization/stew/results, json_serialization/std/options var server = RpcRouter() diff --git a/tests/testethcalls.nim b/tests/testethcalls.nim index f736c65..5d37f85 100644 --- a/tests/testethcalls.nim +++ b/tests/testethcalls.nim @@ -43,7 +43,7 @@ server.rpc(rpcDynamicName "testReturnUint256") do() -> UInt256: let r: UInt256 = "0x1234567890abcdef".parse(UInt256, 16) return r -proc testLocalCalls: Future[seq[StringOfJson]] = +proc testLocalCalls: Future[seq[JsonString]] = ## Call RPCs created with `rpc` locally. ## This simply demonstrates async calls of the procs generated by the `rpc` macro. let @@ -51,7 +51,7 @@ proc testLocalCalls: Future[seq[StringOfJson]] = returnUint256 = server.executeMethod("rpc.testReturnUint256", %[]) return all(uint256Param, returnUint256) -proc testRemoteUInt256: Future[seq[StringOfJson]] = +proc testRemoteUInt256: Future[seq[JsonString]] = ## Call function remotely on server, testing `stint` types let uint256Param = client.call("rpc.uint256Param", %[%"0x1234567890"])