opa
opa copied to clipboard
Improve support for handling undefined inside of queries
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 - XPath 2.0 allows you to construct a sequence. If an expression is undefined, it's not included in the sequence (roughly). So you can say
(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:
a := object.get(input, "foo", {})
b := object.get(input, "bar", {})
c := object.get(input, "baz", 7)
You could improve this using something like walk
:
c := object.select(input, "foo.bar.baz", 7)
One option would be to include some new operator for providing alternatives, e.g., following jq:
c := input.foo.bar.baz // 7
Some questions:
- Can alternative operator be applied to non-ref terms or expressions? If it's applied to expressions, it's tempting to make it catch
false
as well. - What about allowing
default
to be used against references (similar towith
)?