Skip to content

Commit

Permalink
Simplify/refactor HENNGE#173
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Jan 5, 2024
1 parent ce6e63f commit 3937f8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
42 changes: 25 additions & 17 deletions src/aiodynamo/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from .errors import CannotAddToNestedField
from .types import AttributeType, Numeric, ParametersDict
from .utils import MinLen2AppendOnlyList, deparametetrize, low_level_serialize
from .utils import deparametetrize, low_level_serialize

_ParametersCache = Dict[Tuple[Any, Any], str]

Expand Down Expand Up @@ -377,20 +377,24 @@ def encode(self, params: Parameters) -> str:

class Condition(metaclass=abc.ABCMeta):
def __and__(self, other: Condition) -> Condition:
if isinstance(self, AndCondition):
if isinstance(other, AndCondition):
return AndCondition(self.children.extending(other.children))
else:
return AndCondition(self.children.appending(other))
return AndCondition(MinLen2AppendOnlyList.create(self, other))
if isinstance(self, AndCondition) and isinstance(other, AndCondition):
return AndCondition(self.children + other.children)
elif isinstance(self, AndCondition):
return AndCondition(self.children + (other,))
elif isinstance(other, AndCondition):
return AndCondition((self,) + other.children)
else:
return AndCondition((self, other))

def __or__(self, other: Condition) -> Condition:
if isinstance(self, OrCondition):
if isinstance(other, OrCondition):
return OrCondition(self.children.extending(other.children))
else:
return OrCondition(self.children.appending(other))
return OrCondition(MinLen2AppendOnlyList.create(self, other))
if isinstance(self, OrCondition) and isinstance(other, OrCondition):
return OrCondition(self.children + other.children)
elif isinstance(self, OrCondition):
return OrCondition(self.children + (other,))
elif isinstance(other, OrCondition):
return OrCondition((self,) + other.children)
else:
return OrCondition((self, other))

def __invert__(self) -> Condition:
return NotCondition(self)
Expand Down Expand Up @@ -418,18 +422,22 @@ def encode(self, params: Parameters) -> str:

@dataclass(frozen=True)
class AndCondition(Condition):
children: MinLen2AppendOnlyList[Condition]
children: tuple[Condition, ...]

def encode(self, params: Parameters) -> str:
return "(" + " AND ".join(child.encode(params) for child in self.children) + ")"
if len(self.children) < 2:
raise ValueError("two or more subclauses are required")
return f"({' AND '.join(child.encode(params) for child in self.children)})"


@dataclass(frozen=True)
class OrCondition(Condition):
children: MinLen2AppendOnlyList[Condition]
children: tuple[Condition, ...]

def encode(self, params: Parameters) -> str:
return "(" + " OR ".join(child.encode(params) for child in self.children) + ")"
if len(self.children) < 2:
raise ValueError("two or more subclauses are required")
return f"({' OR '.join(child.encode(params) for child in self.children)})"


@dataclass(frozen=True)
Expand Down
31 changes: 9 additions & 22 deletions tests/unit/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
ProjectionExpression,
UpdateExpression,
)
from aiodynamo.utils import MinLen2AppendOnlyList


@pytest.mark.parametrize(
Expand Down Expand Up @@ -148,16 +147,12 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
[
(
F("a").equals("a") & F("b").equals("b"),
AndCondition(
MinLen2AppendOnlyList.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
AndCondition((Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b"))),
),
(
(F("a").equals("a") & F("b").equals("b")) & F("c").equals("c"),
AndCondition(
MinLen2AppendOnlyList.create(
(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -168,7 +163,7 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
(F("a").equals("a") & F("b").equals("b"))
& (F("c").equals("c") & F("d").equals("d")),
AndCondition(
MinLen2AppendOnlyList.create(
(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -178,16 +173,12 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
),
(
F("a").equals("a") | F("b").equals("b"),
OrCondition(
MinLen2AppendOnlyList.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
OrCondition((Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b"))),
),
(
(F("a").equals("a") | F("b").equals("b")) | F("c").equals("c"),
OrCondition(
MinLen2AppendOnlyList.create(
(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -198,7 +189,7 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
(F("a").equals("a") | F("b").equals("b"))
| (F("c").equals("c") | F("d").equals("d")),
OrCondition(
MinLen2AppendOnlyList.create(
(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -210,16 +201,12 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
(F("a").equals("a") | F("b").equals("b"))
& (F("c").equals("c") | F("d").equals("d")),
AndCondition(
MinLen2AppendOnlyList.create(
(
OrCondition(
MinLen2AppendOnlyList.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
(Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b"))
),
OrCondition(
MinLen2AppendOnlyList.create(
Comparison(F("c"), "=", "c"), Comparison(F("d"), "=", "d")
)
(Comparison(F("c"), "=", "c"), Comparison(F("d"), "=", "d"))
),
)
),
Expand Down

0 comments on commit 3937f8b

Please sign in to comment.