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

Partial object shorthand #412

Closed

Conversation

mmussomele
Copy link
Contributor

No description provided.

Rego already supports syntax for declaring constants in modules,
parsing them as lone Bodies and then transforming them into rules
with a Body of `true`. This patch adds similar behavior for partially
defined object documents, allowing keys of the document to be defined
as module level constants.

As an example, instead of writing:

    package foo

    bar["x"] = 1 { true }
    bar["y"] = 2 { true }

One could omit the Bodies and just write:

    package foo

    bar["x"] = 1
    bar["y"] = 2

This is limited to a depth of one, that is, a constant like
`bar["x"]["y"] = 1` is not supported.
@mmussomele mmussomele changed the title Partial bodies Partial object shorthand Aug 11, 2017
@tsandall
Copy link
Member

I'm going to use these changes as the basis for a new PR that also includes support for partial set shorthand. I frequently find myself wanting a convenient way to incrementally define sets. /cc @mmussomele

@tsandall tsandall closed this Aug 24, 2017
tsandall added a commit to tsandall/opa that referenced this pull request Aug 24, 2017
These changes allow partial docs to be defined without a body in Rego
source files. Before, the rules would have to include a `{true}` body
for the parser to allow them. Now, the body can be omitted.

Rules defined this way (inside modules) cannot be copy/pasted as-is into
the REPL. This could be addressed by creating a "paste mode" in the REPL
similar to ipython and other interactive shells.

These changes build on open-policy-agent#412
with a few differences:

- Dynamic values are allowed in the head.
- Partial sets are allowed.

Both of these changes are based on personal experience writing policy.
Dynamic values are fine to allow as the compiler will catch unsafe vars
and rewrite the head to handle refs and comprehensions.
tsandall added a commit that referenced this pull request Aug 24, 2017
These changes allow partial docs to be defined without a body in Rego
source files. Before, the rules would have to include a `{true}` body
for the parser to allow them. Now, the body can be omitted.

Rules defined this way (inside modules) cannot be copy/pasted as-is into
the REPL. This could be addressed by creating a "paste mode" in the REPL
similar to ipython and other interactive shells.

These changes build on #412
with a few differences:

- Dynamic values are allowed in the head.
- Partial sets are allowed.

Both of these changes are based on personal experience writing policy.
Dynamic values are fine to allow as the compiler will catch unsafe vars
and rewrite the head to handle refs and comprehensions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants