Skip to content

Commit

Permalink
make json.to work with the more restricted case objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed May 27, 2019
1 parent 49e686a commit 383147f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
25 changes: 11 additions & 14 deletions lib/pure/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1111,11 +1111,11 @@ proc processObjField(field, jsonNode: NimNode): seq[NimNode] =
exprColonExpr.add(createConstructor(typeNode, indexedJsonNode))
of nnkRecCase:
# A "case" field that introduces a variant.
let exprColonExpr = newNimNode(nnkExprColonExpr)
result.add(exprColonExpr)
let exprEqExpr = newNimNode(nnkExprEqExpr)
result.add(exprEqExpr)

# Add the "case" field name (usually "kind").
exprColonExpr.add(toIdentNode(field[0]))
exprEqExpr.add(toIdentNode(field[0]))

# -> jsonNode["`field[0]`"]
let kindJsonNode = createJsonIndexer(jsonNode, $field[0])
Expand All @@ -1125,7 +1125,7 @@ proc processObjField(field, jsonNode: NimNode): seq[NimNode] =
let getEnumSym = bindSym("getEnum")
let astStrLit = toStrLit(kindJsonNode)
let getEnumCall = newCall(getEnumSym, kindJsonNode, astStrLit, kindType)
exprColonExpr.add(getEnumCall)
exprEqExpr.add(getEnumCall)

# Iterate through each `of` branch.
for i in 1 ..< field.len:
Expand Down Expand Up @@ -1475,20 +1475,18 @@ proc postProcess(node: NimNode): NimNode =
# TODO: Placing `node[0]` inside quote is buggy
var resType = toIdentNode(node[0])

result.add(
quote do:
var `resIdent` = `resType`();
)
var objConstr = newTree(nnkObjConstr, resType)
result.add newVarStmt(resIdent, objConstr)

# Process each ExprColonExpr.
for i in 1..<len(node):
result.add postProcessExprColonExpr(node[i], resIdent)
if node[i].kind == nnkExprEqExpr:
objConstr.add newTree(nnkExprColonExpr, node[i][0], node[i][1])
else:
result.add postProcessExprColonExpr(node[i], resIdent)

# Return the `res` variable.
result.add(
quote do:
`resIdent`
)
result.add(resIdent)


macro to*(node: JsonNode, T: typedesc): untyped =
Expand Down Expand Up @@ -1539,7 +1537,6 @@ macro to*(node: JsonNode, T: typedesc): untyped =
let `temp` = `node`

let constructor = createConstructor(typeNode[1], temp)
# TODO: Rename postProcessValue and move it (?)
result.add(postProcessValue(constructor))

# echo(treeRepr(result))
Expand Down
6 changes: 3 additions & 3 deletions lib/system/assign.nim
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ proc selectBranch(discVal, L: int,
a: ptr array[0x7fff, ptr TNimNode]): ptr TNimNode =
result = a[L] # a[L] contains the ``else`` part (but may be nil)
if discVal <% L:
var x = a[discVal]
let x = a[discVal]
if x != nil: result = x

proc FieldDiscriminantCheck(oldDiscVal, newDiscVal: int,
a: ptr array[0x7fff, ptr TNimNode],
L: int) {.compilerProc.} =
var oldBranch = selectBranch(oldDiscVal, L, a)
var newBranch = selectBranch(newDiscVal, L, a)
let oldBranch = selectBranch(oldDiscVal, L, a)
let newBranch = selectBranch(newDiscVal, L, a)
when defined(nimOldCaseObjects):
if newBranch != oldBranch and oldDiscVal != 0:
sysFatal(FieldError, "assignment to discriminant changes object branch")
Expand Down

0 comments on commit 383147f

Please sign in to comment.