13 Commits

Author SHA1 Message Date
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 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
Stephan Renatus 09dd80b3d2 topdown+ast+rego: read lazy objects from the store (#5380)
Before, any call to a builtin that would reference an object from data would force that
the entire object was transformed to an ast.Value, and a copy of it was passed to the builtin.

Now, we're doing something a little more involved: when an object is read from data, we'll
wrap it into a special container and don't convert it. The conversion only happens when it's
required. As such, we can avoid quite a bit of memory usage, and avoiding the extra work,
make evaluations faster.

Lazy objects are also immutable -- calling Insert() on them will panic. But based on how
topdown works, it should never happen. Only Golang coding modifying the results of a query
evaluation could be affected by this. If you are, see the opt-out mechanisms outlined below.

⚠️ The benchmark that follows is an edge case, but the issue at hand here has come up in
the real world before.

    name                         old time/op    new time/op    delta
    MemberWithKeyFromBaseDoc-16    40.2ms ±13%     0.0ms ± 5%  -99.97%  (p=0.000 n=9+10)
    ObjectGetFromBaseDoc-16        42.4ms ± 4%     0.0ms ± 2%  -99.97%  (p=0.000 n=9+10)

    name                         old alloc/op   new alloc/op   delta
    MemberWithKeyFromBaseDoc-16    17.2MB ± 0%     0.0MB ± 0%  -99.96%  (p=0.000 n=10+10)
    ObjectGetFromBaseDoc-16        17.2MB ± 0%     0.0MB ± 0%  -99.95%  (p=0.000 n=10+10)

    name                         old allocs/op  new allocs/op  delta
    MemberWithKeyFromBaseDoc-16      702k ± 0%        0k ± 0%  -99.98%  (p=0.000 n=10+10)
    ObjectGetFromBaseDoc-16          702k ± 0%        0k ± 0%  -99.98%  (p=0.000 n=10+10)

Note: The optimization is enabled by default, and for all storage implementations. However,
there are multiple layers of opt-out options, in case this causes any unforeseen trouble for
your usage of OPA via Golang. (OPA-as-server use cases don't need to worry about this.)

Concretely, these opt-out options are:

1. an option for `topdown.Query` to not start with lazy objects in the first place
2. an option for `ast.JSONWithOpt` to revert to the old behaviour when converting to golang native values
3. an `EvalOption` for `rego.PrepareForEval(...).Eval(...)` so that it keeps copying the maps it adds to the result set

Fixes #5325.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-11-16 19:43:51 +01:00
Torin Sandall 658ae9fe75 Refactor AST type name strings
Previously we had constants defined for AST type names. These were used
in error messages in various places. The original goal was to make error
messages consistent, however, this approach made it difficult to locate
the source of the error in code.
2018-02-09 16:38:34 -08:00
Torin Sandall ab91356d5b Add separate assignment and equals operators
These changes add separate infix operators for assignment and equals
(from equality/unification which was previously used for all three.)
With assignment, authors can declare local variables that will shadow
globals.
2018-01-27 17:06:42 -08:00
Torin Sandall 02e68689da Add support for calls as values
These changes allow calls to be nested inside terms (e.g., f(x) !=
g(x)). As part of these changes a few things have been refactored:

1) Grammar has been restructured so that construction code is pulled out
into a separate file. Hopefully this makes the grammar more readable.

2) String() implementation on Expr has been simplified to use prefix
notation for calls (except equality) as this avoids the challenge of
worrying about roundtripping policy strings (which is done frequently
inside test cases.) E.g., plus(x,1,y) converted to infix x + 1 = y would
parse to eq(x + 1, y).
2018-01-26 18:05:05 -08:00
Torin Sandall 7ca542adb5 Refactor functions implementation
Previously, functions were implemented with a separate set of types that
had their own code paths in the compiler, eval, etc. These changes
refactor the function implementation so that functions are implemented
as rules with one or more arguments.

By representing functions as rules, we can avoid special casing required
to support functions, e.g., during parse and compile there are a number
of steps that required special casing for functions:

- Parser needed separate grammar definitions for functions (which
  prevented them from being chained or using else)

- Compiler needed separate resolver and type checker implementations
  which was a source of bugs.

In some cases, special casing is unavoidable for now (e.g., during eval)
however this could be improved in the future.

Fixes #471
Fixes #467
Fixes #463
2017-10-10 08:57:58 -07:00
Matthew Mussomele 389d681388 Update ast to support user functions 2017-07-05 13:45:55 -07:00
Torin Sandall 5906ea302b Update ast to support with modifier 2017-02-03 08:39:35 -08:00
Torin Sandall 7fa8a1e311 Refactor to use snake_case in API models
Also, move server types into file separate from server.

Fixes #222
2017-01-20 09:30:09 -08:00
Torin Sandall 699e45db20 Improve module parsing errors
Fixes #213
2017-01-18 12:22:37 -08:00
Torin Sandall 3640081e29 Add utility for AST node names 2016-12-19 09:51:28 -08:00