Skip to content

How to create CaseMapCodecs with recursive case classes #7

@shogun656

Description

@shogun656

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions