3

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?

Jay Felix
  • 31
  • 3
  • Could you show us some code you've got by now? `Reader[Repository, Either[List[String], Entity]]` is effectively a wrapper around a function `Repository => Either[List[String], Entity]` so in the end of the day someone has to run it passing in a `Repository`. Who is supposed to do it (particularly who is supposed to know how to get proper `Repository`)? – SergGr Dec 19 '17 at 05:58
  • @SergGr done! Using this Reader monad in my `ItemService` I want to configure what Repository implementation my Action will pass into Service. I will get this implementation from some sort of configuration or injector object somewhere before Action get it running. At least, it's how I thought in solve this problem - not sure if its a good idea in the end haha! Thanks! – Jay Felix Dec 19 '17 at 11:17

0 Answers0