mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-17 14:31:15 +00:00
* fixes #21638; `fromJson` should supports empty objects
* complete the logic
(cherry picked from commit 5e016e4466)
This commit is contained in:
@@ -89,19 +89,24 @@ macro getDiscriminants(a: typedesc): seq[string] =
|
|||||||
let sym = a[1]
|
let sym = a[1]
|
||||||
let t = sym.getTypeImpl
|
let t = sym.getTypeImpl
|
||||||
let t2 = t[2]
|
let t2 = t[2]
|
||||||
doAssert t2.kind == nnkRecList
|
case t2.kind
|
||||||
result = newTree(nnkBracket)
|
of nnkEmpty: # allow empty objects
|
||||||
for ti in t2:
|
|
||||||
if ti.kind == nnkRecCase:
|
|
||||||
let key = ti[0][0]
|
|
||||||
let typ = ti[0][1]
|
|
||||||
result.add newLit key.strVal
|
|
||||||
if result.len > 0:
|
|
||||||
result = quote do:
|
result = quote do:
|
||||||
@`result`
|
seq[string].default
|
||||||
|
of nnkRecList:
|
||||||
|
result = newTree(nnkBracket)
|
||||||
|
for ti in t2:
|
||||||
|
if ti.kind == nnkRecCase:
|
||||||
|
let key = ti[0][0]
|
||||||
|
result.add newLit key.strVal
|
||||||
|
if result.len > 0:
|
||||||
|
result = quote do:
|
||||||
|
@`result`
|
||||||
|
else:
|
||||||
|
result = quote do:
|
||||||
|
seq[string].default
|
||||||
else:
|
else:
|
||||||
result = quote do:
|
doAssert false, "unexpected kind: " & $t2.kind
|
||||||
seq[string].default
|
|
||||||
|
|
||||||
macro initCaseObject(T: typedesc, fun: untyped): untyped =
|
macro initCaseObject(T: typedesc, fun: untyped): untyped =
|
||||||
## does the minimum to construct a valid case object, only initializing
|
## does the minimum to construct a valid case object, only initializing
|
||||||
|
|||||||
@@ -409,6 +409,11 @@ template fn() =
|
|||||||
doAssert foo.c == 0
|
doAssert foo.c == 0
|
||||||
doAssert foo.c0 == 42
|
doAssert foo.c0 == 42
|
||||||
|
|
||||||
|
block: # bug #21638
|
||||||
|
type Something = object
|
||||||
|
|
||||||
|
doAssert "{}".parseJson.jsonTo(Something) == Something()
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
## TODO: Implement support for nested variant objects allowing the tests
|
## TODO: Implement support for nested variant objects allowing the tests
|
||||||
## bellow to pass.
|
## bellow to pass.
|
||||||
|
|||||||
Reference in New Issue
Block a user