Skip to content

Commit

Permalink
Add "reduce" model
Browse files Browse the repository at this point in the history
  • Loading branch information
4c0n committed Aug 28, 2024
1 parent c0110c0 commit 9c6f498
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pydantic_jsonlogic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,14 @@ class Map(BaseJSONLogicOperation):
# The operation to be performed on each element of the array
BaseJSONLogicOperation,
]


class Reduce(BaseJSONLogicOperation):
reduce: tuple[
# An array or an operation that produces an array
list[Any] | Var | Missing | MissingSome | If | Merge | Filter | Map,
# The operation to be performed for each element of the array
BaseJSONLogicOperation,
# The initial accumulator value
Any,
]
25 changes: 25 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
NotEquals,
NotNot,
Or,
Reduce,
StrictEquals,
StrictNotEquals,
Substr,
Expand Down Expand Up @@ -506,3 +507,27 @@ def test_filter(json: str) -> None:
)
def test_map(json: str) -> None:
Map.model_validate_json(json)


@pytest.mark.parametrize(
"json",
[
"""{"reduce":[
{"var":"integers"},
{"+":[{"var":"current"}, {"var":"accumulator"}]},
0
]}""",
"""{"reduce":[
{"var":"integers"},
{"+":[{"var":"current"}, {"var":"accumulator"}]},
{"var": "start_with"}
]}""",
"""{"reduce":[
{"var":"integers"},
{"*":[{"var":"current"}, {"var":"accumulator"}]},
1
]}""",
],
)
def test_reduce(json: str) -> None:
Reduce.model_validate_json(json)

0 comments on commit 9c6f498

Please sign in to comment.