Scala Cat leftMap似乎无法正确推断类型
我有一个使用Softwaremill sttp客户端发出HTTP请求的函数。我还使用了用于leftMap功能的Cats库。以下是函数:Scala Cat leftMap似乎无法正确推断类型,scala,scala-cats,Scala,Scala Cats,我有一个使用Softwaremill sttp客户端发出HTTP请求的函数。我还使用了用于leftMap功能的Cats库。以下是函数: def tags(repositoryName: String): Either[String, List[Tag]] = { val uri = uri"$gitLabBaseUrl/api/v4/projects/$repositoryName/repository/tags" val request = sttp.get(uri).header(
def tags(repositoryName: String): Either[String, List[Tag]] = {
val uri = uri"$gitLabBaseUrl/api/v4/projects/$repositoryName/repository/tags"
val request = sttp.get(uri).header("PRIVATE-TOKEN", privateToken).response(asJson[List[Tag]])
request
.send()
.body
.flatMap(
_.leftMap(
e => Left(e.message)
)
)
.leftMap(_.toString)
}
如您所见,我希望函数的签名为[String,List[Tag]]。然而,为了实现这一点,我需要做两个leftMap语句,在我的推理中,只有第一个leftMap就足够了。我将语句分解以获得类型签名:
val foo: Id[Response[Either[DeserializationError[circe.Error], List[Tag]]]] = request.send()
val foo2: Either[String, Either[DeserializationError[circe.Error], List[Tag]]] = request.send().body
val foo3: Either[io.Serializable, List[Tag]] = request.send().body.flatMap(_.leftMap(e => Left(e.message)))
正如您所看到的,flatMap返回的类型为[io.Serializable,List[Tag]]。但是,request.send()正文中的签名是:
因此,e.message会产生一个字符串,因此我希望
_.leftMap(e => Left(e.message))
语句已导致
Either[String, List[Tag]]
但它却有一个签名
Either[io.Serializable, List[Tag]]
所以我需要做第二个leftMap来得到正确的签名
Either[String, List[Tag]]
有人能帮我解决一下如何避免做第二张左图吗?这似乎是不必要的,但我还没有找到解决办法
谢谢 试试看
request
.send()
.body
.flatMap { _.left.map(_.message) }
request
.send()
.body
.flatMap { _.left.map(_.message) }