-
Notifications
You must be signed in to change notification settings - Fork 53
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
add custom functions feature #28
base: master
Are you sure you want to change the base?
add custom functions feature #28
Conversation
Thanks for the PR! I like the direction this PR is headed. The FnDispatcher that is globally available using the static instance is only that way so that it can be reused for expressions that only use the built-in functions. Because it's globally available, I chose to make it immutable so that no custom functions can be added without the end-user explicitly opting into them. I think that is lost in this PR given that the global FnDispatcher is now mutable. In order to use custom functions, I think the user should have to explicitly opt-in to them. This is the same approach taken in the Python implementation you linked. Custom functions are currently supported in a limited fashion by using a custom function dispatcher. Here's an example: #20 (comment). Some of the things that this technique does not currently support:
In addition to the above changes, maybe there are some ergonomic changes that could be made to make it easier to add custom functions. For example, perhaps a new function dispatcher class could be added that decorate the built-in dispatcher with custom functions, and allows for mutability to add custom functions. |
@mtdowling as I understand it, this PR currently has the following Pro's:
But the big cons:
So you propose the solution to still pass a custom Given the fact that I don't know the |
After thinking about this for a few years... I don't think custom functions are actually a desirable feature in JMESPath implementations. They add a larger API surface area than I think each language implementation should. Ideally, I'd like to see the Rust implementation of JMESPath get bindings for each of our supported languages, and the language specific implementations work transparently with the Rust implementation if found or the language specific implementation if not. This works much more easily if the language bindings for JMESpath across implementations are as small and constrained as possible. |
On the contrary, I find this PR to be the thing that makes a constrained toolkit really useful. I was stymied by the lack of simple primitives for things like substr, date, etc. Now I can make the experience more comfortable for the template creator without breaking out of the JMESPath specification method. I can name the functions with an underscore (or similar) to signal to the template creator that these are not native functions, and point them to where they are defined. |
Hello ;-)
With envy I have seen that
jmespath.py
added a custom function feature in jmespath/jmespath.py#102 - and that is something I need for the PHP implementation too ;-) so I created this PR that should add this feature - I also added tests and documentation.let me know if anything is missing.
PS: I also changed the singleton handling a bit in
FnDispatcher
as IMHO the instance should be a defined static class property, not implicitly created in thegetInstance()
method.