Implement support for enum fields in JSON macro.
This commit is contained in:
parent
bd58a0d67c
commit
88cb40cd31
2 changed files with 24 additions and 2 deletions
|
|
@ -1421,7 +1421,6 @@ proc processElseBranch(recCaseNode, elseBranch, jsonNode, kindType,
|
||||||
## RecList
|
## RecList
|
||||||
## Sym "other"
|
## Sym "other"
|
||||||
result = @[]
|
result = @[]
|
||||||
# TODO: Remove duplication between processOfBranch
|
|
||||||
let getEnumCall = createGetEnumCall(kindJsonNode, kindType)
|
let getEnumCall = createGetEnumCall(kindJsonNode, kindType)
|
||||||
|
|
||||||
# We need to build up a list of conditions from each ``of`` branch so that
|
# We need to build up a list of conditions from each ``of`` branch so that
|
||||||
|
|
@ -1525,6 +1524,13 @@ proc processType(typeName: NimNode, obj: NimNode,
|
||||||
for field in obj[2]:
|
for field in obj[2]:
|
||||||
let nodes = processObjField(field, jsonNode)
|
let nodes = processObjField(field, jsonNode)
|
||||||
result.add(nodes)
|
result.add(nodes)
|
||||||
|
of nnkEnumTy:
|
||||||
|
let instType = toIdentNode(getTypeInst(typeName))
|
||||||
|
let getEnumCall = createGetEnumCall(jsonNode, instType)
|
||||||
|
result = quote do:
|
||||||
|
(
|
||||||
|
`getEnumCall`
|
||||||
|
)
|
||||||
of nnkSym:
|
of nnkSym:
|
||||||
case ($typeName).normalize
|
case ($typeName).normalize
|
||||||
of "float":
|
of "float":
|
||||||
|
|
|
||||||
|
|
@ -166,4 +166,20 @@ when isMainModule:
|
||||||
var data = to(jsonNode, Data)
|
var data = to(jsonNode, Data)
|
||||||
doAssert data.person.name == "Nimmer"
|
doAssert data.person.name == "Nimmer"
|
||||||
doAssert data.person.age == 21
|
doAssert data.person.age == 21
|
||||||
doAssert data.list == @[1, 2, 3, 4]
|
doAssert data.list == @[1, 2, 3, 4]
|
||||||
|
|
||||||
|
# Test non-variant enum fields.
|
||||||
|
block:
|
||||||
|
type
|
||||||
|
EnumType = enum
|
||||||
|
Foo, Bar
|
||||||
|
|
||||||
|
TestEnum = object
|
||||||
|
field: EnumType
|
||||||
|
|
||||||
|
var node = %{
|
||||||
|
"field": %"Bar"
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = to(node, TestEnum)
|
||||||
|
doAssert result.field == Bar
|
||||||
Loading…
Add table
Add a link
Reference in a new issue