Skip to content
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

Confusing semantics when adding if ... to a partial set rule #6184

Closed
anderseknert opened this issue Aug 28, 2023 · 5 comments
Closed

Confusing semantics when adding if ... to a partial set rule #6184

anderseknert opened this issue Aug 28, 2023 · 5 comments

Comments

@anderseknert
Copy link
Member

From the perspective of a clueless indivdual — and that would be me :) — it is difficult to understand the semantics of these expressions, and how adding if true to a rule head would alter the value/type of the rule(!)

a["b"]

As expected, this creates a set containing the value "b".

a["b"] if true

This creates a map with the key b and the value true. That is... not what I expected, as to me the if in a["b"] if true would be syntax sugar for a["b"] { true }, which apparently it isn't.

Now, I'm well aware that using contains would be the correct way to work with partial sets in ref-head rules, but given how that came out of backwards compatibility concerns, and if also is a new keyword, it's hard to imagine backwards compatibility being the problem here.

The formatter also rewrites a["b"] if true -> a["b"] = true which to me is not semantically equivalent. I've tried finding anything on this in the docs but I've come up short.

@anderseknert
Copy link
Member Author

A side effect of this — a trick we use in Regal to distinguish:

a { true }

from

a = true { true }

Is to simply check the rule head value for the presence of a location attribute. Only in the latter case will there be a location, since in the former case the implied assignment is "generated" and cannot be traced to a location in the source code.

This "trick" however breaks for the beforementioned case, as a location for the value is added for the true value, despite no value being present on that location in the actual source code:

a["b"] if true
opa parse --format json --json-include locations p.rego
{
  "head": {
    "value": {
      "location": {
        "file": "p.rego",
        "row": 5,
        "col": 1
      },
      "type": "boolean",
      "value": true
    }
  }
}

@stale
Copy link

stale bot commented Sep 27, 2023

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days. Although currently inactive, the issue could still be considered and actively worked on in the future. More details about the use-case this issue attempts to address, the value provided by completing it or possible solutions to resolve it would help to prioritize the issue.

@stale stale bot added the inactive label Sep 27, 2023
@tsandall
Copy link
Member

Can we close this in favor of #6247?

@anderseknert
Copy link
Member Author

Sure. Just so that I understand, while we will require contains and if, we don't want to require if for a rule without conditions, right? Provided the original example, I'd expect this:

a["b"]

to become this

a contains "b"

Without an if added / required. Is that correct?

@tsandall
Copy link
Member

Yes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants