【发布时间】:2020-03-30 16:16:14
【问题描述】:
我想做这样的事情:
def loadYaml[T <: Component](ref: LocalRef): Task[T] = {
val yamlString = Source.fromResource(ref.url).mkString
val json: Either[ParsingFailure, Json] = parser.parse(yamlString)
val component = json.flatMap(_.as[T]) // this line creates the error
Task.fromEither(component)
}
解码抛出:
Error:(54, 22) could not find implicit value for parameter d: io.circe.Decoder[T]
.flatMap(_.as[T])
Component 是密封特征。
circe不可能做到这一点吗?
这有效(Component 而不是T):
def loadYaml(ref: LocalRef): Task[Component] = {
val yamlString = Source.fromResource(ref.url).mkString
val json: Either[ParsingFailure, Json] = parser.parse(yamlString)
val component = json.flatMap(_.as[Component]) // this line creates the error
Task.fromEither(component)
}
我认为问题不在于Decoder,因为它适用于Components,无论如何:
implicit val decodeEvent: Decoder[Component] =
List[Decoder[Component]](
Decoder[DbConnection].widen,
Decoder[DbLookup].widen
).reduceLeft(_ or _)
【问题讨论】:
-
@LuisMiguelMejíaSuárez 我认为问题不在于解码器,因为它与组件一起使用。确保我将其添加到我的问题中。
-
@LuisMiguelMejíaSuárez 谢谢,这行得通!如果您有解释,请将其添加为答案。