A FlowBuilder provides tools for cunstructing an akka.stream Flow that transforms a
FlowResult into another FlowResult. More explicitly, it is used to construct a
Flow[FlowResult[I, Ctx], FlowResult[T, Ctx], akka.NotUsed]. Here I is thought of as the
initial or input type, T is the terminal or output type, and Ctx is the context type which
remains unchanged throughout the flow.
We prefer to transform FlowResults rather than the raw types I and T because it allows us to
carry along all the extra information contained in the FlowResult. Namely, the context,
the success/failure status, and the metadata. FlowBuilder makes it easy to transform the underlying
types without affecting this context, and will transparently propogate failures.
Example:
val flow: Flow[FlowResult[Int, Int], FlowResult[String, Int], akka.NotUsed] = FlowBuilder.simple[Int]
.map(i => i * 2)
.flatMap {
case i if i < 0 => Failure(new Exception("number is negative!"))
case i => Success(i)
}.mapWithContext((i, context, metadata) => s"Value: $i, Context: $context")
.flow
This starts with an initial Flow containing FlowResults of integers (and with context as the initia int value).
It then:
multiplies the initial value by 2.
if the value is less than 0, converts it to a failure. This means that subsequent steps in the flow will not
be performed.
Uses both the context and the value to produce a new String value.
A FlowBuilder provides tools for cunstructing an akka.stream Flow that transforms a FlowResult into another FlowResult. More explicitly, it is used to construct a Flow[FlowResult[I, Ctx], FlowResult[T, Ctx], akka.NotUsed]. Here I is thought of as the initial or input type, T is the terminal or output type, and Ctx is the context type which remains unchanged throughout the flow.
We prefer to transform FlowResults rather than the raw types I and T because it allows us to carry along all the extra information contained in the FlowResult. Namely, the context, the success/failure status, and the metadata. FlowBuilder makes it easy to transform the underlying types without affecting this context, and will transparently propogate failures.
val flow: Flow[FlowResult[Int, Int], FlowResult[String, Int], akka.NotUsed] = FlowBuilder.simple[Int] .map(i => i * 2) .flatMap { case i if i < 0 => Failure(new Exception("number is negative!")) case i => Success(i) }.mapWithContext((i, context, metadata) => s"Value: $i, Context: $context") .flow This starts with an initial Flow containing FlowResults of integers (and with context as the initia int value). It then: