feat: add function to retrieve object by path #199
Merged
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.
Ideally, we should point to the location of the misconfiguration in the code as precisely as possible. To do this, the
result.new
funuction takes an object with its location metadata as the second parameter. But there may be problems with this if the passed input is incomplete.The following check safely checks for the absence of
bar
or its false value, but may fail ifbar
or the parent structure is missing, since theinput.provider.foo.bar
expression passed to theresult.new
function will beundefined
.To solve this problem, we could use the
object.get
function to safely get a nested object, for exampleobject.get(some_resource, ["block1", "block2", "some_attr"], resource)
, but in that case the result will not contain the exact location of the misconfig. And callingobject.get
for each level of nesting is fraught with a lot of code.This PR adds a function that solves the problem. It recursively searches for a child object along the given path and if it is not found, it returns the last found one.
Example of use: