-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
(yaml) Emit comments #36
Comments
There is no way to emit comments, in general, by Jackson, so it would be necessary to first figure out why this is something to add (use case, and where contents of comments should come from). So... what is the use case? Why comments? Containing what? |
Using a YAML file as a user-setup file in which an user will be able to change some settings in an easy way, using a notepad. If you want to clarify some of the options you use comments so the user will be able to understand what he should modify and what will cause any of the modifications. |
I guess I am more used to people adding comments manually, and more trying to understand how a machine-processable generation would add comments. I am familiar with comment usage in XML, where generators may add sort of prefix comment containing diagnostics metadata (generation timestamp, system etc). I think it would be possible to add something like |
Even if Jackon finds a way to create API to emit comments, SnakeYAML explicitly does not support dumping comments. It is not supported by the YAML spec. |
@asomov Hmmh. Ok, that would indeed be a blocker. I understand that retaining comments can be tricky (as they really do not play well with object models in general), wrt reading. But was assuming that there might be a way to generate them -- this was useful with XML, for example, adding comments as human-debuggable headers for things like "file generated by [tool name] on [date]" and such, instead of (or in addition to) actual machine-readable (meta)data. |
@cowtowncoder : XML has comments as part of the spec. YAML explicitly ignores the comments. |
@asomov I understand that. I just find this... unexpected. Thank you for pointing out point in specification. Just one point however: while this clarifies that comments are essentially not to be exposed when reading YAML (or preserved within model), they clearly exist in textual representation. As such it would seem odd that generators could not produce them. Presumably that would mean that only humans with with text editors were to use them as... annotations? Put another way: specification does not seem to have much to say on output/generation side, which is what this issue is about. |
The spec says that the generation must be controlled by the composed Node tree. The comments are not part of the Node tree. |
@asomov Ok. I am not familiar enough with the specification there -- it is rather big spec, after all. Another way to view this would also be whether any other YAML tools allow emission or not. At this point it does sound like adding such output may not be a good idea. |
I do not know any tool which can generate comments. |
@asomov @cowtowncoder Are you sure that comments can't be included? |
Where do you see that they are a part of the tree? Comments are a presentation detail and must not have any effect on the serialization tree or representation graph. In particular, comments are not associated with a particular node. " just pass additional argument to emitter with data about comments/style" - well, if you have a proposal how to implement that then feel free to share it with us. |
@asomov you ofc need own emitter then, as SnakeYaml does not support anything like that, so it is probably too complicated to include and support inside jackson |
|
@asomov this was my "private" project and internal code - so I didn't really care about being compilat with yaml specification, I just needed comments, so it was more like a hack. So it is not anything you would want to use in more popular and used project :) |
Than I do not get the reason to start the conversation here (with the references to the spec). |
I just wanted to throw few ideas here (like passing that comments object next to nodes, as this seems to be fine with specification?), as once I also needed feature like that but needed to do it alone i a bit hacky way as there isn't any ready-to-use solution for generating nice configuration files with comments, but would be nice to finally have some better library for this. Sadly jackson can be problematic anyway with yaml, as after all yaml have many features that don't map well to json model, like object keys. Sorry then :) |
On plus side, I think there is agreement that:
I think I will leave this open as a placeholder just in case someone else was looking for this, so it is easy to see our current thinking. |
|
@asomov I don't know if there is any reason for me to describe this, as this was created for simple configuration files, I didn't need to care about many corner cases. in my code I just created CommentsDocument object that was storing header, footer and comments data for each node described by path inside yaml structure, like: some:
object:
# Hello
field: ... For lists I was just ignoring them, so it would also work for: some:
object:
-
# Hello
field: ... And for nested objects in other maps I had a wildcard token: Additionally there was a class that was able to read comments from annotations over classes and include them to final document while/before serializing. But my code was only designed for simple configuration files, and it just needed to work ;) |
@cowtowncoder SnakeYAML Engine is going to support dumping comments. |
Adding support may be tricky as a general feature, although adding methods in |
@cowtowncoder if no one wishes to contribute, than the feature is not that useful. We have done our part and we are ready to help the volunteers to make the dream come true. |
@asomov I appreciate your adding functionality that makes it possible, and will be happy to at least add one mechanism on Marking as "good first issue" in case someone has time to just do the simple thing first. |
@cowtowncoder unfortunately I cannot help you. The support for comments in SnakeYAML ends on the Node level (before Java instances come into play). I am not sure it can be generalized to be used in Jackson. Important: support for comments is not yet released ! I wanted to give it a try. May be we will need to change the API. |
That level makes sense. If I understood it right with a quick look, emission of comments would need Jackson to associate comments with other node types to emit (like value that is written) next. |
I am very interested in this but am not sure I have the time to do a contribution. Thanks for keeping it alive. |
Even today, I am looking for this solution. Let me know if anyone finds any solution to achieve this |
Hi All, @asomov - I cannot access this https://bitbucket.org/asomov/snakeyaml-engine/pull-requests/1 .. Can you provide access to this ? Also you mentioned that snakeyaml is going to add support to dump comments - can you please point me to some reference related to this? |
Btw, I just found this -- https://bitbucket.org/snakeyaml/snakeyaml/issues/497/how-to-use-the-new-process-comments .. Havent tried it yet, but seems like something was done around this. Can you please confirm |
@nikhildigde SnakeYAML Engine migrated to the new home |
I think I did notice that SnakeYAML engine now has means to write (and probably also read?) comments. That would be a required part of the solution. But beyond that there'd still be the question of how to expose comments in Jackson content (f.ex as I think that conceptually comments should probably be associated as sort of metadata to actual content nodes but Jackson really has nothing to support such notion. Adding new value nodes would work for some comments but not nearly all. Supporting comment reading/writing at low level (streaming parsers/generators) is simpler, of course, and that may be the first thing to allow. It won't be super useful for most users but would at least let toolmakers do something. |
I am now using the underlying SnakeYAML to read/write commons (thanks @asomov !) My use case involves marking a translation as being proofread/checked, or capturing other metadata about the value. For example this shows that translator SC has looked at the Spanish: description:
en-US: Injury Cause
sw-TZ: Sababu ya Kuumiza
id-ID: Penyebab Cedera
es-SV: Causa de la lesión #SC Check ... and then these files are read via Jackson (thanks @cowtowncoder !) and become quite a comprehensive outcomes framework for monitoring & evaluation use cases. But there are other things that are driving me to use the lower level, like preserving any indentation or style that the last human used and omitting fields with defaults (Jackson writes |
@asomov Yes I later had found this repo and did a local build of the master. Seems working great! Thanks! Just a question, why did you not enhance SnakeYaml 1.1 impl rather than creating a new impl (engine). Intent of asking is to not have a BWC break and same experience |
To be able to improve it breaking the backwards compatibility |
Just adding a real world use case here. RAML documents use a shebang like identifier We're using Jackson's YAML merge functionality to build complete documents for OpenAPI and RAML. With RAML we need to "fixup" the merged YAML to include the RAML comment header. It would obviously be nice if comments could be preserved since we are only using |
While (typed) comments would be nice, another possibility could be -- if SnakeYAML/-engine supports this -- to pass through "raw" content. Currently it is handled in rather odd way; basically you need The reason this would be slightly easier (excluding part about SnakeYAML support) is that no new types would be needed. |
Hi, if I were interested in contributing to jackson to implement this (not just for yaml, but also for other formats as well), what would be the best way to go about starting this? |
@solonovamax I doubt there is any discussion outside of this issue (except for potentially discussion on XML backend issues, Challenges here include:
Still, tackling (1) would be the first step I think. It could potentially include adding |
|
OK back to something actually useful: How crazy would it be to support something like |
regarding the issues you raised, here's what I was thinking:
I'd ideally like to be able to implement global support across all generators for it.
I wasn't particularly considering databind as I would most likely not be using it for my specific usecase (kotlin delegate properties which store the name of their field) and, any formats which don't support comments would just ignore it. and, imo, there should be zero support for deserializing the comments into a POJO. comments are not data, only a bit of metadata, generally for a human reading it.
I don't particularly see them as being super useful to be exposed to the reader, as they're comments. the contents of them does not matter. however, I do think that they should be stored in the parsed AST ( I was thinking smth like
would return a list of
rather than hard-failing on attempting to write a comment in an unsupported format, it could soft-fail and just return without it being written. |
Yeah; I think there are many limited ways to support comments that could be useful. But we should start with the streaming write ( |
ah, hmm, that is actually a good point. |
Thinking out aloud, of course alternate direction wrt comments, |
…ml files This commit removes the yaml files formatting configuration block from the pom.xml file. There is a really unfortunate bug related to [FasterXML/jackson-dataformats-text#36](FasterXML/jackson-dataformats-text#36)
Is it please possible to emit comment lines with the keys? I could imagine this being done with an annotation for the respective class member. Also, adding whitespace, e.g. empty line would be a nice feature as well.
If this is not currently possible, could anyone please point me to the code where this magic would be happening so that I can add this feature?
Many thanks! :)
The text was updated successfully, but these errors were encountered: