mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-01-08 08:33:14 +00:00
Fix #124
This commit is contained in:
parent
97ba55bbf6
commit
51fadf850c
@ -243,26 +243,33 @@ proc jsonToNim*(params, jsonIdent: NimNode): NimNode =
|
|||||||
jsonElement = quote do:
|
jsonElement = quote do:
|
||||||
`jsonIdent`.elems[`pos`]
|
`jsonIdent`.elems[`pos`]
|
||||||
|
|
||||||
inc pos
|
|
||||||
# declare variable before assignment
|
# declare variable before assignment
|
||||||
result.add(quote do:
|
result.add(quote do:
|
||||||
var `paramIdent`: `paramType`
|
var `paramIdent`: `paramType`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# e.g. (A: int, B: Option[int], C: string, D: Option[int], E: Option[string])
|
||||||
if paramType.isOptionalArg:
|
if paramType.isOptionalArg:
|
||||||
let
|
let
|
||||||
nullAble = pos < minLength
|
|
||||||
innerType = paramType[1]
|
innerType = paramType[1]
|
||||||
innerNode = jsonToNim(paramIdent, innerType, jsonElement, paramName, true)
|
innerNode = jsonToNim(paramIdent, innerType, jsonElement, paramName, true)
|
||||||
|
|
||||||
if nullAble:
|
if pos >= minLength:
|
||||||
|
# allow both empty and null after mandatory args
|
||||||
|
# D & E fall into this category
|
||||||
|
result.add(quote do:
|
||||||
|
if `jsonIdent`.len > `pos` and `jsonElement`.kind != JNull: `innerNode`
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# allow null param for optional args between/before mandatory args
|
||||||
|
# B fall into this category
|
||||||
result.add(quote do:
|
result.add(quote do:
|
||||||
if `jsonElement`.kind != JNull: `innerNode`
|
if `jsonElement`.kind != JNull: `innerNode`
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
result.add(quote do:
|
|
||||||
if `jsonIdent`.len >= `pos`: `innerNode`
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
|
# mandatory args
|
||||||
|
# A and C fall into this category
|
||||||
# unpack Nim type and assign from json
|
# unpack Nim type and assign from json
|
||||||
result.add jsonToNim(paramIdent, paramType, jsonElement, paramName)
|
result.add jsonToNim(paramIdent, paramType, jsonElement, paramName)
|
||||||
|
|
||||||
|
inc pos
|
||||||
|
|||||||
@ -79,6 +79,12 @@ s.rpc("rpc.optionalArg") do(val: int, obj: Option[MyOptional]) -> MyOptional:
|
|||||||
else:
|
else:
|
||||||
MyOptional(maybeInt: some(val))
|
MyOptional(maybeInt: some(val))
|
||||||
|
|
||||||
|
s.rpc("rpc.optionalArg2") do(a, b: string, c, d: Option[string]) -> string:
|
||||||
|
var ret = a & b
|
||||||
|
if c.isSome: ret.add c.get()
|
||||||
|
if d.isSome: ret.add d.get()
|
||||||
|
return ret
|
||||||
|
|
||||||
type
|
type
|
||||||
OptionalFields = object
|
OptionalFields = object
|
||||||
a: int
|
a: int
|
||||||
@ -211,8 +217,32 @@ suite "Server types":
|
|||||||
int2 = MyOptional(maybeInt: some(117))
|
int2 = MyOptional(maybeInt: some(117))
|
||||||
r1 = waitFor s.executeMethod("rpc.optionalArg", %[%117, %int1])
|
r1 = waitFor s.executeMethod("rpc.optionalArg", %[%117, %int1])
|
||||||
r2 = waitFor s.executeMethod("rpc.optionalArg", %[%117])
|
r2 = waitFor s.executeMethod("rpc.optionalArg", %[%117])
|
||||||
|
r3 = waitFor s.executeMethod("rpc.optionalArg", %[%117, newJNull()])
|
||||||
check r1 == %int1
|
check r1 == %int1
|
||||||
check r2 == %int2
|
check r2 == %int2
|
||||||
|
check r3 == %int2
|
||||||
|
|
||||||
|
test "Optional arg2":
|
||||||
|
let r1 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B"])
|
||||||
|
check r1 == %"AB"
|
||||||
|
|
||||||
|
let r2 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B", newJNull()])
|
||||||
|
check r2 == %"AB"
|
||||||
|
|
||||||
|
let r3 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B", newJNull(), newJNull()])
|
||||||
|
check r3 == %"AB"
|
||||||
|
|
||||||
|
let r4 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B", newJNull(), %"D"])
|
||||||
|
check r4 == %"ABD"
|
||||||
|
|
||||||
|
let r5 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B", %"C", %"D"])
|
||||||
|
check r5 == %"ABCD"
|
||||||
|
|
||||||
|
let r6 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B", %"C", newJNull()])
|
||||||
|
check r6 == %"ABC"
|
||||||
|
|
||||||
|
let r7 = waitFor s.executeMethod("rpc.optionalArg2", %[%"A", %"B", %"C"])
|
||||||
|
check r7 == %"ABC"
|
||||||
|
|
||||||
test "Mixed optional arg":
|
test "Mixed optional arg":
|
||||||
var ax = waitFor s.executeMethod("rpc.mixedOptionalArg", %[%10, %11, %"hello", %12, %"world"])
|
var ax = waitFor s.executeMethod("rpc.mixedOptionalArg", %[%10, %11, %"hello", %12, %"world"])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user