Reputation: 229
How to create a set from a string in Rego Open Policy Agent language?
I've a string of elements like "values": "pvc-volume,emptydir-volume,hostPath,ConfigMaps"
which elements need to be validated against a given set of admitted values
valid_backup_profiles := {
"pvc-volume",
"emptydir-volume"
}
for example using intersection
valid_backup_profiles & elements_coming_from_string_above
I know the split(string, ",")
function returning an array of elements but the array cannot be intersected with a set. Is there any smart way to generate a set from a string instead of an array?
Upvotes: 0
Views: 8704
Reputation: 1609
The trick is to use a set comprehension:
s := {x |
some i
parts := split(string, ",")
x := parts[i]
}
You can do it on one line like this if you want:
s := {x | x := split(string, ",")[_]}
Obviously this is nice and compact, but the form above is a bit easier to read. If you want to make the logic reusable, just define a function:
split_csv_set(str) = {x |
some i
parts := split(str, ",")
x := parts[i]
}
Upvotes: 2