Globbing object keys in rego #160
Answered
by
anderseknert
aflmp
asked this question in
OPA and Rego
-
Hi there, I'm trying to glob match keys in an object, can someone help spot the mistake. data.json {
"sources": {
"/resource/type/one": {
"id_1": ["something-one"]
},
"/resource/type/two": {
"id_1": ["something-one"],
"id_2": ["something-two"]
},
"/resource/type/*/three": {
"id_1": ["something-one"]
}
}
} first.rego package play
default match = false
default id_match = false
match {
id_match
}
# exact matching source names
source = s {
s = data.sources[input.source]
}
# glob matching source names
source = s {
glob.match(data.sources[_], ["/"], input.source)
s = data.sources[_]
}
id_match {
source[input.id_type][_] = input.id_value
} for a sample input {
"source": "/resource/type/123456/third",
"id_type": "id_1",
"id_value": "something-one"
} I'm expecting the output to be {
"id_match": true,
"match": true,
"source": {
"id_1": [
"something-one"
]
}
} instead, I'm getting {
"id_match": false,
"match": false
} any suggestions? |
Beta Was this translation helpful? Give feedback.
Answered by
anderseknert
Apr 4, 2022
Replies: 1 comment 7 replies
-
Hi Alam 👋 When iterating over the source = s {
s := data.sources[k]
glob.match(k, ["/"], input.source)
} |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
aflmp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Alam 👋 When iterating over the
data.sources
, you'd want to pass the key rather than the value (as that is an object) to theglob.match
function. This should do it: