flyx / NimYAML

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

dumping leaves part of string on same line as >- making the file unable to be loaded from #112

Closed wreedb closed 2 years ago

wreedb commented 2 years ago

My program uses de-serialized JSON turned into objects as a way to display content, and I was working on creating an index of these objects into YAML, but when dumping a sequence of these objects to a file with NimYAML, the dump (about 40% of the time) leaves a trailing character on the same line as a >- ... forgive me for not knowing what >- is called, i think its a fold? but either way, it leaves a single character on the same line with that fold signifier and that makes loading from it crash every time. I will attach an example photo, i was unable to format backticks to show what i am describing

I have been able to mitigate this by change the NimYAML presenter.nim file a bit, but it isn't pretty... some lines use double quotes, some do not (even though they are strings) and i would much prefer for someone to look into this, or simply tell me I am an idiot and point me towards a potential solution to my problem, as i think going the distance to edit the library myself was a pretty good attempt to solve it after i was unable to figure it out from the documentation.

for reference, the affected entry is the first in this list of two - specifically the description the image

flyx commented 2 years ago

Probably duplicate of #105, try nimble install yaml#head and see if it goes away.

I should do a release with this fix…

wreedb commented 2 years ago

I ran the command you suggested and neither installing or searching for this yielded any packages with nimble

flyx commented 2 years ago

Well I don‘t know your setup. On a proper nimble installation it should fetch the current master version of NimYAML, which incorporates the fix mentioned.

If your setup doesn‘t allow you to do this for some reason, you have to wait for the next release. I estimate this to happen in 2-3 weeks.

flyx commented 2 years ago

I just remembered they changed the syntax, it's nimble install yaml@#head.

wreedb commented 2 years ago

Ah, I see, I was a bit stumped by that as I thought I did have a proper installation of the library. I will try this when I get home from work and let you know if it fixes the issue. However, I had a question about the library-- I hacked up the presenter file on my machine to give me more so the behavior that I wanted, which is that it does not create new lines for values whatsoever. I then noticed that this was writing the values (sometimes) with double quotes, and (sometimes) not with double quotes, so I incorporated a function in my code to execute a perl command to remove every double quotation mark from the file that was written. Perhaps I've only made this unnecessarily messy and difficult for myself... Does this library have ways to control those formatting choices without hacking on the presenter file?

flyx commented 2 years ago

Does this library have ways to control those formatting choices without hacking on the presenter file?

There's PresentationOptions but it doesn't offer much. Generally, NimYAML's presenter has been written with the assumption that you serialize data for communication between software. Hence it cares only about the output being valid YAML, but not much about how the output formatted; as long as it's valid YAML, the software on the other end will be able to load it. The reason for that assumption is that if you cared about details of the presentation, you would probably choose another format like XML that gives far less leeway to formatting than YAML does, and hence will give you better control about how it looks when you serialize it.

NimYAML does allow for some more control over the output format via its YamlStream interface:

import yaml

proc dumpModified[T](value: T, target: Stream) =
  var
    origEvents = represent(value) # add other args as needed
    buffer = newBufferYamlStream()
  for e in origEvents:
    case e.kind
    of yamlScalar: e.scalarStyle = ssPlain
    else: discard
    buffer.put(e)
  present(buffer, target) # possibly give options here

(I'm not using Nim anymore apart from maintaining this lib so beware of errors)

This code would set the style of all scalars to plain (i.e. without quotes). If you need to choose different styles for different data, write custom representObject functions for your types and generate Events with the style set appropriately, as explained in the docs.

wreedb commented 2 years ago

Ah, perhaps I should stop being so finicky about the format of something only I will see. However I will give this example a shot and see how it goes. You can probably close this issue if it is going to be fixed in the next release, and I'd like to say thank you for communicating with me about it, and for being very helpful when you were able.

flyx commented 2 years ago

I'd say it's a bit of a design problem of YAML to say "I'm a serialization language but you can use me in a lot of different styles". There will always be a discrepancy between how a user can style a YAML file and how a YAML implementation can, and that does cause friction because users want the implementation to write YAML like they do, and usually it doesn't. For example, very few YAML implementations, and certainly not NimYAML, can write comments.

Closing this as duplicate.