-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ override builtin functions (#5156)
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 <afiune@mondoo.com>
- Loading branch information
Showing
10 changed files
with
311 additions
and
38 deletions.
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.