-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
display or rename issue with partial eval result #2053
Comments
Yep, this needs to be fixed. I'm not immediately sure what the best option is. If we change the String() function on variables, we lose the ability to roundtrip policies using tools like |
Brainstorming some options:
Digging more into changes for option (3), my favorite so far...
@tsandall Thoughts? |
Reading through the implementation a little more. It seems like we can maybe do a couple different things WRT what I outlined in my previous comment.. The |
The formatter would normally just use the stringer for ast.Var which would swap in `_` for any wildcard variables (internally represented with a `$xx` syntax). This works fine except for AST dumped into the formatter that might have the same wildcard variable used multiple times. This can be seen by using partial evaluation creating multiple statements from a single original source. In the formatted output if we swap in `_` it can affect the resulting logic if they were supposed to be the same variable. The formatter now will check for any wild cards that show up >1 time in the AST passed in to be formatted. Any it finds will be assigned a new variable name like `__wilcardxx__` and in the resulting output will use that name instead of the `_` syntax. Fixes: open-policy-agent#2053 Signed-off-by: Patrick East <east.patrick@gmail.com>
The formatter would normally just use the stringer for ast.Var which would swap in `_` for any wildcard variables (internally represented with a `$xx` syntax). This works fine except for AST dumped into the formatter that might have the same wildcard variable used multiple times. This can be seen by using partial evaluation creating multiple statements from a single original source. In the formatted output if we swap in `_` it can affect the resulting logic if they were supposed to be the same variable. The formatter now will check for any wild cards that show up >1 time in the AST passed in to be formatted. Any it finds will be assigned a new variable name like `__wilcardxx__` and in the resulting output will use that name instead of the `_` syntax. Fixes: #2053 Signed-off-by: Patrick East <east.patrick@gmail.com>
@patrick-east thank you! 😃 |
(This is a bit of a spin-off of #2045.)
Running
opa eval 'data.test.allow' --format=pretty -d test.rego -p
withtest.rego
as(it doesn't matter if you use
a = input.arr[_]
ora := input.arr[_]
) -- the result isNow, that would violate the property that
eval(partial_eval(policy), input) == eval(policy, input)
, which one might expect to hold.However, looking at the JSON output, we find
and this probably means that the two
_
in the pretty output really are meant to be the same variable, i.e.Long story short, if the pretty version of something like this is requested, OPA should not translate the
$01
into_
, as it breaks stuff. 😉Also, if the rego is changed to
a = input.arr[x]
, the pretty output becomesThis suggests that the right thing to do in this situation, facing
$01
, might be to introduce a fresh variable. What do you think?The text was updated successfully, but these errors were encountered: