diff --git a/test/testEventParser.nim b/test/testEventParser.nim index 4db6b25..231d729 100644 --- a/test/testEventParser.nim +++ b/test/testEventParser.nim @@ -170,8 +170,7 @@ template setCurAnchor(val: AnchorId) = template eventStart(k: YamlStreamEventKind) {.dirty.} = assertInStream() yieldEvent() - reset(curEvent) - curEvent.kind = k + curEvent = YamlStreamEvent(kind: k) setTag(yTagQuestionMark) setAnchor(yAnchorNone) inEvent = true diff --git a/yaml/dom.nim b/yaml/dom.nim index 19d7a57..1a47ab6 100644 --- a/yaml/dom.nim +++ b/yaml/dom.nim @@ -148,9 +148,9 @@ proc composeNode(s: var YamlStream, tagLib: TagLibrary, try: case start.kind of yamlStartMap: - result.tag = tagLib.uri(start.mapTag) - result.kind = yMapping - result.fields = newTable[YamlNode, YamlNode]() + result = YamlNode(tag: tagLib.uri(start.mapTag), + kind: yMapping, + fields: newTable[YamlNode, YamlNode]()) while s.peek().kind != yamlEndMap: let key = composeNode(s, tagLib, c) @@ -161,16 +161,16 @@ proc composeNode(s: var YamlStream, tagLib: TagLibrary, discard s.next() addAnchor(c, start.mapAnchor) of yamlStartSeq: - result.tag = tagLib.uri(start.seqTag) - result.kind = ySequence - result.elems = newSeq[YamlNode]() + result = YamlNode(tag: tagLib.uri(start.seqTag), + kind: ySequence, + elems: newSeq[YamlNode]()) while s.peek().kind != yamlEndSeq: result.elems.add(composeNode(s, tagLib, c)) addAnchor(c, start.seqAnchor) discard s.next() of yamlScalar: - result.tag = tagLib.uri(start.scalarTag) - result.kind = yScalar + result = YamlNode(tag: tagLib.uri(start.scalarTag), + kind: yScalar) shallowCopy(result.content, start.scalarContent) addAnchor(c, start.scalarAnchor) of yamlAlias: diff --git a/yaml/serialization.nim b/yaml/serialization.nim index 728f38e..84d5549 100644 --- a/yaml/serialization.nim +++ b/yaml/serialization.nim @@ -774,13 +774,21 @@ macro constructFieldValue(t: typedesc, tIndex: int, stream: untyped, discriminant = newDotExpr(o, newIdentNode($child[0])) discType = newCall("type", discriminant) var disOb = newNimNode(nnkOfBranch).add(newStrLitNode($child[0])) + var objConstr = newNimNode(nnkObjConstr).add(newCall("type", o)) + objConstr.add(newColonExpr(newIdentNode($child[0]), newIdentNode( + "value"))) + for otherChild in tDesc[2].children: + if otherChild == child: + continue + objConstr.add(newColonExpr(newIdentNode($otherChild), newDotExpr(o, + newIdentNode($otherChild)))) disOb.add(newStmtList( checkDuplicate(stream, tName, $child[0], fieldIndex, matched), newNimNode(nnkVarSection).add( newNimNode(nnkIdentDefs).add( newIdentNode("value"), discType, newEmptyNode())), newCall("constructChild", stream, context, newIdentNode("value")), - newAssignment(discriminant, newIdentNode("value")), + newAssignment(o, objConstr), markAsFound(fieldIndex, matched))) caseStmt.add(disOb) var alreadyUsedSet = newNimNode(nnkCurly) @@ -875,7 +883,6 @@ proc constructObjectDefault*[O: object|tuple]( if e.kind != startKind: raise s.constructionError("While constructing " & typetraits.name(O) & ": Expected " & $startKind & ", got " & $e.kind) - when isVariantObject(getType(O)): reset(result) # make discriminants writeable injectIgnoredKeyList(O, ignoredKeyList) injectFailOnUnknownKeys(O, failOnUnknown) while s.peek.kind != endKind: @@ -1063,12 +1070,15 @@ macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped, yAssert tDesc.kind == nnkObjectTy let recCase = tDesc[2][0] yAssert recCase.kind == nnkRecCase - let discriminant = newDotExpr(r, newIdentNode($recCase[0])) - var ifStmt = newNimNode(nnkIfStmt) + result = newNimNode(nnkIfStmt) for i in 1 .. recCase.len - 1: yAssert recCase[i].kind == nnkOfBranch var branch = newNimNode(nnkElifBranch) - var branchContent = newStmtList(newAssignment(discriminant, recCase[i][0])) + var branchContent = newStmtList(newAssignment(r, + newNimNode(nnkObjConstr).add( + newCall("type", r), + newColonExpr(newIdentNode($recCase[0]), recCase[i][0]) + ))) case recCase[i][1].recListLen of 0: branch.add(infix(newIdentNode("yTagNull"), "in", possibleTagIds)) @@ -1082,7 +1092,7 @@ macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped, block: internalError("Too many children: " & $recCase[i][1].recListlen) branch.add(branchContent) - ifStmt.add(branch) + result.add(branch) let raiseStmt = newNimNode(nnkRaiseStmt).add( newCall(bindSym("constructionError"), s, infix(newStrLitNode("This value type does not map to any field in " & @@ -1091,12 +1101,11 @@ macro constructImplicitVariantObject(s, c, r, possibleTagIds: untyped, newNimNode(nnkBracketExpr).add(possibleTagIds, newIntLitNode(0))) ) )) - ifStmt.add(newNimNode(nnkElse).add(newNimNode(nnkTryStmt).add( + result.add(newNimNode(nnkElse).add(newNimNode(nnkTryStmt).add( newStmtList(raiseStmt), newNimNode(nnkExceptBranch).add( newIdentNode("KeyError"), newNimNode(nnkDiscardStmt).add(newEmptyNode()) )))) - result = newStmtList(newCall("reset", r), ifStmt) macro isImplicitVariantObject(o: typed): untyped = result = newCall("compiles", newCall(implicitVariantObjectMarker, o)) diff --git a/yaml/tojson.nim b/yaml/tojson.nim index 6dafd45..01ab8d4 100644 --- a/yaml/tojson.nim +++ b/yaml/tojson.nim @@ -51,27 +51,21 @@ proc jsonFromScalar(content: string, tag: TagId): JsonNode try: case mappedType of yTypeInteger: - result.kind = JInt - result.num = parseBiggestInt(content) + result = JsonNode(kind: JInt, num: parseBiggestInt(content)) of yTypeFloat: - result.kind = JFloat - result.fnum = parseFloat(content) + result = JsonNode(kind: JFloat, fnum: parseFloat(content)) of yTypeFloatInf: - result.kind = JFloat - result.fnum = if content[0] == '-': NegInf else: Inf + result = JsonNode(kind: JFloat, fnum: if content[0] == '-': NegInf else: Inf) of yTypeFloatNaN: - result.kind = JFloat - result.fnum = NaN + result = JsonNode(kind: JFloat, fnum: NaN) of yTypeBoolTrue: - result.kind = JBool - result.bval = true + result = JsonNode(kind: JBool, bval: true) of yTypeBoolFalse: - result.kind = JBool - result.bval = false + result = JsonNode(kind: JBool, bval: false) of yTypeNull: - result.kind = JNull + result = JsonNode(kind: JNull) else: - result.kind = JString + result = JsonNode(kind: JString) shallowCopy(result.str, content) except ValueError: var e = newException(YamlConstructionError, "Cannot parse numeric value")