-
Notifications
You must be signed in to change notification settings - Fork 24
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
✨ override builtin functions #5156
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
✨ override builtin functions
Today, there is no official way to override builtin functions like `length`, this change tries to do the most minimal change in MQL to allow a unique pattern for providers to override builtin functions. The core change is an additional check when executing a bound function. We check if the resource defines the builtin function like `length`, and if so, we prioritize it. If the provider doesn't define it, we execute the function as we did before, by loading first the builtin function and if not existing, then we run the resource function. Here is an example of this pattern to override the `length` builtin function. Having a resource that exposes a function that loads big amounts of resources: ``` cloud { resources() []resources } ``` The provider implementation would look something like this: ```go func (c *mqlCloud) resources() ([]interface, error) { // API call to fetch all resources } ``` When running the MQL query `cloud.resources.length`, we first load all the resources, and then we count them. Since we do not need any information about the resources themselves, this could delay policies if we do things like: ``` cloud.resources.length < 5 && cloud.resources.length > 1 ``` An alternative to improve these resources is to override the builtin function with a more performant implementation. ``` cloud { // update function with a new custom list resource resources() customResources } // definition of a custom list resource customResources { []resource // overrides builtin function length() int } ``` The new implementation moves the logic that loads the resources into a `list()` function and exposes a new function that overrides the builtin function: would look like: ```go func (c *mqlCloud) resources() (*mqlCustomResources, error) { // Only initializes the custom list resource } func (c *mqlCustomResources) list() ([]interface, error) { // API call to fetch all resources } // length() overrides the builtin function, func (c *mqlCustomResources) length() (int64, error) { // This should be a more performant way to count the "resources" } ``` Additionally, this change moves the `findField()` function from the `compiler` to the resource `Schema`. Signed-off-by: Salim Afiune Maya <[email protected]>
- Loading branch information
commit 2d3c8e05929c2d03d5614c298dca273612e0abe5
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: builtin