open-policy-agent/opa

`rego.v1` enforces `if` before chained rule bodies, not allowed by parser

johanfylling opened this issue · 4 comments

The policy:

package play

import rego.v1

p if {
	input.x == 1
} {
	input.x == 2
}

will produce error:

1 error occurred: policy.rego:7: rego_parse_error: `if` keyword is required before rule body

However, updating the policy by adding an if to the second body:

package play

import rego.v1

p if {
	input.x == 1
} if {
	input.x == 2
}

will produce error:

1 error occurred: policy.rego:7: rego_parse_error: unexpected if keyword
	} if {
	  ^

Three possible solutions:

  1. Don't require if on chained bodies despite rego.v1 import
  2. Update parser to allow if on chained bodies
  3. Don't allow chained bodies in 1.0

2 has added benefit of clarity (depending on view, of course):

p if {
	input.x == 1
} if {
	input.x == 2
}

as the added if makes the separation of the bodies somewhat more obvious.

3 might be too obtrusive if this is a commonly used pattern.

If we believe it's a commonly used pattern, would option 1 be better? You can make the argument that the first if applies to the chain maybe.

Agreed — since the purpose of chained bodies is likely compactness, I feel like it's fine to only require if in the visible rule head. So I vote for 1 too.

Alright, so far it looks like no 1 has it 👍.