instances of DecodeBson allow you to decode entire DBObjects. this example demonstrates several decoding utilities:
def bdecode4f[A, B, C, D, X](
fxn: (A, B, C, D) => X)(ak: String, bk: String, ck: String,
dk: String)(implicit decodea: DecodeBsonField[A],
decodeb: DecodeBsonField[B], decodec: DecodeBsonField[C],
decoded: DecodeBsonField[D]): DecodeBson[X] = DecodeBson { dbo =>
ApV.apply4(
decodea(ak, dbo).validation,
decodeb(bk, dbo).validation,
decodec(ck, dbo).validation,
decoded(dk, dbo).validation
)(fxn).disjunction
}
Applicative
instance, ApV in DecodeBsons, to collect up multiple decoding errors.
It is actually one of the methods available in DecodeBsons for conveniently defining a decoder for e.g. a case class
case class FourThings(str: String, anInt: Int, b: Boolean, d: Double)
implicit def decodeFourThings: DecodeBson[FourThings] =
bdecode4f(FourThings.apply)("str", "anInt", "b", "d")
of course, there are other ways to define new implementations of DecodeBson, which you can find in the Scaladocs.
DBOWrapper defines a method decode
, which looks up a DecodeBson[A]
.
val dbo = DBO("str" :> "some string", "anInt" :> 23, "b" :> false, "d" :> 55.3)
val v = dbo.decode[FourThings]
v === \/-(FourThings("some string", 23, false, 55.3))
any DecodeBson
instance can also be used as a DecodeBsonField
val dbo = DBO("4things" :> DBO("str" :> "some string",
"anInt" :> 23, "b" :> false, "d" :> 55.3))
val v = dbo.field[FourThings]("4things")
v === \/-(FourThings("some string", 23, false, 55.3))