Skip ignored nested collections. Fixes #127

This commit is contained in:
Felix Krause 2023-03-10 19:57:21 +01:00
parent 7dd8e88639
commit f692a47820
1 changed files with 15 additions and 9 deletions

View File

@ -733,6 +733,16 @@ macro ensureAllFieldsPresent(s: YamlStream, t: typedesc, o: typed,
result.add(checkMissing(s, t, tName, child, field, matched, o, m)) result.add(checkMissing(s, t, tName, child, field, matched, o, m))
inc(field) inc(field)
proc skipOverValue(s: var YamlStream) =
var e = s.next()
var depth = int(e.kind in {yamlStartMap, yamlStartSeq})
while depth > 0:
case s.next().kind
of yamlStartMap, yamlStartSeq: inc(depth)
of yamlEndMap, yamlEndSeq: dec(depth)
of yamlScalar, yamlAlias: discard
else: internalError("Unexpected event kind.")
macro constructFieldValue(t: typedesc, stream: untyped, macro constructFieldValue(t: typedesc, stream: untyped,
context: untyped, name: untyped, o: untyped, context: untyped, name: untyped, o: untyped,
matched: untyped, failOnUnknown: bool, m: untyped) = matched: untyped, failOnUnknown: bool, m: untyped) =
@ -818,7 +828,10 @@ macro constructFieldValue(t: typedesc, stream: untyped,
newNimNode(nnkRaiseStmt).add( newNimNode(nnkRaiseStmt).add(
newCall(bindSym("constructionError"), stream, m, newCall(bindSym("constructionError"), stream, m,
infix(newLit("While constructing " & tName & ": Unknown field: "), "&", infix(newLit("While constructing " & tName & ": Unknown field: "), "&",
newCall(bindSym("escape"), name)))))))) newCall(bindSym("escape"), name)))))
).add(newNimNode(nnkElse).add(
newCall(bindSym("skipOverValue"), stream)
))))
result.add(caseStmt) result.add(caseStmt)
proc isVariantObject(t: NimNode): bool {.compileTime.} = proc isVariantObject(t: NimNode): bool {.compileTime.} =
@ -889,14 +902,7 @@ proc constructObjectDefault*[O: object|tuple](
if name notin ignoredKeyList: if name notin ignoredKeyList:
constructFieldValue(O, s, c, name, result, matched, failOnUnknown, e.startPos) constructFieldValue(O, s, c, name, result, matched, failOnUnknown, e.startPos)
else: else:
e = s.next() skipOverValue(s)
var depth = int(e.kind in {yamlStartMap, yamlStartSeq})
while depth > 0:
case s.next().kind
of yamlStartMap, yamlStartSeq: inc(depth)
of yamlEndMap, yamlEndSeq: dec(depth)
of yamlScalar, yamlAlias: discard
else: internalError("Unexpected event kind.")
else: else:
constructFieldValue(O, s, c, name, result, matched, failOnUnknown, e.startPos) constructFieldValue(O, s, c, name, result, matched, failOnUnknown, e.startPos)
when isVariantObject(getType(O)): when isVariantObject(getType(O)):