Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Merged
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
30 changes: 21 additions & 9 deletions eppo_client/rules.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import numbers
import re
import semver
from enum import Enum
from typing import Any, List

import semver

from eppo_client.models import SdkBaseModel
from eppo_client.types import ConditionValueType, SubjectAttributes
from eppo_client.types import AttributeType, ConditionValueType, SubjectAttributes


class OperatorType(Enum):
Expand Down Expand Up @@ -49,20 +51,20 @@ def evaluate_condition(
if subject_value is not None:
if condition.operator == OperatorType.MATCHES:
return isinstance(condition.value, str) and bool(
re.search(condition.value, str(subject_value))
re.search(condition.value, to_string(subject_value))
)
if condition.operator == OperatorType.NOT_MATCHES:
elif condition.operator == OperatorType.NOT_MATCHES:
return isinstance(condition.value, str) and not bool(
re.search(condition.value, str(subject_value))
re.search(condition.value, to_string(subject_value))
)
elif condition.operator == OperatorType.ONE_OF:
return isinstance(condition.value, list) and str(subject_value) in [
return isinstance(condition.value, list) and to_string(subject_value) in [
str(value) for value in condition.value
]
elif condition.operator == OperatorType.NOT_ONE_OF:
return isinstance(condition.value, list) and str(subject_value) not in [
str(value) for value in condition.value
]
return isinstance(condition.value, list) and to_string(
subject_value
) not in [str(value) for value in condition.value]
else:
# Numeric operator: value could be numeric or semver.
if isinstance(subject_value, numbers.Number):
Expand Down Expand Up @@ -119,3 +121,13 @@ def compare_semver(
return semver.compare(attribute_value, condition_value) <= 0

return False


def to_string(value: AttributeType) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Handle an array of strings?

Copy link
Member

Choose a reason for hiding this comment

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

null?

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

You could normalize boolean, None and arrays by converting to JSON. Just about every language has some library that can convert to JSON, so this would work for every SDK:
Screenshot 2024-06-07 at 8 25 54 AM

So the logic might be:

if isinstance(value, str):
  return value
elif value.is_integer():
  return str(int(value)) 
return json.dumps(value)

Copy link
Author

Choose a reason for hiding this comment

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

AttributeType = Union[str, int, float, bool]

Attribute cannot be an array -- but good call that we do want to handle null.

Using json is a great suggestion, but I do want to make sure we aren't losing a lot of performance (I've seen json being very slow, but perhaps here it's sufficiently quickly given that the input is small)

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

Yeah, that's a valid concern. It still might be useful as an "else" case for any value that we don't expect (sort of a cross-language toString()), while handling the cases that we expect in a more performant way, but I'm good with whatever you decide so long as we handle None / null

Copy link
Author

Choose a reason for hiding this comment

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

Screenshot 2024-06-07 at 10 30 35 AM

Seems like using json is equally fast

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

it looks like they're equal because they both landed on if isinstance(value, str):. You should try it out with no conditionals -- just running the functions we want tested: (str vs json.dumps)

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

But in any case, the json.dumps only happens after all other conditionals fail, so we're still good here.

Copy link
Author

Choose a reason for hiding this comment

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

Oh good catch 😓

if isinstance(value, str):
return value
elif isinstance(value, bool):
return "true" if value else "false"
elif isinstance(value, float):
return f"{value:.0f}" if value.is_integer() else str(value)
return json.dumps(value)
2 changes: 1 addition & 1 deletion eppo_client/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List, Union

ValueType = Union[str, int, float, bool]
AttributeType = Union[str, int, float, bool]
AttributeType = Union[str, int, float, bool, None]
ConditionValueType = Union[AttributeType, List[AttributeType]]
SubjectAttributes = Dict[str, AttributeType]
Action = str
40 changes: 38 additions & 2 deletions test/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Condition,
evaluate_condition,
matches_rule,
to_string,
)

greater_than_condition = Condition(operator=OperatorType.GT, value=10, attribute="age")
Expand Down Expand Up @@ -137,10 +138,22 @@ def test_evaluate_condition_matches():
Condition(operator=OperatorType.MATCHES, value="^test.*", attribute="email"),
{"email": "test@example.com"},
)
assert evaluate_condition(
Condition(operator=OperatorType.MATCHES, value="true", attribute="flag"),
{"flag": True},
)
assert evaluate_condition(
Condition(operator=OperatorType.MATCHES, value="false", attribute="flag"),
{"flag": False},
)
assert not evaluate_condition(
Condition(operator=OperatorType.MATCHES, value="^test.*", attribute="email"),
{"email": "example@test.com"},
)
assert not evaluate_condition(
Condition(operator=OperatorType.MATCHES, value="False", attribute="flag"),
{"flag": False},
)


def test_evaluate_condition_matches_partial():
Expand Down Expand Up @@ -348,10 +361,10 @@ def test_evaluate_condition_one_of_int():

def test_evaluate_condition_one_of_boolean():
one_of_condition_boolean = Condition(
operator=OperatorType.ONE_OF, value=[True, False], attribute="status"
operator=OperatorType.ONE_OF, value=["true", "false"], attribute="status"
)
assert evaluate_condition(one_of_condition_boolean, {"status": False})
assert evaluate_condition(one_of_condition_boolean, {"status": "False"})
assert evaluate_condition(one_of_condition_boolean, {"status": "false"})
assert not evaluate_condition(one_of_condition_boolean, {"status": "Maybe"})
assert not evaluate_condition(one_of_condition_boolean, {"status": 0})
assert not evaluate_condition(one_of_condition_boolean, {"status": 1})
Expand Down Expand Up @@ -391,3 +404,26 @@ def test_is_not_null_operator():
assert not evaluate_condition(is_not_null_condition, {"size": None})
assert evaluate_condition(is_not_null_condition, {"size": 10})
assert not evaluate_condition(is_not_null_condition, {})


def test_to_string_string():
assert to_string("test") == "test"


def test_to_string_int():
assert to_string(10) == "10"


def test_to_string_float():
assert to_string(10.5) == "10.5"
assert to_string(10.0) == "10"
assert to_string(123456789.0) == "123456789"


def test_to_string_bool():
assert to_string(True) == "true"
assert to_string(False) == "false"


def test_to_string_null():
assert to_string(None) == "null"