Skip to content

Commit

Permalink
Refactoring types (#165)
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
geirolz committed May 2, 2023
1 parent ae32cc8 commit bc570db
Show file tree
Hide file tree
Showing 59 changed files with 1,206 additions and 786 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,37 @@ Assuming we want to check:
- The person has a UK citizenship

Let's write the rules!

```scala
import erules.core.Rule
import erules.core.RuleVerdict.*
import erules.Rule
import erules.PureRule
import erules.RuleVerdict.*
import cats.data.NonEmptyList
import cats.Id

val checkCitizenship: Rule[Id, Citizenship] =
Rule("Check UK citizenship").apply[Id, Citizenship]{
val checkCitizenship: PureRule[Citizenship] =
Rule("Check UK citizenship") {
case Citizenship(Country("UK")) => Allow.withoutReasons
case _ => Deny.because("Only UK citizenship is allowed!")
case _ => Deny.because("Only UK citizenship is allowed!")
}
// checkCitizenship: Rule[Id, Citizenship] = RuleImpl(<function1>,Check UK citizenship,None,None)
// checkCitizenship: PureRule[Citizenship] = RuleImpl(<function1>,RuleInfo(Check UK citizenship,None,None))

val checkAdultAge: Rule[Id, Age] =
Rule("Check Age >= 18").apply[Id, Age] {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
val checkAdultAge: PureRule[Age] =
Rule("Check Age >= 18") {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
}
// checkAdultAge: Rule[Id, Age] = RuleImpl(<function1>,Check Age >= 18,None,None)
// checkAdultAge: PureRule[Age] = RuleImpl(<function1>,RuleInfo(Check Age >= 18,None,None))

val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
val allPersonRules: NonEmptyList[PureRule[Person]] = NonEmptyList.of(
checkCitizenship
.targetInfo("citizenship")
.contramap(_.citizenship),
checkAdultAge
.targetInfo("age")
.contramap(_.age)
)
// allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$12152/0x0000000802cb7950@3df931c5,Check UK citizenship,None,Some(citizenship)), RuleImpl(scala.Function1$$Lambda$12152/0x0000000802cb7950@6b57b3a7,Check Age >= 18,None,Some(age)))
// allPersonRules: NonEmptyList[PureRule[Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$11131/0x000000080283b368@4e8bcb62,RuleInfo(Check UK citizenship,None,Some(citizenship))), RuleImpl(scala.Function1$$Lambda$11131/0x000000080283b368@4a39095c,RuleInfo(Check Age >= 18,None,Some(age))))
```

N.B. Importing even the `erules-generic` you can use macro to auto-generate the target info using `contramapTarget` method.
Expand All @@ -93,18 +95,19 @@ We can evaluate rules in two different ways:
- allowAllNotDenied

```scala
import erules.core.*
import erules.*
import erules.implicits.*
import cats.effect.IO
import cats.effect.unsafe.implicits.*

val person: Person = Person("Mimmo", "Rossi", Age(16), Citizenship(Country("IT")))
// person: Person = Person(Mimmo,Rossi,Age(16),Citizenship(Country(IT)))

val result: IO[EngineResult[Person]] = for {
engine <- RulesEngine[IO].withRules[Id, Person](allPersonRules).denyAllNotAllowed
result <- engine.parEval(person)
} yield result
val result: IO[EngineResult[Person]] =
RulesEngine
.withRules[Id, Person](allPersonRules)
.denyAllNotAllowed[IO]
.map(_.seqEvalPure(person))
// result: IO[EngineResult[Person]] = IO(...)

//yolo
Expand All @@ -119,7 +122,7 @@ result.unsafeRunSync().asReport[String]
// - Rule: Check UK citizenship
// - Description:
// - Target: citizenship
// - Execution time: 115458 nanoseconds
// - Execution time: *not measured*
//
// - Verdict: Right(Deny)
// - Because: Only UK citizenship is allowed!
Expand All @@ -128,7 +131,7 @@ result.unsafeRunSync().asReport[String]
// - Rule: Check Age >= 18
// - Description:
// - Target: age
// - Execution time: 9125 nanoseconds
// - Execution time: *not measured*
//
// - Verdict: Right(Deny)
// - Because: Only >= 18 age are allowed!
Expand Down
33 changes: 18 additions & 15 deletions core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,27 @@ Assuming we want to check:
- The person has a UK citizenship

Let's write the rules!

```scala mdoc:to-string
import erules.core.Rule
import erules.core.RuleVerdict.*
import erules.Rule
import erules.PureRule
import erules.RuleVerdict.*
import cats.data.NonEmptyList
import cats.Id

val checkCitizenship: Rule[Id, Citizenship] =
Rule("Check UK citizenship").apply[Id, Citizenship]{
val checkCitizenship: PureRule[Citizenship] =
Rule("Check UK citizenship") {
case Citizenship(Country("UK")) => Allow.withoutReasons
case _ => Deny.because("Only UK citizenship is allowed!")
case _ => Deny.because("Only UK citizenship is allowed!")
}

val checkAdultAge: Rule[Id, Age] =
Rule("Check Age >= 18").apply[Id, Age] {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
val checkAdultAge: PureRule[Age] =
Rule("Check Age >= 18") {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
}

val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
val allPersonRules: NonEmptyList[PureRule[Person]] = NonEmptyList.of(
checkCitizenship
.targetInfo("citizenship")
.contramap(_.citizenship),
Expand All @@ -90,17 +92,18 @@ We can evaluate rules in two different ways:
- allowAllNotDenied

```scala mdoc:to-string
import erules.core.*
import erules.*
import erules.implicits.*
import cats.effect.IO
import cats.effect.unsafe.implicits.*

val person: Person = Person("Mimmo", "Rossi", Age(16), Citizenship(Country("IT")))

val result: IO[EngineResult[Person]] = for {
engine <- RulesEngine[IO].withRules[Id, Person](allPersonRules).denyAllNotAllowed
result <- engine.parEval(person)
} yield result
val result: IO[EngineResult[Person]] =
RulesEngine
.withRules[Id, Person](allPersonRules)
.denyAllNotAllowed[IO]
.map(_.seqEvalPure(person))

//yolo
result.unsafeRunSync().asReport[String]
Expand Down
44 changes: 22 additions & 22 deletions core/examples/simple.sc
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ case class Person(
)

//------------- CREATE RULES -------------//
import erules.core.Rule
import erules.core.RuleVerdict._
import erules.*
import erules.RuleVerdict.*
import cats.data.NonEmptyList
import cats.Id

val checkCitizenship: Rule[Id, Citizenship] = Rule("Check UK citizenship").apply[Id, Citizenship] {
val checkCitizenship: PureRule[Citizenship] = Rule("Check UK citizenship") {
case Citizenship(Country("UK")) => Allow.withoutReasons
case _ => Deny.because("Only UK citizenship is allowed!")
}

val checkAdultAge: Rule[Id, Age] =
Rule("Check Age >= 18").apply[Id, Age] {
val checkAdultAge: PureRule[Age] = Rule("Check Age >= 18") {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
}

val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
val allPersonRules: NonEmptyList[PureRule[Person]] = NonEmptyList.of(
checkCitizenship
.targetInfo("citizenship")
.contramap(_.citizenship),
Expand All @@ -37,17 +36,17 @@ val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
)

//-------------- RULES ENGINE --------------//
import erules.core.RulesEngine
import erules.RulesEngine
import cats.effect.IO
import cats.effect.unsafe.implicits._

val person: Person = Person("Mimmo", "Rossi", Age(16), Citizenship(Country("IT")))

val result = for {
engine <- RulesEngine[IO]
engine <- RulesEngine
.withRules[Id, Person](allPersonRules)
.denyAllNotAllowed
result <- engine.parEval(person)
.denyAllNotAllowed[IO]
result = engine.seqEvalPure(person)
} yield result

result.unsafeRunSync()
Expand All @@ -65,23 +64,23 @@ case class Person(
)

//------------- CREATE RULES -------------//
import erules.core.Rule
import erules.core.RuleVerdict._
import erules.*
import erules.RuleVerdict.*
import cats.data.NonEmptyList

val checkCitizenship: Rule[Id, Citizenship] =
Rule("Check UK citizenship")[Id, Citizenship] {
val checkCitizenship: PureRule[Citizenship] =
Rule("Check UK citizenship") {
case Citizenship(Country("UK")) => Allow.withoutReasons
case _ => Deny.because("Only UK citizenship is allowed!")
}

val checkAdultAge: Rule[Id, Age] =
Rule("Check Age >= 18")[Id, Age] {
val checkAdultAge: PureRule[Age] =
Rule("Check Age >= 18") {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
}

val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
val allPersonRules: NonEmptyList[PureRule[Person]] = NonEmptyList.of(
checkCitizenship
.targetInfo("person.citizenship")
.contramap(_.citizenship),
Expand All @@ -91,16 +90,17 @@ val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
)

//-------------- RULES ENGINE --------------//
import erules.core.RulesEngine
import erules.*
import cats.effect.IO
import cats.effect.unsafe.implicits._
import erules.implicits._

val person: Person = Person("Mimmo", "Rossi", Age(16), Citizenship(Country("IT")))

val result = for {
engine <- RulesEngine[IO].withRules[Id, Person](allPersonRules).denyAllNotAllowed
result <- engine.parEval(person)
} yield result
val result: IO[EngineResult[Person]] =
RulesEngine
.withRules(allPersonRules)
.denyAllNotAllowed[IO]
.map(_.seqEvalPure(person))

Console.println(result.unsafeRunSync().asReport)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package erules.core
package erules

import erules.core.RuleResultsInterpreterVerdict.{Allowed, Denied}
import erules.RuleResultsInterpreterVerdict.{Allowed, Denied}

/** Describes the engine output.
*
Expand All @@ -13,13 +13,13 @@ import erules.core.RuleResultsInterpreterVerdict.{Allowed, Denied}
*/
case class EngineResult[T](
data: T,
verdict: RuleResultsInterpreterVerdict[T]
verdict: RuleResultsInterpreterVerdict
) extends Serializable {

def drainExecutionsTime: EngineResult[T] =
copy(verdict = this.verdict match {
case a @ Allowed(erules) => a.copy(evaluatedRules = erules.map(_.drainExecutionTime))
case a @ Denied(erules) => a.copy(evaluatedRules = erules.map(_.drainExecutionTime))
case a @ Allowed(erules) => a.copy(evaluatedResults = erules.map(_.drainExecutionTime))
case a @ Denied(erules) => a.copy(evaluatedResults = erules.map(_.drainExecutionTime))
})
}
object EngineResult extends EngineResultInstances {
Expand All @@ -35,7 +35,11 @@ object EngineResult extends EngineResultInstances {
}
)

def combineAll[T](data: T, er1: EngineResult[T], erN: EngineResult[T]*): EngineResult[T] =
def combineAll[T](
data: T,
er1: EngineResult[T],
erN: EngineResult[T]*
): EngineResult[T] =
(er1 +: erN).toList.reduce((a, b) => combine(data, a, b))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package erules.core
package erules

import cats.Show

Expand Down
Loading

0 comments on commit bc570db

Please sign in to comment.