opa icon indicating copy to clipboard operation
opa copied to clipboard

Error when function that does comparison is assigned as value in key/value pair object

Open deshmukhrajvardhan opened this issue 2 years ago • 3 comments

Short description

Was trying to use objects (key/value pairs) to simulate switch statements. It worked for most of the part but fails when a function that does comparison is assigned as a value in key/value pair object

check line 89/90 which cover the below snippet in the below links.

...
"scim":"",
"insights":{
    # if we have values = functions that do comparisons, that causes errors
    "getPostureDevice": insights_eval(arg)
    }

Link to rego playground:

  1. Case where it fails https://play.openpolicyagent.org/p/JQgXaQVyRQ

  2. alternate way i solved the above: Case where a separate object used to cater to that function https://play.openpolicyagent.org/p/A660QRcW59

Let me know if these links are unavailable and I can upload the files in this issue

Steps To Reproduce

Link to rego playground:

  1. Case where it fails https://play.openpolicyagent.org/p/JQgXaQVyRQ

Expected behavior

alternate way i solved the above: Case where a separate object used to cater to that function https://play.openpolicyagent.org/p/A660QRcW59

Additional context

deshmukhrajvardhan avatar Jul 06 '22 18:07 deshmukhrajvardhan

I'm not completely sure I understand what you're asking for; but just to rule out one interpretation right away: Rego does not have first-class functions. Functions cannot be passed as arguments to other functions, or put into collections, or assigned to variables, etc. -- Is that what you meant by

when a function that does comparison is assigned as a value in key/value pair object

srenatus avatar Jul 06 '22 18:07 srenatus

I'm sorry, i haven't been able to ask the question precisely. The attached links help demonstrate the problem i see.

It's true that function pointers/callbacks can't be passed around. But the way i am using them is, if key "a" is present in an object/dictionary then execute function "fn" This works fine for me as you can see in the 2nd link.

The problem i see in the 1st link is that when a function that does comparison is assigned as value it fails. Let me know if the above helps, if not, let me think how to simplify and ask the question.

deshmukhrajvardhan avatar Jul 06 '22 18:07 deshmukhrajvardhan

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days.

stale[bot] avatar Aug 05 '22 19:08 stale[bot]

Hey @deshmukhrajvardhan 👋

I don't believe this is a bug, but if you feel like your question is unanswered, the OPA discussion board would be a good place for getting input on this.

anderseknert avatar Oct 27 '22 11:10 anderseknert