Skip to content

Commit

Permalink
Rename partially to matchOrIgnore (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed May 2, 2023
1 parent bc570db commit c81a54e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 47 deletions.
16 changes: 9 additions & 7 deletions core/src/main/scala/erules/Rule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ object Rule extends RuleInstances {

class RuleBuilder[F[_], T] private[erules] (name: String) { $this =>

def apply(f: Function[T, F[RuleVerdict]]): Rule[F, T] =
def apply(f: T => F[RuleVerdict]): Rule[F, T] =
RuleImpl(f, RuleInfo($this.name))

def partially(f: PartialFunction[T, F[RuleVerdict]])(implicit F: Applicative[F]): Rule[F, T] =
def matchOrIgnore(f: PartialFunction[T, F[RuleVerdict]])(implicit
F: Applicative[F]
): Rule[F, T] =
apply(f.lift.andThen(_.getOrElse(F.pure(Ignore.noMatch))))

def failed(ex: Throwable)(implicit F: ApplicativeThrow[F]): Rule[F, T] =
Expand All @@ -189,31 +191,31 @@ object Rule extends RuleInstances {
apply(_ => F.pure(v))

// assertions
def assert(f: Function[T, F[Boolean]])(implicit
def assert(f: T => F[Boolean])(implicit
F: Applicative[F]
): Rule[F, T] =
fromBooleanF(f)(
ifTrue = Allow.withoutReasons,
ifFalse = Deny.withoutReasons
)

def assert(ifFalse: String)(f: Function[T, F[Boolean]])(implicit
def assert(ifFalse: String)(f: T => F[Boolean])(implicit
F: Applicative[F]
): Rule[F, T] =
fromBooleanF(f)(
ifTrue = Allow.withoutReasons,
ifFalse = Deny.because(ifFalse)
)

def assertNot(f: Function[T, F[Boolean]])(implicit F: Applicative[F]): Rule[F, T] =
def assertNot(f: T => F[Boolean])(implicit F: Applicative[F]): Rule[F, T] =
assert(f.andThen(_.map(!_)))

def assertNot(ifTrue: String)(f: Function[T, F[Boolean]])(implicit
def assertNot(ifTrue: String)(f: T => F[Boolean])(implicit
F: Applicative[F]
): Rule[F, T] = assert(ifTrue)(f.andThen(_.map(!_)))

def fromBooleanF(
f: Function[T, F[Boolean]]
f: T => F[Boolean]
)(ifTrue: => RuleVerdict, ifFalse: => RuleVerdict)(implicit
F: Applicative[F]
): Rule[F, T] = apply(f.andThen(_.ifF(ifTrue, ifFalse)))
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala/erules/RuleVerdict.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private[erules] trait RuleVerdictBecauseSupport[+T <: RuleVerdict] {
object RuleVerdict extends RuleVerdictInstances {

// noinspection ScalaWeakerAccess
val noReasons: List[EvalReason] = Nil
lazy val noReasons: List[EvalReason] = Nil

// TODO: add test
def whenNot(b: Boolean)(ifTrue: => RuleVerdict, ifFalse: => RuleVerdict): RuleVerdict =
Expand All @@ -71,7 +71,7 @@ object RuleVerdict extends RuleVerdictInstances {
sealed trait Allow extends RuleVerdict with RuleVerdictBecauseSupport[Allow]
object Allow extends RuleVerdictBecauseSupport[Allow] {

val allNotExplicitlyDenied: Allow = Allow.because("Allow All Not Explicitly Denied")
lazy val allNotExplicitlyDenied: Allow = Allow.because("Allow All Not Explicitly Denied")

// TODO: add test
def when(b: Boolean)(ifFalse: => RuleVerdict): RuleVerdict =
Expand Down Expand Up @@ -107,7 +107,7 @@ object RuleVerdict extends RuleVerdictInstances {
sealed trait Deny extends RuleVerdict with RuleVerdictBecauseSupport[Deny]
object Deny extends RuleVerdictBecauseSupport[Deny] {

val allNotExplicitlyAllowed: Deny = Deny.because("Deny All Not Explicitly Allowed")
lazy val allNotExplicitlyAllowed: Deny = Deny.because("Deny All Not Explicitly Allowed")

// TODO: add test
def when(b: Boolean)(ifFalse: => RuleVerdict): RuleVerdict =
Expand Down Expand Up @@ -143,7 +143,7 @@ object RuleVerdict extends RuleVerdictInstances {
sealed trait Ignore extends RuleVerdict with RuleVerdictBecauseSupport[Ignore]
object Ignore extends RuleVerdictBecauseSupport[Ignore] {

val noMatch: Ignore = Ignore.because("No match")
lazy val noMatch: Ignore = Ignore.because("No match")

// TODO: add test
def when(b: Boolean)(ifFalse: => RuleVerdict): RuleVerdict =
Expand Down
22 changes: 11 additions & 11 deletions core/src/test/scala/erules/EngineResultSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class EngineResultSpec extends AnyWordSpec with Matchers with EitherValues {

case class Foo(value: String)

val rule1: PureRule[Foo] = Rule("Check Foo").partially {
val rule1: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}

val rule2: PureRule[Foo] = Rule("Check Foo").partially {
val rule2: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}
Expand Down Expand Up @@ -57,12 +57,12 @@ class EngineResultSpec extends AnyWordSpec with Matchers with EitherValues {

case class Foo(value: String)

val rule1: PureRule[Foo] = Rule("Check Foo").partially {
val rule1: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}

val rule2: PureRule[Foo] = Rule("Check Foo").partially {
val rule2: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Deny.withoutReasons
}
Expand Down Expand Up @@ -99,12 +99,12 @@ class EngineResultSpec extends AnyWordSpec with Matchers with EitherValues {

case class Foo(value: String)

val rule1: PureRule[Foo] = Rule("Check Foo").partially {
val rule1: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Deny.withoutReasons
}

val rule2: PureRule[Foo] = Rule("Check Foo").partially {
val rule2: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}
Expand Down Expand Up @@ -141,12 +141,12 @@ class EngineResultSpec extends AnyWordSpec with Matchers with EitherValues {

case class Foo(value: String)

val rule1: PureRule[Foo] = Rule("Check Foo").partially {
val rule1: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Deny.withoutReasons
}

val rule2: PureRule[Foo] = Rule("Check Foo").partially {
val rule2: PureRule[Foo] = Rule("Check Foo").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Deny.withoutReasons
}
Expand Down Expand Up @@ -187,17 +187,17 @@ class EngineResultSpec extends AnyWordSpec with Matchers with EitherValues {

case class Foo(value: String)

val rule1: PureRule[Foo] = Rule("Check Foo 1").partially {
val rule1: PureRule[Foo] = Rule("Check Foo 1").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}

val rule2: PureRule[Foo] = Rule("Check Foo 2").partially {
val rule2: PureRule[Foo] = Rule("Check Foo 2").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}

val rule3: PureRule[Foo] = Rule("Check Foo 3").partially {
val rule3: PureRule[Foo] = Rule("Check Foo 3").matchOrIgnore {
case Foo("") => Deny.because("Empty Value")
case Foo("TEST") => Allow.withoutReasons
}
Expand Down
24 changes: 12 additions & 12 deletions core/src/test/scala/erules/RuleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class RuleSpec
"return the right result once evaluated" in {
case class Foo(@unused x: String, @unused y: Int)

val rule: RuleIO[Foo] = Rule("Check Y value").partially {
val rule: RuleIO[Foo] = Rule("Check Y value").matchOrIgnore {
case Foo(_, 0) => IO.pure(Allow.withoutReasons)
case Foo(_, 1) => IO.pure(Deny.withoutReasons)
}
Expand All @@ -153,7 +153,7 @@ class RuleSpec
case class Foo(@unused x: String, @unused y: Int)
val ex = new RuntimeException("BOOM")

val rule: RuleIO[Foo] = Rule("Check Y value").partially {
val rule: RuleIO[Foo] = Rule("Check Y value").matchOrIgnore {
case Foo(_, 0) => IO.raiseError(ex)
case Foo(_, 1) => IO.pure(Deny.withoutReasons)
}
Expand All @@ -173,7 +173,7 @@ class RuleSpec
"return the right result once evaluated in defined domain" in {
case class Foo(@unused x: String, @unused y: Int)

val rule: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val rule: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand All @@ -186,7 +186,7 @@ class RuleSpec
"return the Ignore once evaluated out of the defined domain" in {
case class Foo(@unused x: String, @unused y: Int)

val rule: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val rule: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand Down Expand Up @@ -298,7 +298,7 @@ class RuleSpec
"return the right result once evaluated" in {
case class Foo(@unused x: String, @unused y: Int)

val rule: Rule[IO, Foo] = Rule("Check Y value").partially {
val rule: Rule[IO, Foo] = Rule("Check Y value").matchOrIgnore {
case Foo(_, 0) => IO.pure(Allow.withoutReasons)
case Foo(_, 1) => IO.pure(Deny.withoutReasons)
}
Expand All @@ -312,7 +312,7 @@ class RuleSpec
"return an exception when a case fail" in {
case class Foo(@unused x: String, @unused y: Int)

val rule: RuleIO[Foo] = Rule("Check Y value").partially {
val rule: RuleIO[Foo] = Rule("Check Y value").matchOrIgnore {
case Foo(_, 0) => IO.raiseError(new RuntimeException("BOOM"))
case Foo(_, 1) => IO.pure(Deny.withoutReasons)
}
Expand All @@ -327,7 +327,7 @@ class RuleSpec
case class Foo(@unused x: String, @unused y: Int)

val rule: PureRule[Foo] =
Rule("Check Y value").partially { case Foo(_, 0) =>
Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand All @@ -338,7 +338,7 @@ class RuleSpec
case class Foo(@unused x: String, @unused y: Int)

val rule: PureRule[Foo] =
Rule("Check Y value").partially { case Foo(_, 0) =>
Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand All @@ -353,10 +353,10 @@ class RuleSpec

val duplicated: List[PureRule[Foo]] = Rule.findDuplicated(
NonEmptyList.of(
Rule("Check Y value").partially { case Foo(_, 0) =>
Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
},
Rule("Check Y value").partially { case Foo(_, 1) =>
Rule("Check Y value").matchOrIgnore { case Foo(_, 1) =>
Allow.withoutReasons
}
)
Expand All @@ -370,10 +370,10 @@ class RuleSpec

val duplicated: Seq[PureRule[Foo]] = Rule.findDuplicated(
NonEmptyList.of(
Rule("Check Y value").partially { case Foo(_, 0) =>
Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
},
Rule("Check X value").partially { case Foo("Foo", _) =>
Rule("Check X value").matchOrIgnore { case Foo("Foo", _) =>
Allow.withoutReasons
}
)
Expand Down
20 changes: 10 additions & 10 deletions core/src/test/scala/erules/RulesEngineSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class RulesEngineSpec
"RulesEngine" should {
"Return a DuplicatedRulesException with duplicated rules" in {

val allowYEqZero1: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero1: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

val allowYEqZero2: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero2: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand All @@ -50,7 +50,7 @@ class RulesEngineSpec

"Respond with DENIED when there are no rules for the target" in {

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand All @@ -76,11 +76,11 @@ class RulesEngineSpec

"Respond with DENIED when a rule Deny the target" in {

val denyXEqTest: PureRule[Foo] = Rule("Check X value").partially { case Foo("TEST", _) =>
val denyXEqTest: PureRule[Foo] = Rule("Check X value").matchOrIgnore { case Foo("TEST", _) =>
Deny.withoutReasons
}

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand Down Expand Up @@ -112,7 +112,7 @@ class RulesEngineSpec

"Respond with ALLOWED when a ALL rules allow the target" in {

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand Down Expand Up @@ -174,7 +174,7 @@ class RulesEngineSpec

"Respond with ALLOWED when there are no rules for the target" in {

val denyYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val denyYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Deny.withoutReasons
}

Expand All @@ -200,11 +200,11 @@ class RulesEngineSpec

"Respond with DENIED when a rule Deny the target" in {

val denyXEqTest: PureRule[Foo] = Rule("Check X value").partially { case Foo("TEST", _) =>
val denyXEqTest: PureRule[Foo] = Rule("Check X value").matchOrIgnore { case Foo("TEST", _) =>
Deny.withoutReasons
}

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand Down Expand Up @@ -233,7 +233,7 @@ class RulesEngineSpec

"Respond with ALLOWED when a ALL rules allow the target" in {

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StringReportEncoderSpec extends AsyncWordSpec with AsyncIOSpec with Matche

case class Foo(x: String, y: Int)

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.withoutReasons
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class XmlReportEncoderSpec extends munit.CatsEffectSuite {
)
}

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.because("because yes!")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JsonReportEncoderSpec extends munit.CatsEffectSuite {
test("EngineResult.asJsonReport return a well-formatted JSON report") {
case class Foo(x: String, y: Int)

val allowYEqZero: PureRule[Foo] = Rule("Check Y value").partially { case Foo(_, 0) =>
val allowYEqZero: PureRule[Foo] = Rule("Check Y value").matchOrIgnore { case Foo(_, 0) =>
Allow.because("reason")
}

Expand Down

0 comments on commit c81a54e

Please sign in to comment.