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.
I found a solution using flatMap and recursion.