Cats leftMap 似乎无法正确推断类型
Cats leftMap seems to be unable to correctly infer type
我有一个使用 Softwaremill sttp 客户端发出 HTTP 请求的功能。我还使用 Cats 库来实现 leftMap 功能。这是函数:
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)
}
如您所见,我希望函数的签名为 Either[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 返回的 Either 的类型为 [io.Serializable,List[Tag]]。然而,来自 request.send().body 的签名是:
Either[String, Either[DeserializationError[circe.Error], List[Tag]]]
因此 e.message 结果是一个字符串,因此我期待
_.leftMap(e => Left(e.message))
声明已经导致
Either[String, List[Tag]]
但它有签名
Either[io.Serializable, List[Tag]]
所以我需要做第二个 leftMap 来得到
的正确签名
Either[String, List[Tag]]
有人可以帮我解决如何避免必须执行第二个 leftMap 的问题吗?好像没必要,但是一直想不出怎么解决。
谢谢!
尝试
request
.send()
.body
.flatMap { _.left.map(_.message) }
我有一个使用 Softwaremill sttp 客户端发出 HTTP 请求的功能。我还使用 Cats 库来实现 leftMap 功能。这是函数:
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)
}
如您所见,我希望函数的签名为 Either[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 返回的 Either 的类型为 [io.Serializable,List[Tag]]。然而,来自 request.send().body 的签名是:
Either[String, Either[DeserializationError[circe.Error], List[Tag]]]
因此 e.message 结果是一个字符串,因此我期待
_.leftMap(e => Left(e.message))
声明已经导致
Either[String, List[Tag]]
但它有签名
Either[io.Serializable, List[Tag]]
所以我需要做第二个 leftMap 来得到
的正确签名Either[String, List[Tag]]
有人可以帮我解决如何避免必须执行第二个 leftMap 的问题吗?好像没必要,但是一直想不出怎么解决。
谢谢!
尝试
request
.send()
.body
.flatMap { _.left.map(_.message) }