forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
topdown: Add
json.marshal_with_options()
builtin for indented/"pret…
…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>
- Loading branch information
1 parent
322ad38
commit 18af050
Showing
6 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
test/cases/testdata/jsonbuiltins/test-json-marshal-with-options.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
--- | ||
cases: | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([1234500000 + 67890, 1e6 * 2, 1e109 / 1e100], {"indent": " "}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-explicit-indent | ||
query: data.test.p = x | ||
want_result: | ||
- x: |- | ||
[ | ||
1234567890, | ||
2000000, | ||
1000000000 | ||
] | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([1234500000 + 67890, 1e6 * 2, 1e109 / 1e100], {}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-empty-object | ||
query: data.test.p = x | ||
want_result: | ||
- x: "[1234567890,2000000,1000000000]" | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([1234500000 + 67890, 1e6 * 2, 1e109 / 1e100], {"pretty": true}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-defaults | ||
query: data.test.p = x | ||
want_result: | ||
- x: |- | ||
[ | ||
1234567890, | ||
2000000, | ||
1000000000 | ||
] | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([1234500000 + 67890, 1e6 * 2, 1e109 / 1e100], {"pretty": false, "prefix": "NO!", "indent": "BAD!"}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-explicit-disable | ||
query: data.test.p = x | ||
want_result: | ||
- x: "[1234567890,2000000,1000000000]" | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([1234500000 + 67890, 1e6 * 2, 1e109 / 1e100], {"prefix": "JSON => "}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-prefix | ||
query: data.test.p = x | ||
want_result: | ||
- x: |- | ||
JSON => [ | ||
JSON => 1234567890, | ||
JSON => 2000000, | ||
JSON => 1000000000 | ||
JSON => ] | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options({"foo": "bar", "bar": "baz"}, {"prefix": "JSON => "}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-object | ||
query: data.test.p = x | ||
want_result: | ||
- x: |- | ||
JSON => { | ||
JSON => "bar": "baz", | ||
JSON => "foo": "bar" | ||
JSON => } | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([], {"indent": " ", "prefix": "---"}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-empty-array | ||
query: data.test.p = x | ||
want_result: | ||
- x: |- | ||
---[] | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([[[[[[[]]]]]]], {"indent": " "}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-deep-array | ||
query: data.test.p = x | ||
want_result: | ||
- x: |- | ||
[ | ||
[ | ||
[ | ||
[ | ||
[ | ||
[ | ||
[] | ||
] | ||
] | ||
] | ||
] | ||
] | ||
] | ||
- data: {} | ||
modules: | ||
- | | ||
package test | ||
p = x { | ||
x := json.marshal_with_options([], {"indent": " ", "include_winning_lottery_numbers": true}) | ||
} | ||
note: jsonbuiltins/marshal_with_options-invalid-key | ||
query: data.test.p = x | ||
strict_error: true | ||
want_error: object contained unknown key "include_winning_lottery_numbers" | ||
want_error_code: eval_type_error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters