具有泛型的递归类的Scala json格式

时间:2018-11-14 19:15:43

标签: json scala

我正在尝试将规则捕获到模型中 这就是我所拥有的

trait Criterion[T] {
  val name: String
  def matches(t: T): Future[Boolean]
}
trait Predicate[T] extends Criterion[T] {
  val criteria: Seq[Criterion[T]]
  val operand: RuleOperand.Name

  override def matches(t: T): Future[Boolean] = {
    val predicate: Boolean => Boolean = if (operand == RuleOperand.ANY) _ == true else _ == false
    Future.find(criteria.map(_.matches(t)))(predicate) map {
      case Some(true) if operand == RuleOperand.ANY => true
      case Some(false) if operand == RuleOperand.ALL => true
      case _ => false
    }
  }
}
case class WeightCriterion(limit: Int) extends Criterion[Person] {
  override val name: String = "weight"

  override def matches(person: Person): Future[Boolean] =
     Future.successful(person.weight > limit)
}

case class HeightCriterion(limit: Int) extends Criterion[Person] {
  override val name: String = "height"

  override def matches(person: Person): Future[Boolean] = 
    Future.successful(person.height > limit)
}

Criterion从Object到Json以及从Json到Object都可以很好地工作,但是我无法使Predicate格式正常工作,如何使它正常工作?

0 个答案:

没有答案