如何为联产品编解码器定义鉴别器的值?

时间:2017-06-11 08:59:31

标签: scala scodec coproduct

请您解释一下副产品是如何运作的?这是我的代码

sealed trait ArdbData

case class ArdbDataString(value: String) extends ArdbData

case class ArdbDataLong(value: Long) extends ArdbData

case class ArdbDataDouble(value: Double) extends ArdbData

object ArdbDataString {
  implicit val codec: Codec[ArdbDataString] = // ... some codec here
  implicit val discriminator: Discriminator[ArdbData, ArdbDataString, Int] = Discriminator(6)
}

object ArdbDataLong {

  implicit val codec: Codec[ArdbDataLong] = // ... some codec here
  implicit val discriminator: Discriminator[ArdbData, ArdbDataLong, Int] = Discriminator(1)
}

object ArdbDataDouble {
  implicit val codec: Codec[ArdbDataDouble] = // ... some codec here
  implicit val discriminator: Discriminator[ArdbData, ArdbDataDouble, Int] = Discriminator(2)
}

object ArdbData {
  implicit val discriminated: Discriminated[ArdbData, Int] = Discriminated(uint8)

  import shapeless.syntax.singleton._

//  implicit val codec: Codec[ArdbData] = Codec.coproduct[ArdbData].discriminatedBy(uint8).using(
//    'ArdbDataString ->> 4 :: 'ArdbDataLong ->> 1 :: 'ArdbDataDouble ->> 2 :: HNil)
}

我认为我必须为ArdbData定义coproduct编解码器,如this test中所示。此外,我需要为discriminatedBy.using()中定义的每个子类型使用特定的ID。当我开始测试时,我突然发现来自.using的密钥未被使用。而是使用来自每个Discriminator实例的ID。然后我评论了Codec.coproduct[ArdbData],它仍然编译!为什么编译?什么是定义鉴别器价值的正确方法?

0 个答案:

没有答案