Fixed a bug with objects with no fields

* can now be loaded properly
 * ref #130
This commit is contained in:
Felix Krause 2023-03-18 14:50:38 +01:00
parent 39444f6536
commit 8e1b07975b
1 changed files with 19 additions and 1 deletions

View File

@ -635,8 +635,13 @@ proc fieldCount(t: NimNode): int {.compiletime.} =
macro matchMatrix(t: typedesc): untyped = macro matchMatrix(t: typedesc): untyped =
result = newNimNode(nnkBracket)
let numFields = fieldCount(t) let numFields = fieldCount(t)
if numFields == 0:
result = quote do:
(seq[bool])(@[])
return
result = newNimNode(nnkBracket)
for i in 0..<numFields: for i in 0..<numFields:
result.add(newLit(false)) result.add(newLit(false))
@ -1199,6 +1204,16 @@ proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
raise s.constructionError(item.startPos, "Anchor on non-ref type") raise s.constructionError(item.startPos, "Anchor on non-ref type")
constructObject(s, c, result) constructObject(s, c, result)
proc constructChild*[I, T](s: var YamlStream, c: ConstructionContext,
result: var array[I, T]) =
let item = s.peek()
if item.kind == yamlStartSeq:
if item.seqProperties.tag notin [yTagQuestionMark, yamlTag(array[I, T])]:
raise s.constructionError(item.startPos, "Wrong tag for " & typetraits.name(array[I, T]))
elif item.seqProperties.anchor != yAnchorNone:
raise s.constructionError(item.startPos, "Anchor on non-ref type")
constructObject(s, c, result)
proc constructChild*[T](s: var YamlStream, c: ConstructionContext, proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
result: var Option[T]) = result: var Option[T]) =
## constructs an optional value. A value with a !!null tag will be loaded ## constructs an optional value. A value with a !!null tag will be loaded
@ -1282,6 +1297,9 @@ proc representChild*(value: string, ts: TagStyle, c: SerializationContext) =
proc representChild*[T](value: seq[T], ts: TagStyle, c: SerializationContext) = proc representChild*[T](value: seq[T], ts: TagStyle, c: SerializationContext) =
representObject(value, ts, c, presentTag(seq[T], ts)) representObject(value, ts, c, presentTag(seq[T], ts))
proc representChild*[I, T](value: array[I, T], ts: TagStyle, c: SerializationContext) =
representObject(value, ts, c, presentTag(array[I, T], ts))
proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext) = proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext) =
if isNil(value): c.put(scalarEvent("~", yTagNull)) if isNil(value): c.put(scalarEvent("~", yTagNull))
else: else: