Support newline-delimited JSON and fix YAML multi-doc vs. sequence #1168
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First of all, sorry for this unsolicited PR!
The changes here fix an inconsistency when handling YAML multi documents, such as
vs. a single YAML document which is a sequence on top-level (resulting in a config which can accidentally be cast to
[]any):Due to an unfortunate cast to
[]anyin theenginepackage, this situation was handled incorrectly and it was impossible to run Rego validations on those fundamentally different structures correctly, as both were handled as the same config inputs to Rego policy"a"and"b". With the change, the inputs stay for the multi-doc (first case), but changes to the correct single input["a", "b"]for the single-document (second case).Also, newline-delimited JSON (
ndjsonextension) can be seen as some multi-document JSON, and I've adjusted the existing JSON parser similarly to the YAML parser such that multiple documents in one file are parsed as[]anywithlength>1.Such
ndjsoninput is convenient whenconftesting logs from sources. I've added an example for this.In fact, there's not even an
\nas delimiter needed, as we just continue parsing documents until we encounter EOF.This required some larger changes across all parsers, and I hope you see the benefit of this change.
Most importantly, the code in
engineis streamlined and does not cast the parsed configurations, leading to the initially described confusion with YAML.I've also took the chance and simplified the
formatcommand, which now consistently uses two spaces instead of tab for separation (this was previously depending on whether--combinewas used or not).I could also offer to make the tests a bit more beautiful using the famous
testifypackage. Would you accept/merge this as well?Let me know what you think!