不幸的是,当DecodeResult[T]
为成功案例构建对象时,游标历史记录被丢弃。
我能想到的唯一解决方案(它只是一个向您解释这个概念的存根)是:
- 实现自定义
HistoryDecodeResult
:
class HistoryDecodeResult[A](
val h: Option[CursorHistory],
result: \/[(String, CursorHistory),A]
) extends DecodeResult[A](result)
object HistoryDecodeResult{
def apply[A](h: Option[CursorHistory], d: DecodeResult[A]) = new HistoryDecodeResult(h, d.result)
}
implicit class AHCursor(a: ACursor){
def asH[A](implicit d: DecodeJson[A]): HistoryDecodeResult[A] = {
HistoryDecodeResult(a.hcursor.map(_.history), a.as[A](d))
}
}
- 覆盖其中的
DecodeResult[T]
组合方法HistoryDecodeResult
以累积历史记录:
override def map[B](f: A => B): HistoryDecodeResult[B] =
HistoryDecodeResult(h, super.map(f))
//Accumulate history `h` using `CursorHistory.++` if flat-mapping with another HistoryDecodeResult
override def flatMap[B](f: A => DecodeResult[B]): HistoryDecodeResult[B] = ???
... //Go on with other methods
- 从你的解码例程中获取
HistoryDecodeResult
(你必须避免Parse.decode
)并询问历史。