Commit Graph

15 Commits

Author SHA1 Message Date
Stephan Renatus 0d7e509613 ci: bump golangci-lint (v2.9.0), fix issues
https://github.com/golangci/golangci-lint/releases/tag/v2.9.0

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2026-02-12 08:42:05 +01:00
Anders Eknert 55683c743c compiler: replace true expr when appending to empty body (#8299)
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>
2026-02-06 10:06:48 +00:00
lif d271cdfff8 rego: Add Data function to simplify adding data from map (#8166)
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>
2026-01-08 22:02:23 +00:00
Ville Vesilehto f77322b3fb build: bump Go version requirement to 1.24 (#7839)
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>
2025-08-24 09:02:09 +02:00
Stephan Renatus 52381423d3 test+eval: add helper to smuggle compiler through context
Signed-off-by: Stephan Renatus <stephan@styra.com>
2025-07-23 22:12:13 +02:00
Anders Eknert f8d5b50a10 Use shorthand form for types (#7757)
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>
2025-07-09 12:23:39 +03:00
Anders Eknert 8ba08ac80c Apply modernize linter fixes (#7599)
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>
2025-05-20 23:12:13 +02:00
Anders Eknert e43ef0a979 Use any in place of interface{} (#7566)
Earlier this evening I tried to run the Go
[modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize)
analyzer on OPA. That didn't go as planned:

- https://github.com/golang/go/issues/73661
- https://github.com/golang/go/issues/73663

While we wait for that to be fixed, I figured an old-fashioned
search-and-replace across the repo may work for at least the
`interface{}` to `any` conversion. That should help make it easier
to see the other fixes as applied by the modernize tool once it has
had those issues resolved.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-05-12 13:57:48 +02:00
Johan Fylling 93c177449c ast: Adding rego_v1 feature to --v0-compatible capabilities (#7474)
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>
2025-04-03 11:30:53 +02:00
Anders Eknert afb30d3f9d Add gocritic linter, fix a bunch of stuff (#7377)
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>
2025-02-24 16:28:41 +01:00
Anders Eknert 55e87e79ae Add perfsprint linter (#7334)
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>
2025-01-31 20:24:05 +01:00
Anders Eknert b942136a4a Use Go 1.22+ int ranges (#7328)
With "some" help from `golangci-lint run --fix ./...`

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-30 09:57:27 +01:00
Johan Fylling 563321d26b Rego v1 capabilities and keywords update (#7216)
* Separating v0- and v1 keywords
* Adding `rego_v1` capability feature

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-17 11:50:11 +01:00
Johan Fylling a179a24c48 v1 API
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>
2024-12-12 15:27:34 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00