Skip to content

Commit

Permalink
More nondeterminism
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Mar 15, 2024
1 parent 87c49c3 commit ac1ec55
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions jsonpath_rfc9535/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from __future__ import annotations

import random
from abc import ABC
from abc import abstractmethod
from contextlib import suppress
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterable
from typing import Optional
from typing import Sequence
Expand Down Expand Up @@ -211,7 +213,14 @@ def __hash__(self) -> int:
def resolve(self, node: JSONPathNode) -> Iterable[JSONPathNode]:
"""Select all items from a array/list or values from a dict/object."""
if isinstance(node.value, dict):
for key, val in node.value.items():
if self.env.nondeterministic:
_items = list(node.value.items())
random.shuffle(_items)
items: Iterable[Any] = iter(_items)
else:
items = node.value.items()

for key, val in items:
_node = JSONPathNode(
value=val,
parts=node.parts + (key,),
Expand Down Expand Up @@ -257,10 +266,17 @@ def __eq__(self, __value: object) -> bool:
def __hash__(self) -> int:
return hash((str(self.expression), self.token))

def resolve(self, node: JSONPathNode) -> Iterable[JSONPathNode]:
def resolve(self, node: JSONPathNode) -> Iterable[JSONPathNode]: # noqa: PLR0912
"""Select array/list items or dict/object values where with a filter."""
if isinstance(node.value, dict):
for key, val in node.value.items():
if self.env.nondeterministic:
_items = list(node.value.items())
random.shuffle(_items)
items: Iterable[Any] = iter(_items)
else:
items = node.value.items()

for key, val in items:
context = FilterContext(
env=self.env,
current=val,
Expand Down

0 comments on commit ac1ec55

Please sign in to comment.