Home > Software engineering >  OPA Rego issues counting
OPA Rego issues counting

Time:12-06

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.

CodePudding user response:

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.

  • Related