-
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.
Co-authored-by: v.miroshnikov <v.miroshnikov@tinkoff.ru>
- Loading branch information
1 parent
765235f
commit a98f373
Showing
5 changed files
with
75 additions
and
64 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
saga-core/src/main/scala/io/github/vlmiroshnikov/saga/Direction.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,5 @@ | ||
package io.github.vlmiroshnikov.saga | ||
|
||
enum Direction[F[_], A]: | ||
case Forward(value: A, rollback: F[Unit]) extends Direction[F, A] | ||
case Rollback(value: Throwable, rollback: F[Unit]) extends Direction[F, A] |
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
6 changes: 6 additions & 0 deletions
6
saga-core/src/main/scala/io/github/vlmiroshnikov/saga/Step.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,6 @@ | ||
package io.github.vlmiroshnikov.saga | ||
|
||
enum Step[F[_], A]: | ||
case Pure[F[_], A](value: A) extends Step[F, A] | ||
case Next[F[_], A](action: F[A], compensate: Either[Throwable, A] => F[Unit]) extends Step[F, A] | ||
case FlatMap[F[_], A, B](fa: Step[F, A], f: A => Step[F, B]) extends Step[F, B] |
47 changes: 47 additions & 0 deletions
47
saga-core/src/main/scala/io/github/vlmiroshnikov/saga/Stepper.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,47 @@ | ||
package io.github.vlmiroshnikov.saga | ||
|
||
import cats.* | ||
import cats.syntax.all.* | ||
|
||
trait Stepper[F[_]]: | ||
def run[A](saga: Step[F, A]): F[A] | ||
|
||
object Stepper: | ||
|
||
def default[F[_]: MonadThrow]: Stepper[F] = new Stepper[F] { | ||
val F = MonadThrow[F] | ||
|
||
override def run[A](saga: Step[F, A]): F[A] = { | ||
|
||
def go[X](step: Step[F, X]): F[Direction[F, X]] = | ||
step match | ||
case Step.Pure(value) => Direction.Forward(value, F.unit).pure[F] | ||
case Step.Next(action, compensate) => | ||
action.attempt.flatMap { result => | ||
result match | ||
case Left(e) => Direction.Rollback(e, compensate(result)).pure[F] | ||
case Right(v) => Direction.Forward(v, compensate(result)).pure[F] | ||
} | ||
|
||
case Step.FlatMap(fa: Step[F, _], cont: (Any => Step[F, X])) => | ||
go(fa).flatMap { | ||
case Direction.Forward(v, prevRollback: F[Unit]) => | ||
go(cont(v)).attempt.flatMap { | ||
case Right(Direction.Forward(r, rollback)) => | ||
Direction.Forward(r, rollback *> prevRollback).pure[F] | ||
case Right(Direction.Rollback(e, rollback)) => | ||
Direction.Rollback(e, rollback *> prevRollback).pure[F] | ||
case Left(e) => | ||
F.raiseError(e) | ||
} | ||
case err @ Direction.Rollback(_, _) => | ||
err.asInstanceOf[Direction[F, X]].pure[F] | ||
} | ||
|
||
go(saga).flatMap { | ||
case Direction.Forward(a, _) => a.pure[F] | ||
case Direction.Rollback(e, rollback) => | ||
rollback *> F.raiseError(e) | ||
} | ||
} | ||
} |
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