Add context-aware query methods
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
YAQL |
New
|
Undecided
|
Unassigned |
Bug Description
Standard library query methods such as where are not context aware. This is useful in many cases but we need such query methods with the context as well. It's not a problem if those are other functions. Here is an example.
Non-context-aware where (from the standard library)
yaql> [1, 2, 3].where($ > 2)
[3]
Context-aware where (with the following context: {"value": 1})
yaql> [1, 2, 3].ctxWhere($.item > $.ctx.value)
[2, 3]
It could be interesting to support custom contexts as well.
yaql> [1, 2, 3].ctxWhere($.item > $.ctx.value, dict(value=>3))
[]
The same goes for other list methods such as select, all, any, join...
We already have implemented most of them, if you are interested, we can make a change request to add them to the standard library.
I was also looking for something similar and couldn't find anything about it in the documentation.
It seems that the canonical way to do it is to combine "let" and "context pass" (->) operators to first define a variable that stores the value you need to use before context is overriden.
``` 'let(target_ age => $.target_ age)->$ .users. where($ .age=$target_ age)'
EXPRESSION=
cat > context.json <<EOF
{
"users": [
{"name": "toto", "age": 18},
{"name": "tata", "age": 19},
{"name": "titi", "age": 18}
],
"target_age": 18
}
EOF
yaql -d context.json -n "$EXPRESSION"
```