Skip to content

Commit

Permalink
MinLen2AppendOnlyList -> SubConditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ojii committed Jan 5, 2024
1 parent b87ccd0 commit 371a1dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
39 changes: 34 additions & 5 deletions src/aiodynamo/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Any,
Callable,
Dict,
Generator,
Iterable,
Iterator,
List,
Optional,
Expand All @@ -19,7 +21,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 @@ -384,7 +386,7 @@ def __and__(self, other: Condition) -> Condition:
return AndCondition(self.children.appending(other))
elif isinstance(other, AndCondition):
return AndCondition(other.children.prepending(self))
return AndCondition(MinLen2AppendOnlyList.create(self, other))
return AndCondition(SubConditions.create(self, other))

def __or__(self, other: Condition) -> Condition:
if isinstance(self, OrCondition):
Expand All @@ -394,7 +396,7 @@ def __or__(self, other: Condition) -> Condition:
return OrCondition(self.children.appending(other))
elif isinstance(other, OrCondition):
return OrCondition(other.children.prepending(self))
return OrCondition(MinLen2AppendOnlyList.create(self, other))
return OrCondition(SubConditions.create(self, other))

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

@dataclass(frozen=True)
class AndCondition(Condition):
children: MinLen2AppendOnlyList[Condition]
children: SubConditions

def encode(self, params: Parameters) -> str:
return "(" + " AND ".join(child.encode(params) for child in self.children) + ")"


@dataclass(frozen=True)
class OrCondition(Condition):
children: MinLen2AppendOnlyList[Condition]
children: SubConditions

def encode(self, params: Parameters) -> str:
return "(" + " OR ".join(child.encode(params) for child in self.children) + ")"
Expand Down Expand Up @@ -671,3 +673,30 @@ def __and__(self, field: F) -> ProjectionExpression:

def encode(self, params: Parameters) -> str:
return ",".join(params.encode_path(field.path) for field in self.fields)


@dataclass(frozen=True)
class SubConditions:
first: Condition
second: Condition
rest: tuple[Condition, ...]

@classmethod
def create(
cls, first: Condition, second: Condition, *rest: Condition
) -> SubConditions:
return cls(first, second, rest)

def prepending(self, value: Condition) -> SubConditions:
return SubConditions(value, self.first, (self.second, *self.rest))

def appending(self, value: Condition) -> SubConditions:
return SubConditions(self.first, self.second, (*self.rest, value))

def extending(self, values: Iterable[Condition]) -> SubConditions:
return SubConditions(self.first, self.second, (*self.rest, *values))

def __iter__(self) -> Generator[Condition, None, None]:
yield self.first
yield self.second
yield from self.rest
29 changes: 0 additions & 29 deletions src/aiodynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import decimal
import logging
from collections import abc as collections_abc
from dataclasses import dataclass
from functools import reduce
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Dict,
Generator,
Generic,
Iterable,
List,
Mapping,
Set,
Expand Down Expand Up @@ -201,28 +197,3 @@ def deparametetrize(
for key, value in params.names.items():
expression = expression.replace(key, value)
return expression


@dataclass(frozen=True)
class MinLen2AppendOnlyList(Generic[T]):
first: T
second: T
rest: tuple[T, ...]

@classmethod
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 __iter__(self) -> Generator[T, None, None]:
yield self.first
yield self.second
yield from self.rest
24 changes: 12 additions & 12 deletions tests/unit/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
OrCondition,
Parameters,
ProjectionExpression,
SubConditions,
UpdateExpression,
)
from aiodynamo.utils import MinLen2AppendOnlyList


@pytest.mark.parametrize(
Expand Down Expand Up @@ -149,15 +149,15 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
(
F("a").equals("a") & F("b").equals("b"),
AndCondition(
MinLen2AppendOnlyList.create(
SubConditions.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
),
(
(F("a").equals("a") & F("b").equals("b")) & F("c").equals("c"),
AndCondition(
MinLen2AppendOnlyList.create(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -167,7 +167,7 @@ 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(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -178,7 +178,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(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -189,15 +189,15 @@ def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
(
F("a").equals("a") | F("b").equals("b"),
OrCondition(
MinLen2AppendOnlyList.create(
SubConditions.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
),
(
(F("a").equals("a") | F("b").equals("b")) | F("c").equals("c"),
OrCondition(
MinLen2AppendOnlyList.create(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -207,7 +207,7 @@ 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(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -218,7 +218,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(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Expand All @@ -230,14 +230,14 @@ 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(
SubConditions.create(
OrCondition(
MinLen2AppendOnlyList.create(
SubConditions.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
OrCondition(
MinLen2AppendOnlyList.create(
SubConditions.create(
Comparison(F("c"), "=", "c"), Comparison(F("d"), "=", "d")
)
),
Expand Down

0 comments on commit 371a1dc

Please sign in to comment.