Skip to content

Custom functions #100

@meric

Description

@meric

What are your thoughts about adding a method to register custom functions directly into RuntimeFunctions class in functions.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:

search("if(bar==`1`, `hello`, `world`)", {'bar': '1'})

This would return the literal hello if the bar key is 1, otherwise it returns world. The only issue is the current python implementation means its going to be hacky to do this. You have to override multiple classes, in functions.py and override the TreeInterpreter and the ParsedResult 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 in functions.py, rather than either forcing a fork or overriding a litany of classes.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions