Open mdedetrich opened 8 months ago
And seems you can do this with lazyFutureSink
and prefixAndTail
or Sink.lazyInit
too.
IIRC, in reactor-core, there is a SwitchOnFirst
method, which the Flux including the first element, do you think we should add that method too, which I think is much easier to use.
This is another commonplace problem that happens when using pekko-streams which arises when you have a
Sink
that has some parameters (i.e. lets say anid
) but the data to create thatid
is in the stream that is being sent to theSink
itself.More concretely, lets assume we have a simple sink that stores some data in some object storage, i.e.
def putData(id: String): Sink[ByteString, _]
. In order to callputData
we need anid
however that is contained within the data being sent to the sink itself, i.e.In this case we want the extract the id (i.e.
???
in above code snippet) from the actual data (i.e. "id:1"). Most importantly is that assuming we do figure out a way to extract the id, we still want all of the data (i.e."id:1;moreData"
) to be sent to theSink
.Now while there are tools to do this, i.e.
Framing
they aren't exactly idea. Framing usually only works when you have a very basic/primitive structure (i.e. lets say comma or new line delimited). If we have more complex data structures, lets say JSON we do have options available but solving the issue is still messy because honestlyFraming
is not really the right tool, i.e. its not that we want to frame the data coming in in a certain way but rather we wan't to consume + buffer the data until we manage to extract some data (i.e. theid
) field and then once thatid
field is extract then send the buffered data along with the rest of the incoming data to a suppliedSink
, i.e.The design of this is still debatable, i.e. of particular note is how to model the
extractor
part. My initial thought was to haveextractor
as aSink
but I realized that practically creating aSink
from aFlow
(which is how the extractor's are going to be typically designed) is quite cumbersome so I came up with this API instead, the usage would look like thisWith the above following example, we would extract the "someId" field from
"id"
usingJsonReader.select("$.id")
and then pass thesomeId
into theputData
sink argument and the entire contents of theSource
will be fed intoputData
Sink
sinceinclusive = true
.@He-Pin @pjfanning @jrudolph @raboof @jxnu-liguobin Thoughts?