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

ast: parse import without package in resolveRefs #3240

Conversation

onelittlenightmusic
Copy link
Contributor

Before, resolveRefs ignored Imports when Package is nil.
This caused opa eval --import option to be ignored without --package option.

This PR makes query compiler generate temporary package with name of ""
(empty string) when package is nil but import one or many imports are provided.

For example, we have two files.

filter1.rego

package f

getFamily(i) = { rtn |
  rtn := i.characters[_].family
}

getChildren(family) = { rtn |
  rtn := family[_].children
}

data.json

{
  "characters": [
    {
      "id": 0,
      "name": "Marty McFly",
      "cast": "Michael J. Fox",
      "family": {
        "children": [
          "Marty McFly Jr.",
          "Marlene McFly"
        ]
      },
      "occupation": "student"
    }
  ]
}

Before, we had this error because of no --package option.

opa eval -I --format=pretty -d filter1.rego --import 'data.f' 'f.getChildren(f.getFamily(input))' < data.json
2 errors occurred:
1:15: rego_type_error: undefined function f.getFamily
1:1: rego_type_error: undefined function f.getChildren

After this PR, we can use --import option without any explicit --package option.

opa eval -I --format=pretty -d filter1.rego --import 'data.f' 'f.getChildren(f.getFamily(input))' < data.json
[
  [
    "Marty McFly Jr.",
    "Marlene McFly"
  ]
]

Fixes #3228

Signed-off-by: Hiro Osaki hiroyuki.osaki@gmail.com

Thank you for reviewing this PR.

onelittlenightmusic and others added 3 commits March 9, 2021 00:42
Before, resolveRefs ignored Imports when Package is nil.
This caused `opa eval --import` option to be ignored without `--package` option.

This PR makes query compiler generate temporary package with name of ""
(empty string) when package is nil but import one or many imports are provided.

Fixes open-policy-agent#3228

Signed-off-by: Hiro Osaki <hiroyuki.osaki@gmail.com>
Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @onelittlenightmusic 🙏

@tsandall tsandall merged commit 51b4172 into open-policy-agent:master Mar 10, 2021
@onelittlenightmusic onelittlenightmusic deleted the eval-import-without-package branch March 14, 2021 07:27
This was referenced Mar 15, 2021
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.

How to use opa eval --import option
2 participants