Reputation: 2238
I am using Scodec to decode Flac metadata. One of the specifications is that there is a Header and a Block that can be repeated a number of times together. Header has a flag which indicates if the current Header/Block combo is the last.
I have been able to decode Header and Block, but how can we create a Vector base on this specification.
Here is the code broken down
//isLastBlock determines if this is the last Header/Block combo to decode.
case class Header(isLastBlock: Boolean)
//Some example data.
case class Block(someData: Int)
object Codec {
//Codec for Header
val headerCodec : Codec[Header] = bool.as[Header]
//Coded for Block
val blockCodec: Codec[Block] = int32.as[Block]
//We are guaranteed at least one Header/Block Combo, but how can we do this?
val headerBlock: Codec[(Header, Block, Vector[(Header, Block)])] = ???
}
Not sure if scodec provides this functionality. The 2 methods vectorOfN and sizedVector do not work because they require knowing the number of items prior to decoding.
Upvotes: 0
Views: 138
Reputation: 531
I'm currently doing exactly the same exercise - so interested to know how you've progressed.
I've also been experimenting with the VectorTerminatedByIsLastIndicator issue, although the approach taken is different.
This is some test code that I created (for Bar read MetadataBlockHeader). Note that the case class for MetadataBlockHeader does not include the isLast indicator - it is just added during encode, and removed during decode.
case class Bar(value: Int)
case class Foo(list1: List[Bar], list2: List[Bar])
object Foo {
def main(args: Array[String]) : Unit = {
implicit val barCodec : Codec[Bar] = {
("value" | int32).hlist
}.as[Bar]
implicit val barListCodec : Codec[List[Bar]] = new Codec[List[Bar]] {
case class IsLast[A](isLast: Boolean, thing: A)
implicit val lastBarCodec : Codec[IsLast[Bar]] = {
("isLast" | bool) :: ("bar" | Codec[Bar])
}.as[IsLast[Bar]]
override def sizeBound: SizeBound = SizeBound.unknown
override def encode(bars: List[Bar]): Attempt[BitVector] = {
if (bars.size == 0) {
Failure(Err("Cannot encode zero length list"))
} else {
val zippedBars = bars.zipWithIndex
val lastBars = zippedBars.map(bi => IsLast(bi._2 + 1 == bars.size, bi._1))
Codec.encodeSeq(lastBarCodec)(lastBars)
}
}
override def decode(b: BitVector): Attempt[DecodeResult[List[Bar]]] = {
val lastBars = decode(b, List.empty)
val bars = lastBars.map(dr => dr.value.thing)
Successful(DecodeResult(bars, lastBars.last.remainder))
}
@tailrec
private def decode(b: BitVector, lastBars: List[DecodeResult[IsLast[Bar]]]) : List[DecodeResult[IsLast[Bar]]] = {
val lastBar = lastBarCodec.decode(b).require
val lastBarsInterim = lastBars :+ lastBar
if (lastBar.value.isLast) lastBarsInterim
else decode(lastBar.remainder, lastBarsInterim)
}
}
implicit val fooCodec : Codec[Foo] = {
(("list1" | Codec[List[Bar]])
:: ("list2" | Codec[List[Bar]]))
}.as[Foo]
val bar1 = Bar(1)
val bar2 = Bar(2)
val bar3 = Bar(3)
val bar4 = Bar(4)
val aFoo = Foo(Seq(bar1, bar2).toList, Seq(bar3, bar4).toList)
println("aFoo: " + aFoo)
val encodedFoo = fooCodec.encode(aFoo).require
println("encodedFoo: " + encodedFoo)
val decodedFoo = fooCodec.decode(encodedFoo).require.value
println("decodedFoo: " + decodedFoo)
assert(decodedFoo == aFoo)
}
}
A fraction more background to this example can be found here.
Upvotes: 0
Reputation: 2238
I found a solution using flatMap and recursion.
//create a single Codec
val headerBlockCode: Codec[(Header,Block)] = headerCodec ~ blockCodec
//Takes the last decode and continues to decode until Header.isLastBlock
def repeat(priorDecode: DecodeResult[(Header,Block)]) : Attempt[DecodeResult[List[(Header, Block)]]] = {
if (priorDecode.value._1.isLastBlock) Successful(priorDecode.map(List(_)))
else {
headerBlockCode.decode(priorDecode.remainder) match {
case f: Failure => f
case s: Successful[DecodeResult[(Header, Block)]] => {
repeat(s.value) match {
case f: Failure => f
case Successful(list) => {
Successful(list.map(decList => s.value.value :: decList))
}
}
}
}
}
}
//Initial the decode
val bv = BitVector(data)
for {
first <- headerBlockCode.decode(bv)
list <- repeat(first)
} yield {
list.map(l => (list.value, l))
}
Upvotes: 0