Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4326

codecs for traits (not sealed traits)

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Scala

      Is it possible to use Macro to derive codecs for traits (not sealed traits)? If not, how do I make one myself?

      Suppose the following:

      trait Foo
      
      case class Bar(foos: List[Foo])
      
      case class Foo1(int: Int) extends Foo
      
      case class Foo2(string: String) extends Foo
      
      val bar = Bar(List(Foo1(42), Foo2("foo2")))
      

      If I register the registry and codec like this:

      val registry = fromRegistries(fromProviders(classOf[Bar], classOf[Foo], classOf[Foo1], classOf[Foo2]), DEFAULT_MONGO_REGISTRY)
      val codec = registry.get(classOf[Bar])
      

      Then I am able to encode (i.e. turn bar into a BsonDocument, but not decode - it will fail with next on empty iterator on FooMacroCodec$1.getClassName.

      Is there a way to do this? I've simplified my code a lot, in reality I simply cannot turn Foo into a sealed trait.

      Thanks in advance.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            andylii@mobagel.com Andy Lii
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: