Skip to content

Commit

Permalink
Move, rename and add reset (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
notxcain authored Nov 1, 2018
1 parent 70bd497 commit 9032fe6
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 96 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To start using Aecor Akka Persistence Runtime add the following to your `build.s
scalaVersion := "2.12.4"
scalacOptions += "-Ypartial-unification"
addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M10" cross CrossVersion.full)
libraryDependencies += "io.aecor" %% "akka-peristence-runtime" % "0.18.0-M1"
libraryDependencies += "io.aecor" %% "akka-peristence-runtime" % "x.y.z" // See current version on the badge above
```

### Entity Behavior Definition
Expand Down Expand Up @@ -126,7 +126,7 @@ Now, the final part before we launch.'

As I said earlier `Subscription[F[_]]` is polymorphic in its effect type.

Our effect would be any `F[_]` with instance of `MonadActionBase[F, Option[SubscriptionState], SubscriptionEvent]` which provides essential operations for eventsources command handler
Our effect would be any `F[_]` with instance of `MonadAction[F, Option[SubscriptionState], SubscriptionEvent]` which provides essential operations for eventsources command handler
* `read: F[Option[SubscriptionState]]` - reads current state
* `append(event: SubscriptionEvent, other: SubscriptionEvent*): F[Unit]` - append one or more events
Other stuff like state recovery and event persistence is held by Akka Persistence Runtime.
Expand All @@ -137,7 +137,7 @@ So lets define `SubscriptionActions`
import cats.implicits._ // needed for syntax like flatMap and unit

final class SubscriptionActions[F[_]](
implicit F: MonadActionBase[F, Option[SubscriptionState], SubscriptionEvent]
implicit F: MonadAction[F, Option[SubscriptionState], SubscriptionEvent]
) extends Subscription[F] {

implicit F._ // import algebra functions
Expand Down
63 changes: 63 additions & 0 deletions modules/core/src/main/scala/aecor/MonadAction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package aecor

import cats.data.EitherT
import cats.{ Applicative, Monad }

trait MonadAction[F[_], S, E] extends Monad[F] {
def read: F[S]
def append(es: E, other: E*): F[Unit]
def reset: F[Unit]
}

trait MonadActionReject[F[_], S, E, R] extends MonadAction[F, S, E] {
def reject[A](r: R): F[A]
}

object MonadActionReject {
implicit def eitherTMonadActionRejectInstance[I[_]: Applicative, F[_], S, E, R](
implicit F: MonadAction[I, S, E],
eitherTMonad: Monad[EitherT[I, R, ?]]
): MonadActionReject[EitherT[I, R, ?], S, E, R] =
new MonadActionReject[EitherT[I, R, ?], S, E, R] {
override def reject[A](r: R): EitherT[I, R, A] = EitherT.leftT(r)
override def read: EitherT[I, R, S] = EitherT.right(F.read)
override def append(es: E, other: E*): EitherT[I, R, Unit] =
EitherT.right(F.append(es, other: _*))
override def reset: EitherT[I, R, Unit] = EitherT.right(F.reset)
override def pure[A](x: A): EitherT[I, R, A] = EitherT.pure[I, R](x)
override def map[A, B](fa: EitherT[I, R, A])(f: A => B): EitherT[I, R, B] = fa.map(f)
override def flatMap[A, B](fa: EitherT[I, R, A])(f: A => EitherT[I, R, B]): EitherT[I, R, B] =
fa.flatMap(f)
override def tailRecM[A, B](a: A)(f: A => EitherT[I, R, Either[A, B]]): EitherT[I, R, B] =
eitherTMonad.tailRecM(a)(f)
}
}

trait MonadActionLift[I[_], F[_], S, E] extends MonadAction[I, S, E] {
def liftF[A](fa: F[A]): I[A]
}

trait MonadActionLiftReject[I[_], F[_], S, E, R]
extends MonadActionLift[I, F, S, E]
with MonadActionReject[I, S, E, R]

object MonadActionLiftReject {
implicit def eitherTMonadActionLiftRejectInstance[I[_], F[_], S, E, R](
implicit I: MonadActionLift[I, F, S, E],
eitherTMonad: Monad[EitherT[I, R, ?]]
): MonadActionLiftReject[EitherT[I, R, ?], F, S, E, R] =
new MonadActionLiftReject[EitherT[I, R, ?], F, S, E, R] {
override def reject[A](r: R): EitherT[I, R, A] = EitherT.leftT(r)
override def liftF[A](fa: F[A]): EitherT[I, R, A] = EitherT.right(I.liftF(fa))
override def read: EitherT[I, R, S] = EitherT.right(I.read)
override def append(es: E, other: E*): EitherT[I, R, Unit] =
EitherT.right(I.append(es, other: _*))
override def reset: EitherT[I, R, Unit] = EitherT.right(I.reset)
override def pure[A](x: A): EitherT[I, R, A] = EitherT.pure[I, R](x)
override def map[A, B](fa: EitherT[I, R, A])(f: A => B): EitherT[I, R, B] = fa.map(f)
override def flatMap[A, B](fa: EitherT[I, R, A])(f: A => EitherT[I, R, B]): EitherT[I, R, B] =
fa.flatMap(f)
override def tailRecM[A, B](a: A)(f: A => EitherT[I, R, Either[A, B]]): EitherT[I, R, B] =
eitherTMonad.tailRecM(a)(f)
}
}
42 changes: 23 additions & 19 deletions modules/core/src/main/scala/aecor/data/ActionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cats.{ Applicative, Functor, Monad, MonadError, ~> }
import cats.data.{ Chain, NonEmptyChain }
import cats.implicits._
import Folded.syntax._
import aecor.{ MonadActionLift, MonadActionLiftReject }

final class ActionT[F[_], S, E, A] private (
val unsafeRun: (S, (S, E) => Folded[S], Chain[E]) => F[Folded[(Chain[E], A)]]
Expand Down Expand Up @@ -67,6 +68,9 @@ trait ActionTFunctions {
def append[F[_]: Applicative, S, E](e: NonEmptyChain[E]): ActionT[F, S, E, Unit] =
ActionT((_, _, es0) => (es0 ++ e.toChain, ()).next.pure[F])

def reset[F[_]: Applicative, S, E]: ActionT[F, S, E, Unit] =
ActionT((_, _, _) => (Chain.empty[E], ()).next.pure[F])

def liftF[F[_]: Functor, S, E, A](fa: F[A]): ActionT[F, S, E, A] =
ActionT((_, _, es) => fa.map(a => (es, a).next))

Expand Down Expand Up @@ -99,30 +103,25 @@ trait ActionTFunctions {
}
}

trait ActionTInstances extends ActionNLowerPriorityInstances {
implicit def monadActionInstance[F[_], S, E, R](
implicit F: MonadError[F, R]
): MonadAction[ActionT[F, S, E, ?], F, S, E, R] =
new ActionNMonadActionLiftInstance[F, S, E]()
with MonadAction[ActionT[F, S, E, ?], F, S, E, R] {
override def reject[A](r: R): ActionT[F, S, E, A] = liftF(F.raiseError(r))
override def handleErrorWith[A](
fa: ActionT[F, S, E, A]
)(f: R => ActionT[F, S, E, A]): ActionT[F, S, E, A] = ActionT { (s, u, es) =>
F.handleErrorWith(fa.unsafeRun(s, u, es)) { r =>
f(r).unsafeRun(s, u, es)
}
}
trait ActionTInstances extends ActionTLowerPriorityInstances1 {
implicit def monadActionLiftRejectInstance[F[_], S, E, R](
implicit F0: MonadError[F, R]
): MonadActionLiftReject[ActionT[F, S, E, ?], F, S, E, R] =
new MonadActionLiftReject[ActionT[F, S, E, ?], F, S, E, R]
with ActionTMonadActionLiftInstance[F, S, E] {
override protected implicit def F: Monad[F] = F0
override def reject[A](r: R): ActionT[F, S, E, A] = ActionT.liftF(F0.raiseError[A](r))
}
}

trait ActionNLowerPriorityInstances {
abstract class ActionNMonadActionLiftInstance[F[_], S, E](implicit F: Monad[F])
trait ActionTLowerPriorityInstances1 {
trait ActionTMonadActionLiftInstance[F[_], S, E]
extends MonadActionLift[ActionT[F, S, E, ?], F, S, E] {
protected implicit def F: Monad[F]
override def read: ActionT[F, S, E, S] = ActionT.read
override def append(e: E, es: E*): ActionT[F, S, E, Unit] =
ActionT.append(NonEmptyChain(e, es: _*))
override def liftF[A](fa: F[A]): ActionT[F, S, E, A] = ActionT.liftF(fa)
override def reset: ActionT[F, S, E, Unit] = ActionT.reset
override def map[A, B](fa: ActionT[F, S, E, A])(f: A => B): ActionT[F, S, E, B] =
fa.map(f)

Expand All @@ -149,9 +148,14 @@ trait ActionNLowerPriorityInstances {
}
}
override def pure[A](x: A): ActionT[F, S, E, A] = ActionT.pure(x)
override def liftF[A](fa: F[A]): ActionT[F, S, E, A] = ActionT.liftF(fa)
}

implicit def monadActionLiftInstance[F[_], S, E](
implicit F: Monad[F]
implicit F0: Monad[F]
): MonadActionLift[ActionT[F, S, E, ?], F, S, E] =
new ActionNMonadActionLiftInstance[F, S, E] {}
new ActionTMonadActionLiftInstance[F, S, E] {
override protected implicit def F: Monad[F] = F0
}

}
53 changes: 0 additions & 53 deletions modules/core/src/main/scala/aecor/data/MonadAction.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aecor.example.account

import aecor.MonadActionReject
import aecor.data.Folded.syntax._
import aecor.data._
import aecor.example.account.AccountEvent._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aecor.example.transaction

import aecor.MonadActionReject
import aecor.data.Folded.syntax._
import aecor.data._
import aecor.example.account.AccountId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package aecor.schedule

import java.time.{ Instant, LocalDateTime, ZoneOffset, ZonedDateTime }

import aecor.data.{ ActionT, EventsourcedBehavior, Folded, MonadActionLift }
import aecor.MonadActionLift
import aecor.data.{ ActionT, EventsourcedBehavior, Folded }
import aecor.data.Folded.syntax._
import aecor.runtime.akkapersistence.serialization.PersistentDecoder.DecodingResult
import aecor.runtime.akkapersistence.serialization.{
Expand Down
5 changes: 3 additions & 2 deletions modules/tests/src/main/scala/aecor/tests/e2e/Counter.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aecor.tests.e2e

import aecor.MonadAction
import aecor.data._
import aecor.data.Folded.syntax._
import aecor.macros.boopickleWireProtocol
Expand Down Expand Up @@ -52,7 +53,7 @@ object CounterBehavior {
)
}

final class CounterActions[F[_]](implicit F: MonadActionBase[F, CounterState, CounterEvent])
final class CounterActions[F[_]](implicit F: MonadAction[F, CounterState, CounterEvent])
extends Counter[F] {

import F._
Expand All @@ -66,6 +67,6 @@ final class CounterActions[F[_]](implicit F: MonadActionBase[F, CounterState, Co
}

object CounterActions {
def apply[F[_]](implicit F: MonadActionBase[F, CounterState, CounterEvent]): Counter[F] =
def apply[F[_]](implicit F: MonadAction[F, CounterState, CounterEvent]): Counter[F] =
new CounterActions[F]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aecor.tests.e2e

import aecor.MonadAction
import aecor.data.Folded.syntax._
import aecor.data._
import aecor.data.EventsourcedBehavior
Expand Down Expand Up @@ -34,7 +35,7 @@ object notification {
}

def notificationActions[F[_]](
implicit F: MonadActionBase[F, NotificationState, NotificationEvent]
implicit F: MonadAction[F, NotificationState, NotificationEvent]
): Notification[F] = new Notification[F] {
import F._
override def create(counterId: CounterId): F[Unit] = append(NotificationCreated(counterId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package aecor.tests

import aecor.MonadAction
import aecor.data._
import aecor.tests.e2e.CounterEvent.{ CounterDecremented, CounterIncremented }
import aecor.tests.e2e.{ Counter, CounterEvent, CounterState }
import cats.Id
import cats.{ Id, Monad }
import org.scalatest.{ FlatSpec, Matchers }
import cats.implicits._

class EventsourcedBehaviorTSpec extends FlatSpec with Matchers {
class EventsourcedBehaviorSpec extends FlatSpec with Matchers {

class CounterOptionalActions[F[_]](
implicit F: MonadActionBase[F, Option[CounterState], CounterEvent]
class CounterOptionalActions[F[_]: Monad](
implicit F: MonadAction[F, Option[CounterState], CounterEvent]
) extends Counter[F] {

import F._
Expand All @@ -22,22 +23,10 @@ class EventsourcedBehaviorTSpec extends FlatSpec with Matchers {
def value: F[Long] = read.map(_.fold(0l)(_.value))
}

object CounterOptionalActions {
def apply[F[_]](
implicit F: MonadActionBase[F, Option[CounterState], CounterEvent]
): Counter[F] =
new CounterOptionalActions[F]
}

"EventsourcedBehavior.optional" should "correctly use init function applying events" in {
val behavior: EventsourcedBehavior[Counter, Id, Option[CounterState], CounterEvent] =
EventsourcedBehavior
.optional(
CounterOptionalActions[ActionT[Id, Option[CounterState], CounterEvent, ?]],
e => CounterState(0L).applyEvent(e),
_.applyEvent(_)
)

.optional(new CounterOptionalActions, e => CounterState(0L).applyEvent(e), _.applyEvent(_))
behavior
.update(behavior.create, CounterEvent.CounterIncremented)
.toOption
Expand Down

0 comments on commit 9032fe6

Please sign in to comment.