Scodec: Coproducts could not find implicit value for parameter auto: scodec.codecs.CoproductBuilderAuto

608 Views Asked by At

I am trying to define an Scodec coproduct codec for communicating with an EELink GPS.

Here is the code:

import scodec.Codec
import scodec.bits.ByteVector
import scodec.codecs._

trait Message
object Message {
  implicit val discriminated: Discriminated[ Message, Int ] = Discriminated(uint8)
  val codec: Codec[ Message ] = Codec.coproduct[ Message ].discriminatedByIndex(uint8)
}

case class GpsId(value: ByteVector)
object GpsId {
  val codec = bytes(8).as[ GpsId ]
}

case class SerialNumber(value: Int)
object SerialNumber {
  val codec = uint16.as[ SerialNumber ]
}

case class Header(protocolNumber: Int, length: Int, serial: SerialNumber)
object Header {
  val codec = (uint8 :: uint16 :: SerialNumber.codec).as[ Header ]
}

case class Login(header: Header, id: GpsId, language: Int) extends Message
object Login {
  val protocolNumber = 0x01
  implicit val discriminator: Discriminator[ Message, Login, Int ] = Discriminator(protocolNumber)
  implicit val codec: Codec[Login] = (Header.codec :: GpsId.codec :: uint8).as[ Login ]
}

I am getting the following:

Error:(14, 48) could not find implicit value for parameter auto: scodec.codecs.CoproductBuilderAuto[com.tecnoguru.ridespark.gps.eelink.messages.Message]
  val codec: Codec[ Message ] = Codec.coproduct[ Message ].discriminatedByIndex(uint8)
                                           ^

I have looked at Scodec - Coproducts could not find implicit value for parameter auto: scodec.codecs.CoproductBuilderAuto but it did not help, from what I see I am defining the codec and the discriminator correctly.

I am running on Scala 2.11.5 with scodec-core 1.7.0 and scodec-bits 1.0.5

1

There are 1 best solutions below

1
On BEST ANSWER

The code that is there now needs two minor changes:

  1. The Message trait must be sealed, or otherwise, Shapeless will not provide a Generic.Aux[Message, SomeCoproduct] instance.
  2. The call to Codec.coproduct[Message] must be after all the subtypes are defined. Moving the companion to the end of the file is sufficient.

With these two changes, the example compiles successfully.