Skip to content

Commit

Permalink
breaking: Remove regal.json_pretty built-in function (#785)
Browse files Browse the repository at this point in the history
We never used it ourselves, and now that OPA has a built-in for this
there's even less need to keep this around. I doubt anyone relied on
it, but let's call it out in the next release notes anyway.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Jun 3, 2024
1 parent 6c91202 commit 83ae84a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 54 deletions.
17 changes: 0 additions & 17 deletions build/capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -6110,23 +6110,6 @@
"type": "function"
}
},
{
"name": "regal.json_pretty",
"decl": {
"args": [
{
"description": "data to marshal to JSON in a pretty format",
"name": "data",
"type": "any"
}
],
"result": {
"name": "output",
"type": "string"
},
"type": "function"
}
},
{
"name": "regal.last",
"decl": {
Expand Down
8 changes: 4 additions & 4 deletions docs/custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ Works just like `rego.parse_module`, but provides an AST including location info
by Regal, like the text representation of each line in the original policy. This is useful for authoring tests to assert
linter rules work as expected.

### `regal.json_pretty(data)`
### `regal.last(array)`

Printing nested objects and arrays is quite helpful for debugging AST nodes, but the standard representation — where
everything is displayed on a single line — not so much. This built-in allows marshalling JSON similar to `json.marshal`,
but with newlines and spaces added for a more pleasant experience.
This built-in function is a much more performant way to express `array[count(array) - 1]`. This performance difference
is almost always irrelevant in "normal" Rego policies, but can have a significant impact in linter rules where it's
sometimes called thousands of times as part of traversing the input AST.

In addition to this, Regal provides many helpful functions, rules and utilities in Rego. Browsing the source code of the
[regal.ast](https://github.com/StyraInc/regal/blob/main/bundle/regal/ast/ast.rego) package to see what's available is
Expand Down
4 changes: 0 additions & 4 deletions internal/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ func Capabilities() *ast.Capabilities {
Name: builtins.RegalParseModuleMeta.Name,
Decl: builtins.RegalParseModuleMeta.Decl,
},
&ast.Builtin{
Name: builtins.RegalJSONPrettyMeta.Name,
Decl: builtins.RegalJSONPrettyMeta.Decl,
},
&ast.Builtin{
Name: builtins.RegalLastMeta.Name,
Decl: builtins.RegalLastMeta.Decl,
Expand Down
28 changes: 0 additions & 28 deletions pkg/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ var RegalParseModuleMeta = &rego.Function{
),
}

// RegalJSONPrettyMeta metadata for regal.json_pretty.
var RegalJSONPrettyMeta = &rego.Function{
Name: "regal.json_pretty",
Decl: types.NewFunction(
types.Args(
types.Named("data", types.A).Description("data to marshal to JSON in a pretty format"),
),
types.Named("output", types.S),
),
}

// RegalLastMeta metadata for regal.last.
var RegalLastMeta = &rego.Function{
Name: "regal.last",
Expand Down Expand Up @@ -101,16 +90,6 @@ func RegalLast(_ rego.BuiltinContext, arr *ast.Term) (*ast.Term, error) {
return arrOp.Elem(arrOp.Len() - 1), nil
}

// RegalJSONPretty regal.json_pretty, like json.marshal but with pretty formatting.
func RegalJSONPretty(_ rego.BuiltinContext, data *ast.Term) (*ast.Term, error) {
encoded, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, err
}

return ast.StringTerm(string(encoded)), nil
}

// TestContextBuiltins returns the list of builtins as expected by the test runner.
func TestContextBuiltins() []*tester.Builtin {
return []*tester.Builtin{
Expand All @@ -121,13 +100,6 @@ func TestContextBuiltins() []*tester.Builtin {
},
Func: rego.Function2(RegalParseModuleMeta, RegalParseModule),
},
{
Decl: &ast.Builtin{
Name: RegalJSONPrettyMeta.Name,
Decl: RegalJSONPrettyMeta.Decl,
},
Func: rego.Function1(RegalJSONPrettyMeta, RegalJSONPretty),
},
{
Decl: &ast.Builtin{
Name: RegalLastMeta.Name,
Expand Down
1 change: 0 additions & 1 deletion pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ func (l Linter) prepareRegoArgs(query ast.Body) ([]func(*rego.Rego), error) {
rego.ParsedQuery(query),
rego.ParsedBundle("regal_eval_params", &dataBundle),
rego.Function2(builtins.RegalParseModuleMeta, builtins.RegalParseModule),
rego.Function1(builtins.RegalJSONPrettyMeta, builtins.RegalJSONPretty),
rego.Function1(builtins.RegalLastMeta, builtins.RegalLast),
)

Expand Down

0 comments on commit 83ae84a

Please sign in to comment.