jmespath / jmespath.site

The repo for the jmespath.org website.
http://jmespath.org/
Apache License 2.0
56 stars 51 forks source link

[Possible improvement] Auto reset projections? #109

Open b-enoit-be opened 2 years ago

b-enoit-be commented 2 years ago

While writing the issue #108, I was wondering if an improvement shouldn't be to systematically reset projections.


Little background: I came to JMESPath thanks to its usage In Ansible. Ansible uses Jinja2, which, previously, would return Python generator from filters like map or select. Because of that, if you needed to display a mapped of filtered list, you would have to explicitly cast it back as list, with a list filter.

e.g.

[1, 2, 3, 4] | select('odd') # gives you a generator object <generator object-id...>

versus

[1, 2, 3, 4] | select('odd') | list # gives you the list [1, 3]

Recently, Jinja decided to change that and those filters return you a list right away, so you can finally do:

[1, 2, 3, 4] | select('odd') # gives you the expected list [1, 3]

I am mostly asking this because I don't see any advantage to have a projection returned.

One downside, though is that on the pipe expression, and if we have string slicing, which will be in the next JEP, we will end up with James, instead of ["J", "J", "J"].

But shouldn't this be handled by parenthesis instead of a pipe expression (which works already!)?

people[*].first[0] # gives `["J", "J", "J"]`
(people[*].first)[0] # gives `James`

Possibly what could make it clearer would be to force parenthesis for the first behaviour?

people[*].(first[0]) # gives `["J", "J", "J"]`
(people[*].first)[0] # gives `James`
people[*].first[0] 
## yields any sort of error, about an ambiguous notation, 
## or return an opinionated or backward compatible solution of the two above?

What are your thoughts?