英文:
OPA Rego issues counting
问题
我正在尝试编写一个规则,但遇到了一个问题。我从我的输入中提取了以下内容:
myData:= [{"Key": "use", "Value": "1"}, {"Key": "use", "Value": "2"}, {"Key": "att1", "Value": "3"}]
我想要计算键为"use"的出现次数。但是当我执行以下操作时:
p := {keep| keep:= myData[_]; myData.Key == "use"}
我以为这会创建一个我想要保留的所有内容的列表,但是Playground报错了:
发生1个错误:policy.rego:24: rego_type_error: undefined ref: data.play.myData.Key
data.play.myData.Key
我希望我可以将它们列在p
中,然后执行count(p) > 1
来检查是否列出了多个。
英文:
I am trying to write a rule but am running into an issue. I managed to extract the following from as my input:
myData:= [{"Key": "use", "Value": "1"}, {"Key": "use", "Value": "2"}, {"Key": "att1", "Value": "3"}]
I am trying to count the amount of times a key with the value use appears. However when I do:
p := {keep| keep:= myData[_]; myData.Key == "use"}
I assumed this would create a listing of all I would like to keep but the playground errors with:
1 error occurred: policy.rego:24: rego_type_error: undefined ref: data.play.myData.Key
data.play.myData.Key
I hoped I could list them in p
and then do count(p) > 1
to check if more that one is listed.
答案1
得分: 2
在你的p
的集合推导式中,你正在遍历myData
中的对象,将每个元素赋值给keep
。然后,你对myData.Key
进行了断言。我认为你想要的是:
p := {keep| keep := myData[_]; keep.Key == "use"}
请注意,这是一个集合推导式,所以对于以下两个输入,p
将是相同的:
myData:= [{"Key": "use", "Value": "1"}]
myData:= [{"Key": "use", "Value": "1"}, {"Key": "use", "Value": "1"}]
如果这不是你想要的,你可以使用数组推导式(p := [ keep | keep := ... ]
)。
英文:
In your set comprehension for p
, you're iterating over the objects in myData
, assigning each element to keep
. Then, you assert something on myData.Key
. I think what you're looking for is
p := {keep| keep := myData[_]; keep.Key == "use"}
Be aware that it's a set comprehension, so p
would be the same for these two inputs:
myData:= [{"Key": "use", "Value": "1"}]
myData:= [{"Key": "use", "Value": "1"}, {"Key": "use", "Value": "1"}]
You could use an array comprehension (p := [ keep | keep := ... ]
) if that's not what you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论