msgpack4z-core icon indicating copy to clipboard operation
msgpack4z-core copied to clipboard

How to create CaseMapCodecs with recursive case classes

Open shogun656 opened this issue 6 years ago • 0 comments

I have two case classes, one being recursive while the other references the recursive case class.

case class InitFunction(
  id: String,
  timestamp: Long,
  tree: Function
)

case class Function(
  name: String,
  path: String,
  callees: Option[List[Function]]
)

This was how I was planning to create codecs for these case classes:

    val factory = new PackerUnpackerFactory {
      def packer: MsgOutBuffer = MsgOutBuffer.create()
      def unpacker(bytes: Array[Byte]) = MsgInBuffer(bytes)
    }

    val mapCodec = CaseMapCodec.string(factory)

    implicit lazy val codec: MsgpackCodec[PythonFunction] = mapCodec.codec(PythonFunction.apply _, PythonFunction.unapply _)("name", "path", "callees")

    implicit val initCodec: MsgpackCodec[PythonInitFunction] = mapCodec.codec(PythonInitFunction.apply _, PythonInitFunction.unapply _)("id", "timestamp", "tree")

but I was getting a java.lang.StackOverflowError from the lazy codec constantly calling itself. Is there a correct way to unpack recursive case classes?

shogun656 avatar Jul 23 '18 12:07 shogun656