-
Notifications
You must be signed in to change notification settings - Fork 176
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
Custom functions #100
Comments
I agree that I would like to have a way to register custom functions in a simple way. Some of the other language implementation of JMESPath support this so I'd like to do so with python. Let me see what I can some up with. Additionally, I'm thinking that it might actually be worth just having some sort of conditional expression in the JMESPath language. I think that's a common enough request. Either via if/else statements, or possibly with something like C's ternary operator. |
Is there anything I can do to assist in pursuing these objectives? I'd love to help if I can. |
I'd love to have a way to evaluate boolean expressions, this is the type of hack I'm resorting to |
Could we perhaps implement it in |
I've pushed a branch that adds support for adding custom functions via That API I'm shooting for is you can just subclass from a base class and anything with a function signature (and likely a naming convention) will be registered as a jmespath function. Let me play around with a few ideas. |
I ended up finding an API that seemed reasonable to me, and still allowed you to leverage the type checking/arity checking that built in jmespath functions have. Let me know if you have any feedback: #102 |
Just in case this is useful for anyone, I found a way to do it (sorry for posting on a dead issue but it's #1 result on google..) Background: I have a field which is titled "Facebook" but sometimes contains VK URLs due to an input error. I wanted to do something like:
To accomplish that, I'm making a custom group from data, where {
"Facebook": "http://vk.com/blahblah"
} I want to create an object like this when it contains Facebook: {
"name": "VKontakte",
"value": "http://vk.com/blahblah"
} And like this when it contains Facebook: {
"name": "Facebook",
"value": "http://www.facebook.com/blahblah"
} And the way I accomplished that was:
Then finally remove the field you just created to check it:
|
What are your thoughts about adding a method to register custom functions directly into
RuntimeFunctions
class infunctions.py
?JMESPath is almost good enough to use as a domain specific language for general language transforming objects. You can sneak in literals in the multi-select hash. You can filter for values, and transform them to booleans using
<=
,==
operators. There's some support for making sure values are numbers.However I don't see anyway to do something like "if value is x, return
y
" where you show a literal if a condition matches. There's no way to convert conditions to an arbitrary literal - if a value in a multi-select hash is going to be a literal, it has to be of the same value no matter what.I can see a possible workaround if custom functions on JMESPath. E.g. if I implement the "if" function for use, I can do something like:
This would return the literal
hello
if thebar
key is 1, otherwise it returnsworld
. The only issue is the current python implementation means its going to be hacky to do this. You have to override multiple classes, infunctions.py
and override theTreeInterpreter
and theParsedResult
classes as well.I think if custom functions were desired, it would be much more elegant if there is a method to register them directly into the
RuntimeFunctions
infunctions.py
, rather than either forcing a fork or overriding a litany of classes.What do you think?
The text was updated successfully, but these errors were encountered: