diff --git a/src/aiodynamo/expressions.py b/src/aiodynamo/expressions.py index c7e6fc8..08a4354 100644 --- a/src/aiodynamo/expressions.py +++ b/src/aiodynamo/expressions.py @@ -382,6 +382,8 @@ def __and__(self, other: Condition) -> Condition: return AndCondition(self.children.extending(other.children)) else: return AndCondition(self.children.appending(other)) + elif isinstance(other, AndCondition): + return AndCondition(other.children.prepending(self)) return AndCondition(MinLen2AppendOnlyList.create(self, other)) def __or__(self, other: Condition) -> Condition: @@ -390,6 +392,8 @@ def __or__(self, other: Condition) -> Condition: return OrCondition(self.children.extending(other.children)) else: return OrCondition(self.children.appending(other)) + elif isinstance(other, OrCondition): + return OrCondition(other.children.prepending(self)) return OrCondition(MinLen2AppendOnlyList.create(self, other)) def __invert__(self) -> Condition: diff --git a/src/aiodynamo/utils.py b/src/aiodynamo/utils.py index 813a6d4..52a6dd4 100644 --- a/src/aiodynamo/utils.py +++ b/src/aiodynamo/utils.py @@ -213,31 +213,16 @@ class MinLen2AppendOnlyList(Generic[T]): def create(cls, first: T, second: T, *rest: T) -> MinLen2AppendOnlyList[T]: return cls(first, second, rest) + def prepending(self, value: T) -> MinLen2AppendOnlyList[T]: + return MinLen2AppendOnlyList(value, self.first, (self.second, *self.rest)) + def appending(self, value: T) -> MinLen2AppendOnlyList[T]: return MinLen2AppendOnlyList(self.first, self.second, (*self.rest, value)) def extending(self, values: Iterable[T]) -> MinLen2AppendOnlyList[T]: return MinLen2AppendOnlyList(self.first, self.second, (*self.rest, *values)) - def __contains__(self, item: Any) -> bool: - return item == self.first or item == self.second or item in self.rest - - def __getitem__(self, index: int) -> T: - if index == 0: - return self.first - elif index == 1: - return self.second - return self.rest[index - 2] - - def __len__(self) -> int: - return len(self.rest) + 2 - def __iter__(self) -> Generator[T, None, None]: yield self.first yield self.second yield from self.rest - - def __reversed__(self) -> Generator[T, None, None]: - yield from reversed(self.rest) - yield self.second - yield self.first diff --git a/tests/unit/test_expressions.py b/tests/unit/test_expressions.py index 1662bd5..8ef3004 100644 --- a/tests/unit/test_expressions.py +++ b/tests/unit/test_expressions.py @@ -164,6 +164,16 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None: ) ), ), + ( + 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"), + ) + ), + ), ( (F("a").equals("a") & F("b").equals("b")) & (F("c").equals("c") & F("d").equals("d")), @@ -194,6 +204,16 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None: ) ), ), + ( + 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"), + ) + ), + ), ( (F("a").equals("a") | F("b").equals("b")) | (F("c").equals("c") | F("d").equals("d")),