Skip to content

Commit

Permalink
NEW: support multiple inner markers in Mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Nov 8, 2023
1 parent 3170bd4 commit 13b6a61
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
21 changes: 15 additions & 6 deletions combadge/core/markers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Dict, Mapping, MutableMapping, TypeVar
from typing import Any, Dict, Iterable, Mapping, MutableMapping, TypeVar

# noinspection PyUnresolvedReferences
from typing_extensions import override
Expand Down Expand Up @@ -64,16 +64,25 @@ def __call__(self, response: Any, payload: Mapping[Any, Any]) -> Any: # noqa: D
_MutableMappingT = TypeVar("_MutableMappingT", bound=MutableMapping[Any, Any])


@dataclass
@dataclass(init=False)
class Mixin(ResponseMarker):
"""Mix in the inner marker output to the payload."""
"""Mix in the inner marker outputs to the payload."""

inner: ResponseMarker
"""Inner marker to apply to a payload."""
inner: Iterable[ResponseMarker]

__slots__ = ("inner",)

def __init__(self, *inner: ResponseMarker) -> None:
"""
Initialize the marker.
Args:
*inner: inner markers to apply to a payload
"""
self.inner = inner

@override
def __call__(self, response: Any, payload: _MutableMappingT) -> _MutableMappingT: # noqa: D102
payload.update(self.inner(response, payload))
for marker in self.inner:
payload.update(marker(response, payload))
return payload
15 changes: 10 additions & 5 deletions tests/core/markers/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ def test_map() -> None:


def test_mixin() -> None:
class InnerMarker(ResponseMarker):
class InnerMarker1(ResponseMarker):
def __call__(self, response: Any, payload: Any) -> Any:
return {"inner": "foo"}
return {"inner1": "foo"}

assert Mixin(InnerMarker())(..., {"outer": "bar"}) == {
"inner": "foo",
"outer": "bar",
class InnerMarker2(ResponseMarker):
def __call__(self, response: Any, payload: Any) -> Any:
return {"inner2": "bar"}

assert Mixin(InnerMarker1(), InnerMarker2())(..., {"outer": "qux"}) == {
"inner1": "foo",
"inner2": "bar",
"outer": "qux",
}

0 comments on commit 13b6a61

Please sign in to comment.