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
o2k: reorder object members to prettify the yaml output #3340
Conversation
wrt failing CI; tried a bunch of things but couldn't get past Flow. Generation all works when testing manually. |
k, managed to fix the linter issues. all green now. |
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.
Lookin' good to me! We'll come back to merge this after code-freeze for TS lifts 🙏🏽
(I updated to TypeScript for you) |
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 just want to drop here that I fundamentally do not approve of doing something like this. Objects are explicitly unordered in JavaScript, so we need to find another way to accomplish this.
An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function.
Furthermore, it's also explicitly unordered in YAML:
The content of a mapping node is an unordered set of key: value node pairs, with the restriction that each of the keys is unique.
While some JavaScript engines respect insertion order, almost by accident of implementation, others do not (for example, the engine used in safari). I've encountered bugs in real-world applications in the past due to changes precisely like this.
But at the same time, I can definitely see the value of something like this for creating deterministic output files. I think, even in that case, we should explore using something like https://www.npmjs.com/package/json-stable-stringify in place of our JSON.stringify
calls. That way, we don't have to depend on developers remembering to sort at every which-place, and it greatly reduces room for future error.
I'm happy to help with this, lemme know.
CORRECTION: As of ECMAScript 2020 this has been standardized https://github.com/tc39/proposal-for-in-order I thought maybe this would throw it off but it doesn't: All the same, I can't find this exact proposal listed anywhere or in v8 release notes but https://kangax.github.io/compat-table/es2016plus/ is the closest thing I can find, and judging by this it will be at least until Node 16 (note, we're on Node 10 now) that we can consider this spec applicable to our codebase. That means the above holds, although only for another few months or years, thankfully. On the other hand, v8 already worked this way (it was JavaScript Core, used in Safari, that didn't) so maybe it's fine. I donno. The other thing is, this is hard-ish to test:
We would, after all, really want some tests for this if we are going to start caring about it and this PR introduces no tests. I'm happy to help with this. I think with the stable stringify approach we can make it happen and I'd be happy to contribute some tests or fix all existing tests (we'd likely want/have to many many of them). |
Looks like YAML stringify has an option to sort keys |
The goal here was not to sort, but make it human friendly, hence the PR only defines order for the first entries, and the last entries. Using a sorted output (basically canonicalization) doesn't make sense to me. No parser should ever rely on order. Any follow steps in a ci/cd pipeline will most likely mess it up again. So the question is; is there a use case for canonicalized output? (my 2cts; no) if yes; then we skip human friendly output |
@develohpanda as an aside for later, re We needed to upgrade to at least 1.8 to get the |
@Tieske Can I close this one out? |
The o2k output has the object members in random order, depending on where they were generated. This adds a reordering that prettifies the final yaml output to be more readable.
fixes INS-671