Skip to content

Commit ac1ec55

Browse files
committed
More nondeterminism
1 parent 87c49c3 commit ac1ec55

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

jsonpath_rfc9535/selectors.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
from __future__ import annotations
44

5+
import random
56
from abc import ABC
67
from abc import abstractmethod
78
from contextlib import suppress
89
from typing import TYPE_CHECKING
10+
from typing import Any
911
from typing import Iterable
1012
from typing import Optional
1113
from typing import Sequence
@@ -211,7 +213,14 @@ def __hash__(self) -> int:
211213
def resolve(self, node: JSONPathNode) -> Iterable[JSONPathNode]:
212214
"""Select all items from a array/list or values from a dict/object."""
213215
if isinstance(node.value, dict):
214-
for key, val in node.value.items():
216+
if self.env.nondeterministic:
217+
_items = list(node.value.items())
218+
random.shuffle(_items)
219+
items: Iterable[Any] = iter(_items)
220+
else:
221+
items = node.value.items()
222+
223+
for key, val in items:
215224
_node = JSONPathNode(
216225
value=val,
217226
parts=node.parts + (key,),
@@ -257,10 +266,17 @@ def __eq__(self, __value: object) -> bool:
257266
def __hash__(self) -> int:
258267
return hash((str(self.expression), self.token))
259268

260-
def resolve(self, node: JSONPathNode) -> Iterable[JSONPathNode]:
269+
def resolve(self, node: JSONPathNode) -> Iterable[JSONPathNode]: # noqa: PLR0912
261270
"""Select array/list items or dict/object values where with a filter."""
262271
if isinstance(node.value, dict):
263-
for key, val in node.value.items():
272+
if self.env.nondeterministic:
273+
_items = list(node.value.items())
274+
random.shuffle(_items)
275+
items: Iterable[Any] = iter(_items)
276+
else:
277+
items = node.value.items()
278+
279+
for key, val in items:
264280
context = FilterContext(
265281
env=self.env,
266282
current=val,

0 commit comments

Comments
 (0)