-
Notifications
You must be signed in to change notification settings - Fork 28
/
server_handler_wrapper.nim
319 lines (275 loc) · 10.1 KB
/
server_handler_wrapper.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# json-rpc
# Copyright (c) 2019-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
{.push raises: [], gcsafe.}
import
std/[macros, typetraits],
stew/[byteutils, objects],
json_serialization,
json_serialization/std/[options],
json_serialization/stew/results,
../errors,
./jrpc_sys,
./shared_wrapper,
../jsonmarshal
export
jsonmarshal
type
RpcSetup = object
numFields: int
numOptionals: int
minLength: int
# ------------------------------------------------------------------------------
# Optional resolvers
# ------------------------------------------------------------------------------
template rpc_isOptional(_: auto): bool = false
template rpc_isOptional[T](_: results.Opt[T]): bool = true
template rpc_isOptional[T](_: options.Option[T]): bool = true
# ------------------------------------------------------------------------------
# Run time helpers
# ------------------------------------------------------------------------------
func unpackArg(args: JsonString, argName: string, argType: type): argType
{.gcsafe, raises: [JsonRpcError].} =
## This where input parameters are decoded from JSON into
## Nim data types
try:
result = JrpcConv.decode(args.string, argType)
except CatchableError as err:
raise newException(RequestDecodeError,
"Parameter [" & argName & "] of type '" &
$argType & "' could not be decoded: " & err.msg)
# ------------------------------------------------------------------------------
# Compile time helpers
# ------------------------------------------------------------------------------
func hasOptionals(setup: RpcSetup): bool {.compileTime.} =
setup.numOptionals > 0
func rpcSetupImpl[T](val: T): RpcSetup {.compileTime.} =
## Counting number of fields, optional fields, and
## minimum fields needed by a rpc method
mixin rpc_isOptional
var index = 1
for field in fields(val):
inc result.numFields
if rpc_isOptional(field):
inc result.numOptionals
else:
result.minLength = index
inc index
func rpcSetupFromType(T: type): RpcSetup {.compileTime.} =
var dummy: T
rpcSetupImpl(dummy)
template expectOptionalParamsLen(params: RequestParamsRx,
minLength, maxLength: static[int]) =
## Make sure positional params with optional fields
## meets the handler expectation
let
expected = "Expected at least " & $minLength & " and maximum " &
$maxLength & " Json parameter(s) but got "
if params.positional.len < minLength:
raise newException(RequestDecodeError,
expected & $params.positional.len)
template expectParamsLen(params: RequestParamsRx, length: static[int]) =
## Make sure positional params meets the handler expectation
# https://github.com/nim-lang/Nim/issues/24228 especially in a Chronos async
# context, with `{.compileTime.}` `$` `int`, `uint64`, and `int64` overloads
# https://github.com/nim-lang/Nim/blob/v2.0.10/lib/system/dollars.nim#L28-L34
# provides for for compile-time evaluation from, can cause JSON-RPC code not
# to compile. Explicitly choose the non-CTFE overloads, which do not trigger
# this Nim issue.
let
nonConstLength = length
expected = "Expected " & $nonConstLength & " JSON parameter(s) but got "
if params.positional.len != length:
raise newException(RequestDecodeError,
expected & $params.positional.len)
template setupPositional(setup: static[RpcSetup], params: RequestParamsRx) =
## Generate code to check positional params length
when setup.hasOptionals:
expectOptionalParamsLen(params, setup.minLength, setup.numFields)
else:
expectParamsLen(params, setup.numFields)
template len(params: RequestParamsRx): int =
params.positional.len
template notNull(params: RequestParamsRx, pos: int): bool =
params.positional[pos].kind != JsonValueKind.Null
template val(params: RequestParamsRx, pos: int): auto =
params.positional[pos].param
template unpackPositional(params: RequestParamsRx,
paramVar: auto,
paramName: static[string],
pos: static[int],
setup: static[RpcSetup],
paramType: type) =
## Convert a positional parameter from Json into Nim
when not defined(nimHasTemplateRedefinitionPragma):
{.pragma: redefine.}
template innerNode() {.redefine.} =
paramVar = unpackArg(params.val(pos), paramName, paramType)
# e.g. (A: int, B: Option[int], C: string, D: Option[int], E: Option[string])
when rpc_isOptional(paramVar):
when pos >= setup.minLength:
# allow both empty and null after mandatory args
# D & E fall into this category
if params.len > pos and params.notNull(pos):
innerNode()
else:
# allow null param for optional args between/before mandatory args
# B fall into this category
if params.notNull(pos):
innerNode()
else:
# mandatory args
# A and C fall into this category
# unpack Nim type and assign from JSON
if params.notNull(pos):
innerNode()
func makeType(typeName, params: NimNode): NimNode =
## Generate type section contains an object definition
## with fields of handler params
let typeSec = quote do:
type `typeName` = object
let obj = typeSec[0][2]
let recList = newNimNode(nnkRecList)
if params.len > 1:
for i in 1..<params.len:
recList.add params[i]
obj[2] = recList
typeSec
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]
func makeHandler(procName, params, procBody, returnInner: NimNode): NimNode =
## Generate rpc handler proc
let
returnType = quote do: Future[`returnInner`]
paramList = makeParams(returnType, params)
pragmas = quote do: {.async.}
result = newProc(
name = procName,
params = paramList,
body = procBody,
pragmas = pragmas
)
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`)
)
func setupNamed(paramsObj, paramsIdent, params: NimNode): NimNode =
let x = ident"x"
var caseStmt = nnkCaseStmt.newTree(
quote do: `x`.name
)
for paramName, paramType in paramsIter(params):
caseStmt.add ofStmt(x, paramsObj, paramName, paramType)
caseStmt.add nnkElse.newTree(
quote do: discard
)
result = quote do:
for `x` in `paramsIdent`.named:
`caseStmt`
func wrapServerHandler*(methName: string, params, procBody, procWrapper: NimNode): NimNode =
## This proc generate something like this:
##
## proc rpcHandler(paramA: ParamAType, paramB: ParamBType): Future[ReturnType] =
## procBody
## return retVal
##
## proc rpcWrapper(params: RequestParamsRx): Future[JsonString] =
## type
## RpcType = object
## paramA: ParamAType
## paramB: ParamBType
##
## var rpcVar: RpcType
##
## if params.isPositional:
## if params.positional.len < expectedLen:
## raise exception
## rpcVar.paramA = params.unpack(paramA of ParamAType)
## rpcVar.paramB = params.unpack(paramB of ParamBType)
## else:
## # 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
let
params = params.ensureReturnType()
setup = newStmtList()
typeName = genSym(nskType, "RpcType")
paramsObj = ident"rpcVar"
handlerName = genSym(nskProc, methName & "_rpcHandler")
paramsIdent = genSym(nskParam, "rpcParams")
returnType = params[0]
hasParams = params.len > 1 # not including return type
rpcSetup = ident"rpcSetup"
handler = makeHandler(handlerName, params, procBody, returnType)
named = setupNamed(paramsObj, paramsIdent, params)
if hasParams:
setup.add makeType(typeName, params)
setup.add quote do:
const `rpcSetup` = rpcSetupFromType(`typeName`)
var `paramsObj`: `typeName`
# unpack each parameter and provide assignments
var
pos = 0
positional = newStmtList()
executeParams: seq[NimNode]
for paramIdent, paramType in paramsIter(params):
let paramName = $paramIdent
positional.add quote do:
unpackPositional(`paramsIdent`,
`paramsObj`.`paramIdent`,
`paramName`,
`pos`,
`rpcSetup`,
`paramType`)
executeParams.add quote do:
`paramsObj`.`paramIdent`
inc pos
if hasParams:
setup.add quote do:
if `paramsIdent`.kind == rpPositional:
setupPositional(`rpcSetup`, `paramsIdent`)
`positional`
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:
expectParamsLen(`paramsIdent`, 0)
let
awaitedResult = ident "awaitedResult"
doEncode = quote do: encode(JrpcConv, `awaitedResult`)
maybeWrap =
if returnType.noWrap: awaitedResult
else: ident"JsonString".newCall doEncode
executeCall = newCall(handlerName, executeParams)
result = newStmtList()
result.add handler
result.add quote do:
proc `procWrapper`(`paramsIdent`: RequestParamsRx): Future[JsonString] {.async.} =
# Avoid 'yield in expr not lowered' with an intermediate variable.
# See: https://github.com/nim-lang/Nim/issues/17849
`setup`
let `awaitedResult` = await `executeCall`
return `maybeWrap`