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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
@override
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
@override
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,
    )