OPA Rego问题计数

huangapple go评论87阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年12月5日 23:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70235808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定