Skip to content

Commit

Permalink
Fixed a bug with objects with no fields
Browse files Browse the repository at this point in the history
 * can now be loaded properly
 * ref #130
  • Loading branch information
flyx committed Mar 18, 2023
1 parent 39444f6 commit 8e1b079
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion yaml/serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,13 @@ proc fieldCount(t: NimNode): int {.compiletime.} =


macro matchMatrix(t: typedesc): untyped =
result = newNimNode(nnkBracket)
let numFields = fieldCount(t)
if numFields == 0:
result = quote do:
(seq[bool])(@[])
return

result = newNimNode(nnkBracket)
for i in 0..<numFields:
result.add(newLit(false))

Expand Down Expand Up @@ -1199,6 +1204,16 @@ proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
raise s.constructionError(item.startPos, "Anchor on non-ref type")
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,
result: var Option[T]) =
## constructs an optional value. A value with a !!null tag will be loaded
Expand Down Expand Up @@ -1282,6 +1297,9 @@ proc representChild*(value: string, ts: TagStyle, c: SerializationContext) =
proc representChild*[T](value: seq[T], ts: TagStyle, c: SerializationContext) =
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) =
if isNil(value): c.put(scalarEvent("~", yTagNull))
else:
Expand Down

0 comments on commit 8e1b079

Please sign in to comment.