Skip to content

Commit 642fe90

Browse files
added get_nested_values_info to common lib (#4955)
1 parent 1e1654f commit 642fe90

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

assets/libraries/common.rego

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package generic.common
22

3+
import future.keywords.in
4+
35
# build_search_line will convert all values to string, and build path with given values
46
# values need to be in the correct order
57
# obj case is for the walk function although it can be used as needed
@@ -705,3 +707,32 @@ has_wildcard(statement, typeAction) {
705707
} else {
706708
check_actions(statement, typeAction)
707709
}
710+
711+
712+
get_search_key(arr) = sk {
713+
sk := concat_path(arr[0].searchKey)
714+
} else = sk {
715+
sk := ""
716+
}
717+
718+
# valid returns if the array_vals are nested in the object (array_vals should be sorted)
719+
# searchKey returns the searchKey possible
720+
#
721+
# object := {"elem1": {"elem2": "elem3"}}
722+
# array_vals := ["elem2", "elem3", "elem4"]
723+
#
724+
# return_value := {"valid": false, "searchKey": "elem2.elem3"}
725+
get_nested_values_info(object, array_vals) = return_value {
726+
arr := [x |
727+
some i, _ in array_vals;
728+
[path, _] := walk(object)
729+
path == array.slice(array_vals, 0, count(array_vals)-i)
730+
x := {
731+
"searchKey": path
732+
}
733+
]
734+
return_value := {
735+
"valid": count(array_vals) == count(arr),
736+
"searchKey": get_search_key(arr)
737+
}
738+
}

0 commit comments

Comments
 (0)