-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update placeholder #
hack to work better
#43
Comments
Being open source, I'll take a peek and see if there is a PR I can craft up. But in the meantime, I'd love any sort of discussion around this. |
I'm starting to think this could be a feature request for this repo or https://github.com/PaesslerAG/gVal because I'm not sure there is a way to parse the JSONPath (complex enough to avoid doing at all/most costs) and use the placeholder hack to generate full paths for all resolved values. I think for simple cases (direct value resolution, single-segment wildcards, single-levels of recursion) it could be possible but more complex cases (recursion with segments after recursion, any level of nested recursion) fails based on my understanding. I think there are other cases that I remember being difficult/impossible but that's the gist of it. I'll keep digging but as of right now, this is definitely not straight forward. |
To shed some light on this based on a slightly convoluted example (modified from https://jsonpath.com), here is an example JSON document and the example placeholders returned for a few JSONPath expressions: JSON {
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
},
{
"miscellaneous": [
{
"type": "mobile",
"number": "0123-4567-8910",
"nested": [
{
"type": "work",
"number": "0123-4567-8910"
}
]
}
]
}
],
"others": [
{
"number": {
"details": {
"type": "home",
"content": "0123-4567-8910"
}
}
},
{
"number": {
"details": {
"type": "work",
"content": "0123-4567-8910"
}
}
}
],
"nested": {
"others": [
{
"number": {
"details": {
"type": "home",
"content": "0123-4567-8910"
}
}
},
{
"number": {
"details": {
"type": "work",
"content": "0123-4567-8910"
}
}
}
]
}
} Results
Based on these examples (convoluted, I know...but I'm trying to identify edge cases), assuming I could correctly parse the JSONPath, I could reconstruct the actual path for all non-multi-nested expressions. But for multi-nested expressions (Example: |
Actually, https://github.com/PaesslerAG/jsonpath/blob/master/jsonpath.go#L7 may have just given me a possibility, but if I understand things, it won't be ideal due to the fact you'd have to evaluate the JSONPath expression N times (where N is the number of wildcards). |
#
hack to work better without wildcard paths#
hack to work better
After a good deal of mucking around, it seems like the context between the JSONPath parsing and the gVal execution is different, so attempting to update If anyone more knowledgeable on gVal and/or jsonpath can share some ideas, let me know. I'm going to punt for a bit, I've been stuck on this for a few days and I need to make progress elsewhere. @generikvault maybe? |
Okay, I just found out about gval.NewEvaluableWithContext...gonna give this a shot. |
Using |
What if we just created a new API like |
Okay, I will have a PR together that should handle this and it does not use the "placeholder hack". While I would also consider this to be a hack of sorts, it works and instead of hoping to share a common scope to keep track of these automatically, I resort to a much simpler approach. Here is the process:
For the example above, here are the new keys (matched values are identical to
|
This commit adds the `GetWithMaps` API that will instead of returning the resolved value(s), it will return a map whose keys are the JSONPath to the corresponding resolved value. Fixes PaesslerAG#43
I ended up with a much simpler approach, although sanitizing the paths returned by |
As mentioned by @generikvault in #10 (comment), there is a "hack" for getting the path to the value(s) returned by
jsonpath.Get()
. This works great for wildcard paths but for absolute paths, the onus is on thejsonpath
consumer to be able to accurately parse the provided JSONPath to be able to append to the provided path. Here is an example of some JSONPath values and the returned path using the aforementioned link:As you can tell, the path provided by the placeholder
#
hack is far from the actual path of the resolved value provided byjsonpath.Get()
. Is there any possibility of extending the placeholder#
hack to be more accurate for non-wildcard JSONPath values, or could there be an API exposed (or hack) that would make it where we can parse the provided JSONPath (consistently) so that we can add the proper suffix to the value provided by the placeholder#
hack?The text was updated successfully, but these errors were encountered: