flyx / NimYAML

YAML implementation for Nim
https://nimyaml.org
Other
191 stars 36 forks source link

Can't load a dumped OrderedTable if it has a long string key with brackets #142

Closed cerk closed 8 months ago

cerk commented 8 months ago

I am using NimYAML to dump an OrderedTable. Everything works great unless one of the keys in the table is greater in length than dumper.presentation.maxLineLength and also has brackets ([]) in it. In this case, NimYAML dumps something out but then can't read it back in. Here is a small example program that demonstrates the issue:

import std/streams
import std/tables
import yaml

type SerializeObj = object
  nickname: OrderedTable[string, string]

proc main =
  var nickname: OrderedTable[string, string]
  # There is only an error if there are brackets in the key.
  nickname["[This key with brackets is purposefully long so that it goes over the default 80 character limit]"] = "Some string"

  let outObj = SerializeObj(nickname: nickname)
  var strStream = newStringStream()
  var dumper = Dumper()
  # If I uncomment this, there is no error.
  #dumper.presentation.maxLineLength = some(256)
  dumper.dump(outObj, strStream)
  echo strStream.data

  var inObj: SerializeObj
  # Error: unhandled exception: Expected map start, got yamlScalar.
  load(strStream.data, inObj)
  strStream.close()

  for k, v in nickname:
    echo k, ": ", v

when isMainModule:
  main()

As commented in the code, if I increase dumper.presentation.maxLineLength, the issue goes away.

flyx commented 8 months ago

Fixed it, thanks for reporting!