Sumit Kumar Arora
Sumit Kumar Arora

Reputation: 75

How to convert Source[String, Future[IOResult]] to Source[ByteString, Future[IOResult]]

Need to convert

val graph: Source[String, Future[IOResult]]= <some flow>

to

val graph: Source[ByteString, Future[IOResult]] =<some flow>

so that it can be pass to Result as shown below:

Result(
        header = ResponseHeader(OK, Map(CONTENT_DISPOSITION → s"application")),
        body = HttpEntity.Streamed(graph, None, None)
        )

Any help would be appreciated. Thanks.

Upvotes: 0

Views: 151

Answers (2)

Levi Ramsey
Levi Ramsey

Reputation: 20561

There is a map method on a Source and that Repr[T] is an an alias for a Source[T, Mat] with the same materializer.

So you can make a Source[String, Mat] into a Source[ByteString, Mat] with:

graph.map(ByteString(_))

Upvotes: 1

cbley
cbley

Reputation: 4608

You can use via to transform the elements of your existing source:

val byteStringSource: Source[ByteString, Future[IOResult]] =
  graph.via(Flow.fromFunction(ByteString(_)))

Upvotes: 0

Related Questions