Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wreedb opened this issue Jun 28, 2022 · 8 comments

Comments

@wreedb
Copy link

wreedb commented Jun 28, 2022

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
Copy link
Owner

flyx commented Jun 28, 2022

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

I should do a release with this fix…

@wreedb
Copy link
Author

wreedb commented Jun 28, 2022

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

@flyx
Copy link
Owner

flyx commented Jun 28, 2022

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
Copy link
Owner

flyx commented Jun 30, 2022

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

@wreedb
Copy link
Author

wreedb commented Jun 30, 2022

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
Copy link
Owner

flyx commented Jun 30, 2022

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
Copy link
Author

wreedb commented Jun 30, 2022

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
Copy link
Owner

flyx commented Jun 30, 2022

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.

@flyx flyx closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants