-
Notifications
You must be signed in to change notification settings - Fork 221
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
Parent operator proposal #299
Comments
When I think of it, the access class can for paths be nicely implemented in javascript with the proxy object. In this way we could add custom behaviour without polluting jsonata. This is an alternative to the mixin: #87. This is actually so powerful that you can implement your own path based DSL. I use this in a myriad of ways, for instance like a kind of mini ORM. |
Interesting. I've also been looking at symbols as a way of fixing #296. I'll look into what can be done here. Thanks! |
Just solved this neatly for my use case with proxy and symbol:
Note that for array parent is set, not the array itself. This implementation requires no changes to jsonata at all. In fact, proxies can be used to create surprisingly advanced DSL's. Maybe useful in a FAQ section or recipe? |
I've now got this mostly implemented. I propose to use the symbol '%' within a path expression to represent the 'parent' of the current context. I define 'parent' to be the enclosing object which has the property representing the context value. Example:
This returns an array of objects for each product in each order in each account. Information from the enclosing Order and Account objects can be accessed using the parent operator. There has been some discussion on slack as to which symbol to use for this. I had originally proposed to use the exclamation mark '!' since that is currently not used. However, there were concerns that this would be mistaken for a Boolean NOT operator as used in many languages (although not JSONata - it has a
|
I'm not sure if this is too late to continue the discussion on the syntax. I wanted to think a couple steps ahead to some of the other possibilities for looking "up" the tree to do other ancestor queries. In general I think that building on the I'm not sure how the tokenizer and parser might be impacted by some of these changes, sorry if it's obvious why these are impossible Existing Parent-only: Numbered ancestor: Wildcard ancestor: |
I'd love to see some compelling use-cases that would justify the need for all of these. The case for adding a parent accessor was clearly made (several times) with examples. Currently, to access information in the parent structure means binding it to a variable and referring to it in a sub-expression as in the following example:
http://try.jsonata.org/rJVVfeUZU With a parent operator, this is achieved using:
which is conceptually much simpler. Syntactically, the dollar
Going forward, we could add the operator |
Thanks @andrew-coleman for the illustrative examples.
This blew my mind and clearly kills the
I don't have these use cases. I guess $$ is really just a special character, and not an ancestor query, and $ is better left for variables / functions. In conclusion I support the |
A while ago I said that I would describe the way a parent operator works in legacy programming languages.
Since then I discovered the power of Symbols.
In legacy we would work with classes so to add behaviour to data we would create an access class as a layer between the framework(jsonata) and the data. In there pointers to the data could be stored to indicate, for instance, parent of current data.
Drawback of this solution is that every call to the evaluation part has to be redirected to this class. That is quite a bit of interfacing.
Now with javascript we have symbols as a metadata annotation that can be added directly to the data without pollution the look and feel. We could add either a reference, a copy or a path so that we can easily retrieve the parent.
In the jsonata common scope add:
and for instance use a standard name in the
evaluateName
method:Then whenever a name is traversed, the symbol must be added, with the parent:
In the debugger we can see the symbol but it cannot be accessed outside of the jsonata scope.
This above example is not complete since it does not deal with arrays, but you get the point.
A similar approach can be used to store paths. This to do even more complicated accessing.
Here is an idea of path + access class in java
Let me know what you think.
Cheers
Martin
The text was updated successfully, but these errors were encountered: