-
Notifications
You must be signed in to change notification settings - Fork 326
internal/encoding/yaml: encode YAML anchors as CUE definitions #3987
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
base: master
Are you sure you want to change the base?
Conversation
If recursive anchors like that no longer fail, what CUE do they result in? |
You can see in the error above:
Still recursive, so it's invalid. But the encoder doesn't recurse infinitely. |
Ah I see. That's fine; I don't imagine these cases will actually show up in practice often. Indeed I think we avoided cycles because of endless recursion. |
On the one hand it makes no sense to emit an invalid CUE file. |
Fine, then I'll edit the test. |
d914607
to
80c4fa0
Compare
return nil, err | ||
} | ||
|
||
return d.addAnchorNodes(expr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than buffering these for the entire document and adding them at the very top, it would be better to add them closer to where they are used. if an anchor is declared in the middle of the document, we should place the definition in roughly the same spot, and not at the very start or end of the whole file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we discussed this when we create this issue several months ago.
Since CUE definitions are scoped, we can't place the anchor definition exactly at the scope of the original anchor, since there may be aliases outside this scope.
So there are two options to go about this, the simpler would be to lift all anchor definitions to the top-level of the CUE document. But then - is it any different than putting everything at the top / bottom?
The more complex solution would be to find the inner-most scope containing all anchor aliases. But since this code is recursive, doing so without buffering would be quite complex - is it worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, I don't mean to place them in an inner scope - the top level file scope is fine. I just mean placing them relatively close to their original position - in my example above, near the middle of the file - rather than at the start or end of the whole file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an implementation for this, but it's not really nice.
I can push this as a separate commit if you want to have a look.
Basically you have to add a recursion parameter to differentiate between when you're handling a top-level map as opposed to any other map.
IMO this just makes the code more complicated for little actual benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, I pushed the patch. But I think it's unwise to merge it like this.
If you have any suggestions for a cleaner implementation I'm up for it.
But in the current form, I think keeping things simple is more important than being marginally better.
0e46f6f
to
add7dce
Compare
I pushed some changes to make the tests pass. |
This commits supports encoding YAML documents such as: a: &a 3 b: *a To this CUE document: #a: 3 a: #a b: #a Fixes cue-lang#3818 Signed-off-by: Omri Steiner <[email protected]>
add7dce
to
33cc9e0
Compare
Alright, that should be sorted. Had a positioning issue where I accidentally called |
…re they're defined Signed-off-by: Omri Steiner <[email protected]>
@OmriSteiner the tests are failing, not sure if you saw that? |
Yes, I did.
I only pushed the additional commit for you to look at. If you think this is worth the tradeoff with implementation complexity, then I can fix the tests. Otherwise I think it's better if I remove the last commit here (and then the tests pass). |
Ah, I had misunderstood. And apologies for the slowness. Yes, I think your latest commit is a perfectly fine approach. If you can fix the tests, I'll do a final review :) |
Fixes #3818
There's one small issue open to handle. Tests are currently failing with:
I could change the code to match the old behavior of rejecting such YAML documents, since the resulting CUE document is invalid.
However, I think the original reason we error out for such document was that it would lead to endless recursion in the parser. This is no longer the case, so I'm debating whether the correct fix would be to error out, or change the test.