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

[UFC] Use partial instead of full match for regex #37

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions eppo_client/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def evaluate_condition(
if subject_value is not None:
if condition.operator == OperatorType.MATCHES:
return isinstance(condition.value, str) and bool(
re.match(condition.value, str(subject_value))
re.search(condition.value, str(subject_value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)
if condition.operator == OperatorType.NOT_MATCHES:
return isinstance(condition.value, str) and not bool(
re.match(condition.value, str(subject_value))
re.search(condition.value, str(subject_value))
)
elif condition.operator == OperatorType.ONE_OF:
return isinstance(condition.value, list) and str(subject_value).lower() in [
Expand Down
21 changes: 21 additions & 0 deletions test/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ def test_evaluate_condition_matches():
)


def test_evaluate_condition_matches_partial():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

assert evaluate_condition(
Condition(
operator=OperatorType.MATCHES, value="@example\\.com", attribute="email"
),
{"email": "alice@example.com"},
)
assert not evaluate_condition(
Condition(
operator=OperatorType.MATCHES, value="@example\\.com", attribute="email"
),
{"email": "alice@sample.com"},
)
assert evaluate_condition(
Condition(
operator=OperatorType.MATCHES, value="@example\\.com", attribute="email"
),
{"email": "bob@example.com"},
)


def test_evaluate_condition_not_matches():
assert not evaluate_condition(
Condition(
Expand Down
Loading