-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Send type hint parameter to udf when a Stream is defined (#202
- Loading branch information
1 parent
67e6165
commit 98dd6df
Showing
12 changed files
with
782 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 9 additions & 2 deletions
11
examples/recommended-worker-app/recommended_worker_app/streams.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import logging | ||
|
||
from kstreams import ConsumerRecord, stream | ||
from kstreams import ConsumerRecord, Send, stream | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@stream("local--hello-world", group_id="example-group") | ||
async def consume(cr: ConsumerRecord) -> None: | ||
async def consume(cr: ConsumerRecord, send: Send) -> None: | ||
logger.info(f"showing bytes: {cr.value}") | ||
value = f"Event confirmed. {cr.value}" | ||
|
||
await send( | ||
"local--kstreams", | ||
value=value.encode(), | ||
key="1", | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import inspect | ||
import sys | ||
import typing | ||
|
||
from kstreams import ConsumerRecord, types | ||
from kstreams.streams import Stream | ||
from kstreams.streams_utils import UDFType, setup_type | ||
|
||
from .middleware import BaseMiddleware | ||
|
||
if sys.version_info < (3, 10): | ||
|
||
async def anext(async_gen: typing.AsyncGenerator): | ||
return await async_gen.__anext__() | ||
|
||
|
||
class UdfHandler(BaseMiddleware): | ||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
signature = inspect.signature(self.next_call) | ||
self.params = list(signature.parameters.values()) | ||
self.type: UDFType = setup_type(self.params) | ||
|
||
def bind_udf_params(self, cr: ConsumerRecord) -> typing.List: | ||
# NOTE: When `no typing` support is deprecated then this can | ||
# be more eficient as the CR will be always there. | ||
ANNOTATIONS_TO_PARAMS = { | ||
ConsumerRecord: cr, | ||
Stream: self.stream, | ||
types.Send: self.send, | ||
} | ||
|
||
return [ANNOTATIONS_TO_PARAMS[param.annotation] for param in self.params] | ||
|
||
async def __call__(self, cr: ConsumerRecord) -> typing.Any: | ||
""" | ||
Call the coroutine `async def my_function(...)` defined by the end user | ||
in a proper way according to its parameters. The `handler` is the | ||
coroutine defined by the user. | ||
Use cases: | ||
1. UDFType.CR_ONLY_TYPING: Only ConsumerRecord with typing | ||
@stream_engine.stream(topic, name="my-stream") | ||
async def consume(cr: ConsumerRecord): | ||
... | ||
2. UDFType.ALL_TYPING: ConsumerRecord and Stream with typing. | ||
The order is important as they are arguments and not kwargs | ||
@stream_engine.stream(topic, name="my-stream") | ||
async def consume(cr: ConsumerRecord, stream: Stream): | ||
... | ||
""" | ||
params = self.bind_udf_params(cr) | ||
|
||
if inspect.isasyncgenfunction(self.next_call): | ||
return await anext(self.next_call(*params)) | ||
return await self.next_call(*params) |
Oops, something went wrong.