[英]Scodec: Coproducts could not find implicit value for parameter auto: scodec.codecs.CoproductBuilderAuto
I am trying to define an Scodec coproduct codec for communicating with an EELink GPS.我正在尝试定义一个用于与 EELink GPS 通信的 Scodec 副产品编解码器。
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.我查看了Scodec - Coproducts 找不到参数 auto 的隐式值:scodec.codecs.CoproductBuilderAuto但它没有帮助,从我看到的情况来看,我正在正确定义编解码器和鉴别器。
I am running on Scala 2.11.5 with scodec-core 1.7.0 and scodec-bits 1.0.5我在 Scala 2.11.5 上运行,编码为 1.7.0,编码为 1.0.5
The code that is there now needs two minor changes:现在的代码需要两个小改动:
Message
trait must be sealed
, or otherwise, Shapeless will not provide a Generic.Aux[Message, SomeCoproduct]
instance.Message
特性必须被sealed
,或以其它方式,无形不会提供一个Generic.Aux[Message, SomeCoproduct]
实例。Codec.coproduct[Message]
must be after all the subtypes are defined.Codec.coproduct[Message]
的调用必须在定义了所有子类型之后。 Moving the companion to the end of the file is sufficient. With these two changes, the example compiles successfully.通过这两个更改,示例编译成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.