opa icon indicating copy to clipboard operation
opa copied to clipboard

Allow user-defined zero-argument functions in Rego

Open johanfylling opened this issue 2 years ago • 6 comments

It's possible to declare functions with zero arguments in Rego:

package example

foo() := 1

these are however treated as regular rules, and there is no semantic difference between the above policy and:

package example

foo := 1

This is cause for confusion, as the user likely expects foo() to behave as a function (e.g. not contribute to output document), but it will actually behave as a rule.

Alternatively, it should be a parser error to declare a zero-arg rule.

johanfylling avatar Oct 13 '23 12:10 johanfylling

Alternatively, it should be a parser error to declare a zero-arg rule.

I would suggest taking a stand here instead. The title of this issue suggests as much :)

anderseknert avatar Oct 13 '23 21:10 anderseknert

IMO unless we think there is an actual need for user-defined zero-argument functions, this should be a parser error.

ashutosh-narkar avatar Oct 13 '23 21:10 ashutosh-narkar

I would suggest taking a stand here instead. The title of this issue suggests as much :)

My intention was to bring this up for discussion. But in retrospect, I should have done that as a comment, and not as part of the description.

johanfylling avatar Oct 13 '23 21:10 johanfylling

Yeah, no worries @johanfylling 🙂

@ashutosh-narkar seeing these used in policy libraries working on Regal integrations is what brought this up. Longer discussion on Slack, but since that's only retained for 90 days, motivation summarized for posterity. The arguments for allowing zero-arity functions:

  1. Not having to explain why zero-arity functions won't work, as there isn't really a technical reason.
  2. Consistency with built-in functions (opa.runtime(), rego.metadata.rule(), etc)
  3. Allows “masking”, or making values "private" when evaluated as part of larger document.

anderseknert avatar Oct 14 '23 02:10 anderseknert

Thanks for the context @anderseknert! This list looks reasonable.

ashutosh-narkar avatar Oct 16 '23 17:10 ashutosh-narkar

Make sure to add #6314 as a testcase for when this gets implemented.

anderseknert avatar Nov 08 '23 10:11 anderseknert