我正在尝试将规则捕获到模型中 这就是我所拥有的
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
格式正常工作,如何使它正常工作?