Skip to content
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

Indented/"pretty-printed" JSON marshaling #6630

Closed
sean-r-williams opened this issue Mar 14, 2024 · 3 comments · Fixed by #6636
Closed

Indented/"pretty-printed" JSON marshaling #6630

sean-r-williams opened this issue Mar 14, 2024 · 3 comments · Fixed by #6636

Comments

@sean-r-williams
Copy link
Contributor

What is the underlying problem you're trying to solve?

We're using SpaceLift for automating Terraform management. SpaceLift uses OPA for notification policies, which determines when (and what) to comment on PRs to SpaceLift-connected repos.

Presenting contextual plan output (i.e. new resource state) is easiest by serializing the state object to JSON. (This is what one of the examples in the Spacelift docs suggests.) However, both interpolating the object via sprintf or using json.marshal both produce compressed, single-line JSON. This is hard for human readers to interpret.

Describe the ideal solution

Add an optional Boolean parameter to json.marshal() (defaulting to false) that indicates the resultant JSON string should be indented.

pretty = json.marshal(objectFoo, true)
normal = json.marshal(objectFoo)
alsoNormal = json.marshal(objectFoo, false) 

Describe a "Good Enough" solution

Add a new built-in (e.g. json.marshal_indent() that functions identically to json.marshal, but with indented/multi-line output.

Additional Context

@ashutosh-narkar
Copy link
Member

Updating the existing builtin would be backwards-incompatible. A new json.marshal_indent() seems like a good option. Feel free to contribute if you'd like.

@sean-r-williams
Copy link
Contributor Author

@ashutosh-narkar Does Rego/OPA support polymorphic functions/optional parameters? Part of the rationale behind adding a new optional parameter was so any existing [one-parameter] calls to json.marshal() wouldn't be affected.

@ashutosh-narkar
Copy link
Member

Does Rego/OPA support polymorphic functions/optional parameters?

Currently not. Only print is a variadic builtin function.

johanfylling pushed a commit that referenced this issue Apr 5, 2024
…ty-printed" and/or line-prefixed JSON (#6636)

Fixes #6630

Signed-off-by: Sean Williams <72675818+sean-r-williams@users.noreply.github.com>
tsidebottom pushed a commit to tsidebottom/opa- that referenced this issue Apr 17, 2024
…ty-printed" and/or line-prefixed JSON (open-policy-agent#6636)

Fixes open-policy-agent#6630

Signed-off-by: Sean Williams <72675818+sean-r-williams@users.noreply.github.com>
Signed-off-by: Thomas Sidebottom <thomas.sidebottom@va.gov>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants