Jay Felix
Jay Felix

Reputation: 31

Combining Future with Kleisli and Either on Finch endpoint

I’m hacking a bit with Finch and Cats. I ended up with an issue where my Service returns a Reader of Repository and Either as Reader[Repository, Either[List[String], Entity]].

The problem is: I need to transform the Either’s Right value to a Finch’s Output in a FP way. So, using for-expr won’t work because it will evaluates to a new Reader monad.

I saw a few implementations using fold as a solution like either.fold[Output[Entity]](NotFound)(Ok) - but I am not sure if its a valid path for me with this Reader between my Either and fold.

Finch’s Endpoint is a Future, so I wonder if I encapsulate my Reader monad within a Future, I could transform the possible and eventual evaluation of Either’s Right to a Finch’s Output.

Here's what I got now:

object ItemAction {
  def routes: Endpoint[String] = post("todo" :: "items" :: jsonBody[Item]) { create _ }

  def create(i: Item): Output[Item] = ??? 
}

object ItemService {
  def create(item: Item): Reader[ItemRepository, Either[String, Item]] = Reader { (repository: ItemRepository) =>
    repository.create(item)
  }
}

So, my idea is to transform ItemService#create output into Output[Item] on ItemAction#create. Output[Item] is a Future, so a signature like Future[Reader[?]] could fits into ItemAction but not sure if its possible and recommended.

Any ideas on this matter?

Upvotes: 3

Views: 252

Answers (0)

Related Questions