Skip to content

ChannelVisitor

faststream.redis.testing.ChannelVisitor #

Bases: Visitor

visit #

visit(
    *,
    sub: LogicSubscriber,
    channel: str | None = None,
    list: str | None = None,
    stream: str | None = None,
) -> str | None
Source code in faststream/redis/testing.py
def visit(
    self,
    *,
    sub: "LogicSubscriber",
    channel: str | None = None,
    list: str | None = None,
    stream: str | None = None,
) -> str | None:
    if channel is None or not isinstance(sub, ChannelSubscriber):
        return None

    sub_channel = sub.channel

    if (
        sub_channel.pattern
        and bool(
            re.match(
                # Escaped wholesale and then opened back up at the wildcard,
                # the way `REDIS_ADDRESS_SYNTAX` does it: a Broker address may
                # hold a literal brace, and `{2}` left bare is a quantifier.
                re.escape(sub_channel.name).replace(r"\*", ".*"),
                channel or "",
            ),
        )
    ) or channel == sub_channel.name:
        return channel

    return None

get_message #

get_message(
    channel: str, body: Any, sub: ChannelSubscriber
) -> Any
Source code in faststream/redis/testing.py
def get_message(  # type: ignore[override]
    self,
    channel: str,
    body: Any,
    sub: "ChannelSubscriber",
) -> Any:
    return PubSubMessage(
        type="message",
        data=body,
        channel=channel,
        # Real Redis reports the pattern it was psubscribed with; this keeps
        # reporting the template, as it did before the two were split apart.
        # Aligning it belongs with #2450, which gives the template a meaning.
        pattern=sub.channel.address.template.encode()
        if sub.channel.pattern
        else None,
    )