我正在和 Finch 和 Cats 打交道。我最终遇到了一个问题,即我的服务返回存储库的读取器和作为Reader[Repository, Either[List[String], Entity]].
问题是:我需要以 FP 方式将 Either's Right 值转换为 Finch 的输出。因此,使用 for-expr 将不起作用,因为它将评估为新的 Reader monad。
我看到了一些使用 fold 作为解决方案的实现either.fold[Output[Entity]](NotFound)(Ok),但我不确定它是否对我来说是在我的 Either 和 fold 之间使用这个 Reader 的有效路径。
Finch 的 Endpoint 是一个 Future,所以我想知道我是否将我的 Reader monad 封装在一个 Future 中,我可以将 Either's Right 的可能和最终评估转换为 Finch 的输出。
这是我现在得到的:
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)
}
}
所以,我的想法是将ItemService#create输出转换为Output[Item]on ItemAction#create。Output[Item]是一个Future,所以像这样的签名Future[Reader[?]]可能适合ItemAction但不确定它是否可能和推荐。
关于这个问题有什么想法吗?