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

feat(common lib): improved performance of get_nested_values_info #5075

Merged
merged 2 commits into from
Mar 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions assets/libraries/common.rego
Original file line number Diff line number Diff line change
Expand Up @@ -710,43 +710,32 @@ has_wildcard(statement, typeAction) {
}


get_search_key(arr) = sk {
sk := concat_path(arr[0].searchKey)
} else = sk {
sk := ""
}

# valid returns if the array_vals are nested in the object (array_vals should be sorted)
# valid returns if all array_vals are nested in the object (array_vals should be sorted)
# searchKey returns the searchKey possible
#
# object := {"elem1": {"elem2": "elem3"}}
# array_vals := ["elem2", "elem3", "elem4"]
# array_vals := ["elem1", "elem2", "elem4"]
#
# return_value := {"valid": false, "searchKey": "elem2.elem3"}
# return_value := {"valid": false, "searchKey": "elem1.elem2"}
get_nested_values_info(object, array_vals) = return_value {
arr := [x |
some i, _ in array_vals;
[path, _] := walk(object)
path == array.slice(array_vals, 0, count(array_vals)-i)
x := {
"searchKey": path
}
some i, _ in array_vals
path := array.slice(array_vals, 0, i+1)
walk(object, [path, _]) # evaluates to false if path is not in object
x := path[i]
]

return_value := {
"valid": count(array_vals) == count(arr),
"searchKey": get_search_key(arr)
"searchKey": concat(".", arr)
}
}

remove_last_point(searchKey) = sk {
endswith(searchKey, ".")
sk = substring(searchKey, 0, count(searchKey) -1)
} else = sk {
sk := searchKey
sk := trim_right(searchKey, ".")
}

# This function is based on this docs(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-optimized.html#describe-ebs-optimization)

# This function is based on these docs: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-optimized.html#describe-ebs-optimization
is_aws_ebs_optimized_by_default(instanceType) {
inArray(data.common_lib.aws_ebs_optimized_by_default, instanceType)
}