forked from business4s/workflows4s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
340 additions
and
277 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
33 changes: 33 additions & 0 deletions
33
src/main/scala/workflow4s/wio/builders/BranchBuilder.scala
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,33 @@ | ||
package workflow4s.wio.builders | ||
|
||
import cats.implicits.catsSyntaxOptionId | ||
import workflow4s.wio.{WIO, *} | ||
import workflow4s.wio.model.ModelUtils | ||
|
||
object BranchBuilder { | ||
|
||
trait Step0[Ctx <: WorkflowContext]() { | ||
|
||
def branch[In]: BranchBuilderStep1[In] = BranchBuilderStep1() | ||
|
||
case class BranchBuilderStep1[In]() { | ||
|
||
def when[Err, Out <: WCState[Ctx]](cond: In => Boolean)(wio: WIO[In, Err, Out, Ctx]): Step2[Unit, Err, Out] = | ||
Step2(cond.andThen(Option.when(_)(())), wio.transformInput((x: (In, Any)) => x._1), None) | ||
|
||
def create[T, Err, Out <: WCState[Ctx]](cond: In => Option[T])(wio: WIO[(In, T), Err, Out, Ctx]): Step2[T, Err, Out] = | ||
Step2(cond, wio, None) | ||
|
||
case class Step2[T, Err, Out <: WCState[Ctx]](cond: In => Option[T], wio: WIO[(In, T), Err, Out, Ctx], name: Option[String]) { | ||
|
||
def named(name: String): Step2[T, Err, Out] = this.copy(name = name.some) | ||
def autoNamed()(using n: sourcecode.Name): Step2[T, Err, Out] = named(ModelUtils.prettifyName(n.value)) | ||
|
||
def done: WIO.Branch[In, Err, Out, Ctx] = WIO.Branch(cond, wio, name) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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,51 @@ | ||
package workflow4s.wio.builders | ||
|
||
import cats.implicits.catsSyntaxOptionId | ||
import workflow4s.wio.* | ||
import workflow4s.wio.WIO.Timer | ||
import workflow4s.wio.WIO.Timer.DurationSource | ||
import workflow4s.wio.internal.EventHandler | ||
import workflow4s.wio.model.ModelUtils | ||
|
||
import java.time.Instant | ||
import scala.jdk.DurationConverters.* | ||
import scala.reflect.ClassTag | ||
|
||
object ForkBuilder { | ||
|
||
trait Step0[Ctx <: WorkflowContext]() { | ||
|
||
def fork[In]: ForkBuilder[In, Nothing, Nothing] = ForkBuilder(Vector(), None) | ||
|
||
case class ForkBuilder[-In, +Err, +Out <: WCState[Ctx]](branches: Vector[WIO.Branch[In, Err, Out, Ctx]], name: Option[String]) { | ||
def onSome[T, Err1 >: Err, Out1 >: Out <: WCState[Ctx], In1 <: In](cond: In1 => Option[T])( | ||
wio: WIO[(In1, T), Err1, Out1, Ctx], | ||
name: String = null, | ||
): ForkBuilder[In1, Err1, Out1] = addBranch(WIO.Branch(cond, wio, Option(name))) | ||
|
||
// here we can add some APIs for exhaustive handling of Booleans or Eithers. | ||
|
||
def matchCondition[T, Err1 >: Err, Out1 >: Out <: WCState[Ctx], In1 <: In](condition: In1 => Boolean, name: String = null)( | ||
onTrue: WIO[In1, Err1, Out1, Ctx], | ||
onFalse: WIO[In1, Err1, Out1, Ctx], | ||
): WIO[In1, Err1, Out1, Ctx] = WIO.Fork( | ||
Vector( | ||
WIO.build[Ctx].branch[In1].when(condition)(onTrue).named("Yes").done, | ||
WIO.build[Ctx].branch[In1].when(condition.andThen(!_))(onFalse).named("No").done, | ||
), | ||
Option(name), | ||
) | ||
|
||
def addBranch[T, Err1 >: Err, Out1 >: Out <: WCState[Ctx], In1 <: In]( | ||
b: WIO.Branch[In1, Err1, Out1, Ctx], | ||
): ForkBuilder[In1, Err1, Out1] = this.copy(branches = branches.appended(b)) | ||
|
||
def named(name: String): ForkBuilder[In, Err, Out] = this.copy(name = name.some) | ||
def autoNamed()(using n: sourcecode.Name): ForkBuilder[In, Err, Out] = named(ModelUtils.prettifyName(n.value)) | ||
|
||
def done: WIO[In, Err, Out, Ctx] = WIO.Fork(branches, name) | ||
} | ||
|
||
} | ||
|
||
} |
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
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
Oops, something went wrong.