@@ -33,6 +33,20 @@ def _is_special_integer_case(x, y):
3333 return x is True or x is False
3434
3535
36+ class Options (object ):
37+ """Options to control how a JMESPath function is evaluated."""
38+ def __init__ (self , dict_cls ):
39+ #: The class to use when creating a dict. The interpreter
40+ # may create dictionaries during the evalution of a JMESPath
41+ # expression. For example, a multi-select hash will
42+ # create a dictionary. By default we use a dict() type.
43+ # You can set this value to change what dict type is used.
44+ # The most common reason you would change this is if you
45+ # want to set a collections.OrderedDict so that you can
46+ # have predictible key ordering.
47+ self .dict_cls = dict_cls
48+
49+
3650class _Expression (object ):
3751 def __init__ (self , expression ):
3852 self .expression = expression
@@ -67,8 +81,12 @@ class TreeInterpreter(Visitor):
6781 }
6882 MAP_TYPE = dict
6983
70- def __init__ (self ):
84+ def __init__ (self , options = None ):
7185 super (TreeInterpreter , self ).__init__ ()
86+ self ._options = options
87+ self ._dict_cls = self .MAP_TYPE
88+ if options is not None and options .dict_cls is not None :
89+ self ._dict_cls = self ._options .dict_cls
7290 self ._functions = functions .RuntimeFunctions ()
7391 # Note that .interpreter is a property that uses
7492 # a weakref so that the cyclic reference can be
@@ -167,7 +185,7 @@ def visit_literal(self, node, value):
167185 def visit_multi_select_dict (self , node , value ):
168186 if value is None :
169187 return None
170- collected = self .MAP_TYPE ()
188+ collected = self ._dict_cls ()
171189 for child in node ['children' ]:
172190 collected [child ['value' ]] = self .visit (child , value )
173191 return collected
0 commit comments