The OPA parser places a single boolean true term inside an expression
to populate empty rule bodies, so that e.g. `x := input.x` parses as:
```rego
x := input.x if {
true
}
```
When the compiler rewrites rules in its various stages, it would
previously ignore such "true expressions" and simply append what
it rewrote as the next expression:
```
x := __local1__ if {
true
__local1__ = input.x
}
```
This is of course redundant, as the only purpose of the `true` expr
was to mark the body as empty — which it no longer is! While fairly
harmless, this contributes noise to the compiler's output, which is
then passed onwards to topdown evaluation. Not a huge deal, but
measurable!
**BenchmarkRegalLintingItselfPrepareOnce**
```
502062750 ns/op 1900946509 B/op 45426050 allocs/op
496509250 ns/op 1893094357 B/op 45112660 allocs/op
```
With this change, the compiler now replaces an empty true expression with
a new one whenever we append to a body. Meaning `true` remains to mark
empty bodies, but not in non-empty ones.
This is more about elegance than performance though :)
Signed-off-by: Anders Eknert <anders.eknert@apple.com>
Add rego.Data function to allow setting data directly from a
map[string]any, providing a simpler alternative to using
Store(inmem.NewFromObject(data)). This improves the Go SDK API
by reducing boilerplate for the common case of using an in-memory
store with static data.
Fixes: #5961
Signed-off-by: majiayu000 <1835304752@qq.com>
Go 1.23 is no longer supported as per Go release policy.
Changes:
- Use Go v1.24.6 as the project SDK requirement
- Apply lint fixes for Go 1.24
- Fix "non-constant format string in call" issues as seen in CI.
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
Not so much an optimiziation (although a tiny one) as it is
fixing something that annoyed me :) Don't create new types when
we have interned alternatives available.
Note that `NewAny` can't always be replaced with `A`, like when
provided as arg expecting `[]Type` — as `A` is boxed to `Type`.
Signed-off-by: Anders Eknert <anders@styra.com>
Following up on #7566, and now applying the more exciting
modernizations. fmt.Appendf was new to me! But especially
the contains checks are so much better IMHO. I have reviewed
all changes myself and did a few manual changes where it
became obvious that things could be improved a little further.
(the modernize analyzer still has some issues running against
OPA, and I have manually worked around those for the time being)
Signed-off-by: Anders Eknert <anders@styra.com>
to allow for using Rego v1 bundles in `opa build`/`check`/`eval`/`test`.
Before this change, a bundle with `1` as `rego_version`/`file_rego_versions` would be rejected when evaluated with the `--v0-compatible` flag with the error:
```
rego_parse_error: illegal capabilities: rego_v1 feature required for parsing v1 Rego
```
This is fixed by adding the `rego_v1` feature to the `v0` default capabilities applied when using the `--v0-compatible` flag. Note: this allows OPA to accept Rego `v1` modules inside bundles, but modules without a specified Rego version, such as freestanding non-bundle modules or modules inside bundles with no specified Rego version, are parsed as `v0`.
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Brace yourselves! For there are many touched files here. No changes
in semantics however.
Spent a long time trying out the various optional rules gocritic
provides, and settled for a few of them. There are more I really
like, but that would take many hours to address across the codebase.
Perhaps others find gocritic too pedantic? If so, we can merge the
fixes without enabling the rule.
Signed-off-by: Anders Eknert <anders@styra.com>
And update code to conform to the rule.
- Replace unnecessary fmt.Sprintf with string concatenation
- Replace fmt.Sprint with more efficient strconv.Itoa
- Replace static fmt.Errorf calls with more efficient errors.New
Thanks @srenatus for pushing me down this rabbit hole!
Signed-off-by: Anders Eknert <anders@styra.com>
All packages, except for `cmd` and `internal`, have been moved into a new `v1` root package.
Old packages are kept for backwards-compatibility reasons. All contained code is replaced with simple type aliases and proxy functions to `v1` implementations.
Old packages default to the Rego v0 syntax, new `v1` packages default to the Rego v1 syntax.
Signed-off-by: Johan Fylling <johan.dev@fylling.se>