pbdirect icon indicating copy to clipboard operation
pbdirect copied to clipboard

Case class with nested inner class not resolving properly.

Open akara opened this issue 6 years ago • 2 comments

Simple example:

object TestMessages {
  case class InnerMessage(id: Int, name: String)
}

import TestMessages._

case class NestedInnerMessage(id: Int, inner: InnerMessage)

Writing this message works correctly:

    "write a message with inner nested type to Protobuf" in {
      import TestMessages._
      val nested = NestedInnerMessage(0, InnerMessage(1, "Hi"))
      nested.toPB shouldBe Array[Byte](8, 0, 18, 6, 8, 1, 18, 2, 72, 105)
    }

However, reading gives a compiler error:

    "read a message with inner nested type to Protobuf" in {
      import TestMessages._
      val bytes = Array[Byte](8, 0, 18, 6, 8, 1, 18, 2, 72, 105)
      bytes.pbTo[NestedInnerMessage] shouldBe NestedInnerMessage(0, InnerMessage(1, "Hi"))
    }
error: could not find implicit value for parameter reader: pbdirect.PBParser[pbdirect.NestedInnerMessage]

akara avatar May 19 '19 18:05 akara

The workaround is helping the compiler picking the right Parser and Reader rather than having it implicitly resolve those first level ones does make this code compile and test properly:

    "read a message with inner nested type to Protobuf" in {
      import TestMessages._
      val bytes = Array[Byte](8, 0, 18, 6, 8, 1, 18, 2, 72, 105)
      bytes.pbTo[NestedInnerMessage](PBParser.requiredParser(PBReader.prodReader)) shouldBe
        NestedInnerMessage(0, InnerMessage(1, "Hi"))
    }

akara avatar May 19 '19 18:05 akara

I can't tell for sure whether this is a scalac issue or something else. The additional PBParser resolution on top of PBReader resolution as opposed to the writer side which only has single layer PBWriter resolution seems to have made the difference.

akara avatar May 19 '19 18:05 akara