New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve support for handling undefined inside of queries #2345
Comments
Throwing in my $.50, an alternative operator that can be applied to expressions is very close to an inline-style OR (as opposed to incremental definitions). This would be a useful quality of life improvement for situations where where you need to resort to an extra auxiliary rule now. It also doesn't introduce any new semantics to the language since it can be rewritten as auxiliary rules. I like
|
I think this feature would be great, also for when calling functions that could be undefined. |
With |
Will that |
No, I'm not sure I follow your example completely though. Could you provide a complete one where you show what you want to accomplish.. where that something is currently cumbersome? 🙂 |
If you see here: https://play.openpolicyagent.org/p/DTC58WYnFl |
Thanks @aholmis, I think I understand, although I'm not sure I got how an "undefined operator" would help here. Undefined as introduced by unknown attributes in canbe(thing) { thing == 0 }
canbe(input.count) --> canbe(thing) { thing == 0 }
canbe(object.get(input, ["count"], -1)) Undefined as introduced by a function that doesn't evaluate can be dealt with either like you did with canbe(thing) = true { thing == 0 }
canbe(thing) = false { thing != 0 } If we had an "undefined operator" we could do something like this, which would be pretty nice I guess :) o := {
"x": f(x) // false
} |
This is what I would like, because then you don't modify the function itself, you just adapt the usage.
This would still be undefined and cause the rule to be undefined as well, because the function body does not evaluate to |
That said, it is not a big problem for us (I just found this issue on github), and the amount of work required to introduce a new operator might be too much? |
It's often necessary to select a nested field out of a document but fallback to a default value if the referenced field is undefined. In the past users could write a helper function to assist with this, however, over time we ended up adding the
object.get
built-in function to make it easier.Programming languages don't typically include this type of feature--it's just implemented in libraries. Looking at query languages for hierarchical data:
jq
uses//
for it's alternative operator(foo, bar)[1]
and iffoo
is undefined, the return value isbar
(XPath indices are 1-based.)One issue with
object.get
is that it only works one level at a time. For example:You could improve this using something like
walk
:One option would be to include some new operator for providing alternatives, e.g., following jq:
Some questions:
false
as well.default
to be used against references (similar towith
)?The text was updated successfully, but these errors were encountered: