Skip to content

Commit

Permalink
Merge pull request #95 from marier-nico/feat/dyn-filter-modifies-event
Browse files Browse the repository at this point in the history
fix: avoid updating event when filter does not match
  • Loading branch information
marier-nico authored Dec 13, 2022
2 parents 892e31c + 63500b2 commit 5cec194
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/content/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- v3.2.0: Allow ``Dyn`` filters to modify the event.
- v3.1.6: Fix type hints with ``Depends``.
- v3.1.5: Add py.typed file to support type hints.
- v3.1.4: Simplify calling processors by making ``EventProcessor`` callable.
- v3.1.3: Fix a bug with invocation when an event matches several filters.
- v3.1.2: Fix an incorrect import.
Expand Down
3 changes: 2 additions & 1 deletion docs/content/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ be valid for a dependency (see :ref:`Dependencies` for details). For example :
False

Since the Dyn filter is basically a way to do anything you can't do with static filters, it also allows modifying the
event before it gets passed to a processor function. For example :
event before it gets passed to a processor function. Note that the event is only modified if the value returned by the
dynamic function is truthy (i.e. event is unmodified if the filter doesn't match). For example :

.. testcode::

Expand Down
5 changes: 3 additions & 2 deletions src/event_processor/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ def matches(self, event: dict) -> bool:
else:
result = call_with_injection(self.resolver, event=Event(event), cache={})

if self.inject_as:
matches = bool(result)
if self.inject_as and matches:
event[self.inject_as] = result

return bool(result)
return matches

def __hash__(self):
return hash(self.resolver)
Expand Down
11 changes: 10 additions & 1 deletion src/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def dyn_filter():
assert hash(filter_) == hash(dyn_filter)


def test_dyn_filter_updates_event_when_inject_as_is_specified():
def test_dyn_filter_updates_event_when_inject_as_is_specified_and_filter_matches():
mock_event = {}
filter_ = Dyn(lambda e: "my-value", inject_as="my-key")

Expand All @@ -485,6 +485,15 @@ def test_dyn_filter_updates_event_when_inject_as_is_specified():
assert mock_event["my-key"] == "my-value"


def test_dyn_filter_does_not_update_event_when_inject_as_is_specified_and_filter_does_not_match():
mock_event = {}
filter_ = Dyn(lambda e: False, inject_as="my-key")

filter_.matches(mock_event)

assert "my-key" not in mock_event


def test_eq_filter_matches_when_resolvers_are_equal():
mock_resolver = Mock()

Expand Down

0 comments on commit 5cec194

Please sign in to comment.