Skip to content

Commit ee43f7d

Browse files
committed
Allow custom functions to be provided via Options
1 parent 7eed376 commit ee43f7d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

jmespath/visitor.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _is_special_integer_case(x, y):
3535

3636
class Options(object):
3737
"""Options to control how a JMESPath function is evaluated."""
38-
def __init__(self, dict_cls):
38+
def __init__(self, dict_cls=None, custom_functions=None):
3939
#: The class to use when creating a dict. The interpreter
4040
# may create dictionaries during the evalution of a JMESPath
4141
# expression. For example, a multi-select hash will
@@ -45,6 +45,7 @@ def __init__(self, dict_cls):
4545
# want to set a collections.OrderedDict so that you can
4646
# have predictible key ordering.
4747
self.dict_cls = dict_cls
48+
self.custom_functions = custom_functions
4849

4950

5051
class _Expression(object):
@@ -87,11 +88,16 @@ class TreeInterpreter(Visitor):
8788

8889
def __init__(self, options=None):
8990
super(TreeInterpreter, self).__init__()
90-
self._options = options
9191
self._dict_cls = self.MAP_TYPE
92-
if options is not None and options.dict_cls is not None:
92+
if options is None:
93+
options = Options()
94+
self._options = options
95+
if options.dict_cls is not None:
9396
self._dict_cls = self._options.dict_cls
94-
self._functions = functions.RuntimeFunctions()
97+
if options.custom_functions is not None:
98+
self._functions = self._options.custom_functions
99+
else:
100+
self._functions = functions.RuntimeFunctions()
95101

96102
def default_visit(self, node, *args, **kwargs):
97103
raise NotImplementedError(node['type'])

0 commit comments

Comments
 (0)