thedevsaddam / gojsonq

A simple Go package to Query over JSON/YAML/XML/CSV Data
https://github.com/thedevsaddam/gojsonq/wiki
MIT License
2.17k stars 140 forks source link

Multiple calls to `Find` returns nil #59

Open tomelm opened 4 years ago

tomelm commented 4 years ago

I'm trying to use gojsonq to make some queries to JSON data. I was setting up some tests that were returning nil when I tried to validate the data inside of the JSON object. Eventually, I noticed if I do multiple Finds, it starts returning nil.

Here's an example of the behavior: https://play.golang.org/p/qxiooDmflIe

Is this expected? I couldn't find a way in the documentation to run the same query multiple times and get the same data back.

Kvaz1r commented 4 years ago

Yes, it's expected. Query it's like a chain if you move forward you can't automatically return back to previous state. There is Reset/0 method. Although it would be great to have opportunity save current state and return not only to start point.

thedevsaddam commented 4 years ago

I appreciate the thought, it would be great if the reset calls invoke automatically. This can be an improvement.

tomelm commented 4 years ago

Thanks @Kvaz1r, that's what I was looking for! Maybe for Find it automatically resets since it seems like users won't really be building queries with that?

joe94 commented 4 years ago

Yeah, an efficient reset would be nice here for uses cases where we want to initialize the query object given a map and then just query for specific values given their paths many times.

Also https://github.com/thedevsaddam/gojsonq/wiki/Queries#findpath mentions that

Caveat: You can't chain further query methods after it. If you need that, you should use From() method

so it's be compatible with the design.