Circe:高效解码多级 ADT
Circe: decode multi-level ADT efficiently
我想用 Circe 解码以下 ADT:
sealed trait PaymentType
object PaymentType extends EnumEncoder[PaymentType] {
case object DebitCard extends PaymentType
case object Check extends PaymentType
case object Cash extends PaymentType
case object Mobile extends PaymentType
}
sealed trait CreditCard extends PaymentType
object CreditCard extends EnumEncoder[CreditCard] {
case object UNKNOWN_CREDIT_CARD extends CreditCard
case object NOT_ACCEPTED extends CreditCard
case object VISA extends CreditCard
case object MASTER_CARD extends CreditCard
case object DINERS_CLUB extends CreditCard
case object AMERICAN_EXPRESS extends CreditCard
case object DISCOVER_CARD extends CreditCard
}
如您所见,有一个父类型 PaymentType
,它有一些直接继承者和另一个密封特征家族 CreditCard
。现在解码是这样完成的:
object CreditCard {
implicit val decoder: Decoder[CreditCard] = Decoder.instance[CreditCard] {
_.as[String].map {
case "NOT_ACCEPTED" => NOT_ACCEPTED
case "VISA" => VISA
case "MASTER_CARD" => MASTER_CARD
case "DINERS_CLUB" => DINERS_CLUB
case "AMERICAN_EXPRESS" => AMERICAN_EXPRESS
case "DISCOVER_CARD" => DISCOVER_CARD
case _ => UNKNOWN_CREDIT_CARD
}
}
object PaymentType {
implicit val decoder: Decoder[PaymentType] = Decoder.instance[PaymentType] {
_.as[String].flatMap {
case "DebitCard" => Right(DebitCard)
case "Check" => Right(Check)
case "Cash" => Right(Cash)
case "Mobile" => Right(Mobile)
case _ => Left(DecodingFailure("", List()))
}
}.or(CreditCard.decoder.widen)
}
我不喜欢的是 PaymentType
解码器,尤其是在遇到基于信用卡的完全正常的情况下,我需要创建一个额外且不必要的 DecodingFailure
实例支付方式。我们已经将 CPU 的 99.9% 花在了 JSON 处理上,但它看起来不太对劲。要么是糟糕的 ADT 设计,要么 Circe 中应该有一种方法可以更好地处理这个问题。有什么想法吗?
您可以将 CreditCard
解码器的后备移动到 PaymentType
解码器案例中,这样您就可以避免失败:
implicit val decoder: Decoder[PaymentType] = Decoder.instance[PaymentType] { c =>
c.as[String].flatMap {
case "DebitCard" => Right(DebitCard)
case "Check" => Right(Check)
case "Cash" => Right(Cash)
case "Mobile" => Right(Mobile)
case _ => CreditCard.decoder(c)
}
}
不过,在这种情况下,我可能会将字符串解析分解为单独的方法:
sealed trait PaymentType
object PaymentType extends EnumEncoder[PaymentType] {
case object DebitCard extends PaymentType
case object Check extends PaymentType
case object Cash extends PaymentType
case object Mobile extends PaymentType
private val nameMapping = List(DebitCard, Check, Cash, Mobile).map(pt =>
pt.productPrefix -> pt
).toMap
def fromString(input: String): Option[PaymentType] = nameMapping.get(input)
}
sealed trait CreditCard extends PaymentType
object CreditCard extends EnumEncoder[CreditCard] {
case object UNKNOWN_CREDIT_CARD extends CreditCard
case object NOT_ACCEPTED extends CreditCard
case object VISA extends CreditCard
case object MASTER_CARD extends CreditCard
case object DINERS_CLUB extends CreditCard
case object AMERICAN_EXPRESS extends CreditCard
case object DISCOVER_CARD extends CreditCard
private val nameMapping = List(
NOT_ACCEPTED,
VISA,
MASTER_CARD,
DINERS_CLUB,
AMERICAN_EXPRESS,
DISCOVER_CARD
).map(pt => pt.productPrefix -> pt).toMap
def fromString(input: String): CreditCard =
nameMapping.getOrElse(input, UNKNOWN_CREDIT_CARD)
}
然后你可以根据 fromString
方法编写解码器,这对我来说是一种更好的解决问题的方法(我不确定哪种方法将涉及更少的分配)。不过,这可能主要是个人喜好问题。
我想用 Circe 解码以下 ADT:
sealed trait PaymentType
object PaymentType extends EnumEncoder[PaymentType] {
case object DebitCard extends PaymentType
case object Check extends PaymentType
case object Cash extends PaymentType
case object Mobile extends PaymentType
}
sealed trait CreditCard extends PaymentType
object CreditCard extends EnumEncoder[CreditCard] {
case object UNKNOWN_CREDIT_CARD extends CreditCard
case object NOT_ACCEPTED extends CreditCard
case object VISA extends CreditCard
case object MASTER_CARD extends CreditCard
case object DINERS_CLUB extends CreditCard
case object AMERICAN_EXPRESS extends CreditCard
case object DISCOVER_CARD extends CreditCard
}
如您所见,有一个父类型 PaymentType
,它有一些直接继承者和另一个密封特征家族 CreditCard
。现在解码是这样完成的:
object CreditCard {
implicit val decoder: Decoder[CreditCard] = Decoder.instance[CreditCard] {
_.as[String].map {
case "NOT_ACCEPTED" => NOT_ACCEPTED
case "VISA" => VISA
case "MASTER_CARD" => MASTER_CARD
case "DINERS_CLUB" => DINERS_CLUB
case "AMERICAN_EXPRESS" => AMERICAN_EXPRESS
case "DISCOVER_CARD" => DISCOVER_CARD
case _ => UNKNOWN_CREDIT_CARD
}
}
object PaymentType {
implicit val decoder: Decoder[PaymentType] = Decoder.instance[PaymentType] {
_.as[String].flatMap {
case "DebitCard" => Right(DebitCard)
case "Check" => Right(Check)
case "Cash" => Right(Cash)
case "Mobile" => Right(Mobile)
case _ => Left(DecodingFailure("", List()))
}
}.or(CreditCard.decoder.widen)
}
我不喜欢的是 PaymentType
解码器,尤其是在遇到基于信用卡的完全正常的情况下,我需要创建一个额外且不必要的 DecodingFailure
实例支付方式。我们已经将 CPU 的 99.9% 花在了 JSON 处理上,但它看起来不太对劲。要么是糟糕的 ADT 设计,要么 Circe 中应该有一种方法可以更好地处理这个问题。有什么想法吗?
您可以将 CreditCard
解码器的后备移动到 PaymentType
解码器案例中,这样您就可以避免失败:
implicit val decoder: Decoder[PaymentType] = Decoder.instance[PaymentType] { c =>
c.as[String].flatMap {
case "DebitCard" => Right(DebitCard)
case "Check" => Right(Check)
case "Cash" => Right(Cash)
case "Mobile" => Right(Mobile)
case _ => CreditCard.decoder(c)
}
}
不过,在这种情况下,我可能会将字符串解析分解为单独的方法:
sealed trait PaymentType
object PaymentType extends EnumEncoder[PaymentType] {
case object DebitCard extends PaymentType
case object Check extends PaymentType
case object Cash extends PaymentType
case object Mobile extends PaymentType
private val nameMapping = List(DebitCard, Check, Cash, Mobile).map(pt =>
pt.productPrefix -> pt
).toMap
def fromString(input: String): Option[PaymentType] = nameMapping.get(input)
}
sealed trait CreditCard extends PaymentType
object CreditCard extends EnumEncoder[CreditCard] {
case object UNKNOWN_CREDIT_CARD extends CreditCard
case object NOT_ACCEPTED extends CreditCard
case object VISA extends CreditCard
case object MASTER_CARD extends CreditCard
case object DINERS_CLUB extends CreditCard
case object AMERICAN_EXPRESS extends CreditCard
case object DISCOVER_CARD extends CreditCard
private val nameMapping = List(
NOT_ACCEPTED,
VISA,
MASTER_CARD,
DINERS_CLUB,
AMERICAN_EXPRESS,
DISCOVER_CARD
).map(pt => pt.productPrefix -> pt).toMap
def fromString(input: String): CreditCard =
nameMapping.getOrElse(input, UNKNOWN_CREDIT_CARD)
}
然后你可以根据 fromString
方法编写解码器,这对我来说是一种更好的解决问题的方法(我不确定哪种方法将涉及更少的分配)。不过,这可能主要是个人喜好问题。