-
Notifications
You must be signed in to change notification settings - Fork 212
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
Make AST package available for generic data manipulation #822
Comments
I'd love to support this. That kind of use case is exactly why I designed it with an intermediate AST as opposed to building structures straight from the parser. The main reason |
Sounds like a plan!
…________________________________
From: Thomas Pelletier ***@***.***>
Sent: Friday, October 7, 2022 11:20:54 PM
To: pelletier/go-toml ***@***.***>
Cc: Mike Farah ***@***.***>; Author ***@***.***>
Subject: Re: [pelletier/go-toml] Make AST package available for generic data manipulation (Issue #822)
I'd love to support this. That kind of use case is exactly why I designed it with an intermediate AST as opposed to building structures straight from the parser.
The main reason interal/ast is not public at the moment is that I haven't felt confident that its interface was stable enough to meet the backward compatibility guarantees as the rest of the go-toml package. Practically speaking it hasn't moved much recently, but still. If you're ok with me exposing it and trying to not break things on that API (but with no guarantees), I'm happy to!
—
Reply to this email directly, view it on GitHub<#822 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAIZHNM2626LZSVQ7YYVPG3WCAISNANCNFSM6AAAAAAQ7IHVQQ>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Diving deeper into this - my code is becoming a little unwieldy - it would help if there was a |
Also - can you make the comments available on your Node struct? |
Final question (sorry) - how do I write back out as a toml file using this lib? |
The way I've been doing it in the unmarshaler is to "stash" the expression, so that my next call to Line 568 in 58a592b
We could probably create a some tooling or abstraction over that. Happy to take ideas of what an interface over that would look like.
It's possible, but at the moment the parser skips over them (because unmarshaler wouldn't need it). I'm unclear how to represent them though.
Easiest today is to create a structure that represents the document you want to emit, and use |
Ideally it'd be something like parser.Peek() and it would return a Regarding the comments - the way the go-yaml parser handles them is by having 3 string fields on its Node struct: HeadComment, LineComment and FootComment. So something like # hi
a: cat # things
# blah the |
Thinking about being able to roundtrip a file with comments - I'd need some sort of way of re-encoding them 🤔 Would it be possible to expose an interface that encoded the Node interface you have at the moment? |
I'm also very interested this as I'm currently improve the encoding/decoding support in dasel. Good work here @mikefarah , thanks for taking it on. I too like the way go-yaml's nodes are exposed. |
Sorry it took me so long to respond -- life gets in the way. Regarding storing comments: we can probably do something similar as go-yaml, though we would need to clarify a few rules for cases like: a = 1
# is this comment a's foot or b's head?
b = 2
c = [ # what does this comment belong to?
# how about this one?
] but we may just wing it as we implement it. I'm not 100% sure about storing the comments directly in the It may be possible to add the support of encoding into the existing
Overall I think it's doable, and can be made good enough, and eventually we can probably get a fair amount of encoding code reused between this and the marshaler. |
@mikefarah @TomWright I might have time this coming weekend to take a stab at having the parser keep comments and expose them on the AST (no promises 😇 ). If you have any thoughts about the above (especially about the actual usefulness to preserve whitespace), it'd welcome it! |
Thanks @pelletier, I've definitely had comments raised with dasel that users would like whitespace to be preserved 👍 |
Sweet! Whitespace would be awesome. Not sure how it could be stored - perhaps similarly to HeadComment/FootComment, you could potentially have a Leading/Trailing whitespace string. In terms of writing back to toml - how would I go about doing that using the library? Once I've parsed the nodes from toml and the ast.Node package - they get converted to another data structure before being processed. So I need some sort of mechanism to convert it back to ast.Node (I guess..?) and pass it to some sort of encoder.... |
I'm going to play around with it and submit a PR to get your feedback. My goal is to be able to retain that information (comments and whitespaces) in a way that doesn't impact the performance of the unmarshaler.
Today there is no mechanism to encode |
I follow a similar pattern of unmarshaling into a custom structure, and then marshaling back into the original structure based on the target file format. If we could provide a |
Thanks @pelletier for tagging #860! |
Hi,
I'm the author of yq (github.com/mikefarah/yq) which is a popular command line tool for manipulating data files (yml, json, xml, properties). I'd love to add TOML support (mikefarah/yq#1364) - and this libary seems like it'd be a best fit...if the internal AST package was made public.
That way I could (attempt) to preserve comments, map key order etc.
Is this something you would consider? Happy to help if I can...
The text was updated successfully, but these errors were encountered: