Add context-aware query methods

Bug #1790115 reported by Xavier Hardy
This bug affects 3 people
Affects Status Importance Assigned to Milestone

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)

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.

Revision history for this message
Sylvan Le Deunff (sylvanld) wrote :

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.

EXPRESSION='let(target_age => $.target_age)->$.users.where($.age=$target_age)'

cat > context.json <<EOF
    "users": [
        {"name": "toto", "age": 18},
        {"name": "tata", "age": 19},
        {"name": "titi", "age": 18}
    "target_age": 18

yaql -d context.json -n "$EXPRESSION"

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.