Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Make event_match work on booleans and integers #13466

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13466.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make event_match work on booleans and integers.
4 changes: 4 additions & 0 deletions synapse/push/push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ def _flatten_dict(
for key, value in d.items():
if isinstance(value, str):
result[".".join(prefix + [key])] = value.lower()
elif isinstance(value, bool) or isinstance(value, int):
# Convert booleans and integers to their string representation
# so that they can be matched as well
result[".".join(prefix + [key])] = f"{value}".lower()
elif isinstance(value, Mapping):
_flatten_dict(value, prefix=(prefix + [key]), result=result)

Expand Down
56 changes: 56 additions & 0 deletions tests/push/test_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,62 @@ def test_event_match_non_body(self) -> None:
"pattern should not match before a newline",
)

# boolean true should match
condition = {
"kind": "event_match",
"key": "content.value",
"pattern": "true",
}
self._assert_matches(
condition,
{"value": True},
"patterns should match and be case-insensitive",
)
self._assert_not_matches(
condition,
{"value": False},
"pattern should not match opposite boolean value",
)

# boolean false should match
condition = {
"kind": "event_match",
"key": "content.value",
"pattern": "false",
}
self._assert_matches(
condition,
{"value": False},
"patterns should match and be case-insensitive",
)
self._assert_not_matches(
condition,
{"value": True},
"pattern should not match opposite boolean value",
)

# integers should match
condition = {
"kind": "event_match",
"key": "content.value",
"pattern": "123",
}
self._assert_matches(
condition,
{"value": 123},
"patterns should match and be case-insensitive",
)
self._assert_not_matches(
condition,
{"value": 4123},
"pattern should only match at the start/end of the value",
)
self._assert_not_matches(
condition,
{"value": 1234},
"pattern should only match at the start/end of the value",
)

def test_no_body(self) -> None:
"""Not having a body shouldn't break the evaluator."""
evaluator = self._get_evaluator({})
Expand Down