Skip to content

Commit 5f8020e

Browse files
committed
enable isEmpty and empty methods
1 parent 3675bda commit 5f8020e

File tree

8 files changed

+63
-12
lines changed

8 files changed

+63
-12
lines changed

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ organization := "com.github.celadari"
44

55
homepage := Some(url("https://github.com/celadari/json-logic-scala"))
66

7-
version := "1.0.0"
7+
version := "1.1.0"
88

99
scalaVersion := "2.10.7"
1010

@@ -33,6 +33,8 @@ publishTo := {
3333
else Some("releases" at "https://oss.sonatype.org/service/local/staging/deploy/maven2")
3434
}
3535

36+
publishConfiguration := publishConfiguration.value.withOverwrite(true)
37+
3638
credentials += Credentials(Path.userHome / ".sbt" / ".credentials")
3739

3840
publishArtifact in Test := false

project/plugins.sbt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
resolvers += "jgit-repo" at "https://download.eclipse.org/jgit/maven"
22

3-
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "latest.integration") // https://github.com/sbt/sbt-ghpages
3+
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3") // https://github.com/sbt/sbt-ghpages
44

5-
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "latest.integration")
5+
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0")
6+
7+
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")

src/main/scala/com/github/celadari/jsonlogicscala/core/ComposeLogic.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ object ComposeLogic {
2626
}
2727
val OPERATORS: Array[String] = BINARY_OPERATORS.ALL ++ MULTIPLE_OPERATORS.ALL
2828

29+
/**
30+
* Returns an empty condition.
31+
* @since 1.1.0
32+
* @return [[ComposeLogic]] instance.
33+
*/
34+
def empty: ComposeLogic = new ComposeLogic("", Array())
35+
2936
private[core] def decode(jsonLogic: JsObject, jsonLogicData: JsObject)(implicit decoder: Decoder): ComposeLogic = {
3037
// check for operator field
3138
val fields = jsonLogic.fields
3239

3340
// check for compose logic operator field
3441
if (fields.length > 1) throw new Error("JSON object is supposed to have only one operator field.")
3542
val operator = fields.head._1
36-
if (!OPERATORS.contains(operator)) throw new Error(s"Invalid parsed operator: $operator")
3743

3844
// if operator is compose logic
3945
ComposeLogic(operator, decodeArrayOfConditions((jsonLogic \ operator).get, jsonLogicData)(decoder))
@@ -67,4 +73,12 @@ object ComposeLogic {
6773

6874
}
6975

70-
case class ComposeLogic(override val operator: String, conditions: Array[JsonLogicCore]) extends JsonLogicCore(operator)
76+
case class ComposeLogic(override val operator: String, conditions: Array[JsonLogicCore]) extends JsonLogicCore(operator) {
77+
78+
/**
79+
* Indicates if this represents an empty condition.
80+
* @since 1.1.0
81+
* @return boolean to indicate if all sub-conditions are empty as well or if sub-conditions array is empty.
82+
*/
83+
def isEmpty: Boolean = conditions.forall(_.isEmpty)
84+
}

src/main/scala/com/github/celadari/jsonlogicscala/core/Decoder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ abstract class Decoder {
2828
case "boolean" => jsValue.as[Boolean]
2929
case otherType => customDecode(jsValue, otherType)(reads)
3030
}
31-
ValueLogic("var", value)
31+
ValueLogic("var", Some(value))
3232
}
3333
}

src/main/scala/com/github/celadari/jsonlogicscala/core/Encoder.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ abstract class Encoder {
1313
def customValueAndType(value: Any): (String, JsValue)
1414

1515
def getJsValueAndType(value: Any): (String, JsValue) = {
16+
if (Option(value).isEmpty) return ("null", JsNull)
17+
1618
value match {
1719
case value: String => ("string", JsString(value))
1820
case value: Byte => ("byte", JsNumber(value.toInt))
@@ -36,7 +38,7 @@ abstract class Encoder {
3638
def encode(valueLogic: ValueLogic[_]): (String, String, JsValue) = {
3739
val codenameData = valueLogic.codename
3840

39-
val (typeData, jsValue) = getJsValueAndType(valueLogic.value)
41+
val (typeData, jsValue) = getJsValueAndType(valueLogic.valueOpt.orNull)
4042
(typeData, codenameData, jsValue)
4143
}
4244
}

src/main/scala/com/github/celadari/jsonlogicscala/core/JsonLogicCore.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import play.api.libs.json._
55

66
object JsonLogicCore {
77

8+
/**
9+
* Returns an empty condition.
10+
* @since 1.1.0
11+
* @return [[JsonLogicCore]] instance.
12+
* @note Be careful with this method: returned instance is neither of type [[ComposeLogic]] or type [[ValueLogic]].
13+
*/
14+
def empty: JsonLogicCore = new JsonLogicCore("") {
15+
override def isEmpty: Boolean = true
16+
}
17+
818
private[core] def decode(jsonLogic: JsObject, jsonLogicData: JsObject)(implicit decoder: Decoder): JsonLogicCore = {
919
// check for operator field
1020
val fields = jsonLogic.fields
@@ -14,8 +24,6 @@ object JsonLogicCore {
1424

1525
// check for compose logic operator field
1626
if (fields.length > 1) throw new Error("JSON object is supposed to have only one operator field.")
17-
val operator = fields.head._1
18-
if (!ComposeLogic.OPERATORS.contains(operator)) throw new Error(s"Invalid parsed operator: $operator")
1927

2028
// if operator is compose logic
2129
ComposeLogic.decode(jsonLogic, jsonLogicData)(decoder)
@@ -60,4 +68,12 @@ abstract class JsonLogicCore(val operator: String) {
6068
def reduce(implicit reducer: ReduceLogic): Any = {
6169
reducer.reduce(this)
6270
}
71+
72+
/**
73+
* Indicates if this represents an empty condition.
74+
* @since 1.1.0
75+
* @return boolean to indicate if empty.
76+
* @since abstract method.
77+
*/
78+
def isEmpty: Boolean
6379
}

src/main/scala/com/github/celadari/jsonlogicscala/core/ValueLogic.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import play.api.libs.json._
55

66
object ValueLogic {
77

8+
/**
9+
* Returns an empty condition.
10+
* @since 1.1.0
11+
* @return [[ValueLogic]] instance.
12+
*/
13+
def empty[T]: ValueLogic[T] = new ValueLogic[T]("", None, randomUUID.toString)
14+
815
private[core] def decode(jsonLogic: JsObject, jsonLogicData: JsObject)(implicit decoder: Decoder): ValueLogic[_] = {
916
decoder.decode(jsonLogic, jsonLogicData)
1017
}
@@ -23,6 +30,14 @@ object ValueLogic {
2330

2431
case class ValueLogic[T](
2532
override val operator: String,
26-
value: T,
33+
valueOpt: Option[T],
2734
codename: String = randomUUID.toString
28-
) extends JsonLogicCore(operator)
35+
) extends JsonLogicCore(operator) {
36+
37+
/**
38+
* Indicates if this represents an empty condition.
39+
* @since 1.1.0
40+
* @return boolean to indicate if [[ValueLogic.valueOpt]] is [[None]].
41+
*/
42+
def isEmpty: Boolean = valueOpt.isEmpty
43+
}

src/main/scala/com/github/celadari/jsonlogicscala/operators/ReduceLogic.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ReduceLogic(implicit cmpOp: CompareOperator, ctnOp: ContainsOperator, bool
2525
}
2626
}
2727

28-
def reduceValueLogic(condition: ValueLogic[_]): Any = condition.value
28+
def reduceValueLogic(condition: ValueLogic[_]): Any = condition.valueOpt.get
2929

3030
def reduce(condition: JsonLogicCore): Any = {
3131
condition match {

0 commit comments

Comments
 (0)