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

Allow passing own compiler to oracle #7354

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

anderseknert
Copy link
Member

@anderseknert anderseknert commented Feb 10, 2025

Verified that this works in Regal.

Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

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

👍

@@ -45,6 +50,14 @@ type DefinitionQueryResult struct {
Result *ast.Location `json:"result"`
}

// WithCompiler sets the compiler to use for the oracle. If not set, a new ast.Compiler
// will be created when needed.
func (o *Oracle) WithCompiler(compiler *ast.Compiler) *Oracle {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a setter for the compiler so that we can create an oracle in one place, and update it with a compiler in another? If not, maybe consider adding a vararg option to the New() factory func instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what we do in Regal currently. Set the compiler once in a package-level var and then have it updated with the compiler per request. Not sure to what degree the compiler can be reused though 🤔 Are you saying we could have that set only once too?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the internal call to Compiler.Compile(map[string]*Module) will reset the modules on the compiler at least; but maybe there is other state that would need to get wiped that isn't today 🤔 .
But if you're creating a new compiler for each round, then also creating a new oracle doesn't sound like much added overhead 😄.

Copy link
Member Author

Choose a reason for hiding this comment

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

For sure, but we'd rather reuse the compiler too if that's an option :) I just assumed it wasn't given that the original implementation had it created per call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great if we could add a test for injecting a custom compiler. Could simply be one with a simple/no-op compile-stage added that we then declare in the call to compileUpto().

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a (trivial) test to assert the custom compiler is used.

Verified that this works in Regal, but also added a trivial
test to assert that a custom compiler passed is used.

Signed-off-by: Anders Eknert <[email protected]>
@anderseknert anderseknert merged commit af64edb into open-policy-agent:main Feb 10, 2025
28 checks passed
@anderseknert anderseknert deleted the opa-oracle-2 branch February 10, 2025 20:57
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