It seems that the only way we could find it out is to "filter" given array to get an array of filtered values and then compare its size to the original array's size.
I can describe this flow using rego syntax:
colors := ["red", "blue", "green"]
nonRedColors := [ color | color = colors[i] ; colors[i] != "red"]
count(colors) != count(nonRedColors)
Is this the only way it's possible to find out an element is not included in array?
It is possible to have a separate function that would do this, but isn't there a built in function that can handle this, because this use case seems common
You can write a helper function or rule:
contains(colors, elem) {
colors[_] = elem
}
Then refer to that in your policy:
not contains(["blue", "green"], "red")
If you're constructing the array inside your policy (i.e., it's not supplied as an input) then you consider using a set instead: s := {"blue", "green"}; not s["red"].
Will future releases include some built-ins for such frequently used operations to remove the need of writing custom helpers?
Also, in your example you manually exclude "red" element from colors array. It does not seem convenient
We've been kicking around the idea of a "for all" quantifier that would let you express "Does not exist" as well as other common patterns.
Cool, thanks!
Closing this issue for now. Thanks for opening it.
contains_element(arr, elem) = true {
arr[_] = elem
} else = false { true }
Need to be explicitly declared as a bool type in some cases
v = [a, b] {
element_sets = [{1, 2}, {2}]
a = [contains_element(s, 3) | s = element_sets[_]]
b = [contains_element2(s, 3) | s = element_sets[_]]
}
contains_element(arr, elem) = true {
arr[_] = elem
} else = false { true }
contains_element2(arr, elem) {
arr[_] = elem
}
Eval result:
{
"v": [
[
false,
false
],
[]
]
}
Most helpful comment
You can write a helper function or rule:
Then refer to that in your policy:
If you're constructing the array inside your policy (i.e., it's not supplied as an input) then you consider using a set instead:
s := {"blue", "green"}; not s["red"].