Skip to content

Commit

Permalink
Circe - Use unwrapper encoder for EvalReason
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed Aug 10, 2022
1 parent 04c82e5 commit 54dc1b8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
.targetInfo("age")
.contramap(_.age)
)
// allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$8362/0x0000000802349290@6b3982a9,Check UK citizenship,None,Some(citizenship)), RuleImpl(scala.Function1$$Lambda$8362/0x0000000802349290@1f4bc8ea,Check Age >= 18,None,Some(age)))
// allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$10486/0x00000008029eb390@3b64b963,Check UK citizenship,None,Some(citizenship)), RuleImpl(scala.Function1$$Lambda$10486/0x00000008029eb390@65d4d410,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 Down Expand Up @@ -119,7 +119,7 @@ result.unsafeRunSync().asReport[String]
// - Rule: Check UK citizenship
// - Description:
// - Target: citizenship
// - Execution time: 220042 nanoseconds
// - Execution time: 123167 nanoseconds
//
// - Verdict: Right(Deny)
// - Because: Only UK citizenship is allowed!
Expand All @@ -128,7 +128,7 @@ result.unsafeRunSync().asReport[String]
// - Rule: Check Age >= 18
// - Description:
// - Target: age
// - Execution time: 19292 nanoseconds
// - Execution time: 16709 nanoseconds
//
// - Verdict: Right(Deny)
// - Because: Only >= 18 age are allowed!
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/erules/core/EvalReason.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cats.Show
* @param message
* reason message
*/
case class EvalReason(message: String)
case class EvalReason(message: String) extends AnyVal
object EvalReason extends EvalReasonInstances with EvalReasonSyntax {

def stringifyList(reasons: List[EvalReason]): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import erules.core.RuleResultsInterpreterVerdict.{Allowed, Denied}

/** ADT to define the possible responses of the engine evaluation.
*/
sealed trait RuleResultsInterpreterVerdict[T] extends Serializable {
sealed trait RuleResultsInterpreterVerdict[-T] extends Serializable {

/** Result reasons
*/
Expand Down
14 changes: 5 additions & 9 deletions modules/circe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
.targetInfo("age")
.contramap(_.age)
)
// allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$8362/0x0000000802349290@705bbf9a,Check UK citizenship,None,Some(citizenship)), RuleImpl(scala.Function1$$Lambda$8362/0x0000000802349290@1d14745f,Check Age >= 18,None,Some(age)))
// allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$10486/0x00000008029eb390@1ea18d05,Check UK citizenship,None,Some(citizenship)), RuleImpl(scala.Function1$$Lambda$10486/0x00000008029eb390@531ca4f4,Check Age >= 18,None,Some(age)))
```

Import
Expand Down Expand Up @@ -111,13 +111,11 @@ result.unsafeRunSync().asJsonReport
// "verdict" : {
// "type" : "Deny",
// "reasons" : [
// {
// "message" : "Only UK citizenship is allowed!"
// }
// "Only UK citizenship is allowed!"
// ]
// },
// "executionTime" : {
// "length" : 91167,
// "length" : 125334,
// "unit" : "NANOSECONDS"
// }
// },
Expand All @@ -130,13 +128,11 @@ result.unsafeRunSync().asJsonReport
// "verdict" : {
// "type" : "Deny",
// "reasons" : [
// {
// "message" : "Only >= 18 age are allowed!"
// }
// "Only >= 18 age are allowed!"
// ]
// },
// "executionTime" : {
// "length" : 10750,
// "length" : 56500,
// "unit" : "NANOSECONDS"
// }
// }
Expand Down
9 changes: 5 additions & 4 deletions modules/circe/src/main/scala/erules/circe/instances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package erules.circe

import erules.circe.report.{JsonReportInstances, JsonReportSyntax}
import erules.core.*
import io.circe.generic.semiauto.deriveEncoder

object implicits extends CirceAllInstances with CirceAllSyntax

Expand All @@ -15,6 +16,9 @@ private[circe] trait BasicTypesCirceInstances {
import io.circe.*
import io.circe.syntax.*

implicit final val evalReasonCirceEncoder: Encoder[EvalReason] =
Encoder.encodeString.contramap(_.message)

implicit def engineResultCirceEncoder[T: Encoder]: Encoder[EngineResult[T]] =
io.circe.generic.semiauto.deriveEncoder[EngineResult[T]]

Expand All @@ -27,7 +31,7 @@ private[circe] trait BasicTypesCirceInstances {
}

implicit def ruleResultCirceEncoder[T]: Encoder[RuleResult[T, RuleVerdict]] =
io.circe.generic.semiauto.deriveEncoder[RuleResult[T, RuleVerdict]]
deriveEncoder[RuleResult[T, RuleVerdict]]

implicit def ruleCirceEncoder[T]: Encoder[AnyTypedRule[T]] =
Encoder.instance { v =>
Expand All @@ -46,9 +50,6 @@ private[circe] trait BasicTypesCirceInstances {
"reasons" -> Json.fromValues(v.reasons.map(_.asJson))
)
}

implicit final val evalReasonCirceEncoder: Encoder[EvalReason] =
io.circe.generic.semiauto.deriveEncoder[EvalReason]
}

//---------- SYNTAX ----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JsonReportEncoderSpec extends munit.CatsEffectSuite {
case class Foo(x: String, y: Int)

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

val engine: IO[RulesEngineIO[Foo]] =
Expand All @@ -33,27 +33,28 @@ class JsonReportEncoderSpec extends munit.CatsEffectSuite {
obtained = result,
returns = json"""
{
"data" : {
"x" : "TEST",
"y" : 0
},
"verdict" : {
"type" : "Allowed",
"evaluatedRules" : [
{
"rule" : {
"name" : "Check Y value",
"fullDescription" : "Check Y value"
},
"verdict" : {
"type" : "Allow",
"reasons" : [
]
}
}
]
"data" : {
"x" : "TEST",
"y" : 0
},
"verdict" : {
"type" : "Allowed",
"evaluatedRules" : [
{
"rule" : {
"name" : "Check Y value",
"fullDescription" : "Check Y value"
},
"verdict" : {
"type" : "Allow",
"reasons" : [
"reason"
]
}
}
}"""
]
}
}"""
)
}

Expand Down

0 comments on commit 54dc1b8

Please sign in to comment.