Skip to content

Commit

Permalink
Add exists & contains methods for JsResult (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaabramov authored and cchantep committed Sep 24, 2018
1 parent 8a143c7 commit c1a80a5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ lazy val `play-json` = crossProject(JVMPlatform, JSPlatform).crossType(CrossType
ProblemFilters.exclude[ReversedMissingMethodProblem]("play.api.libs.json.JsResult.recoverWith"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("play.api.libs.json.Writes.contramap"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("play.api.libs.json.OWrites.contramap"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("play.api.libs.json.JsResult.contains"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("play.api.libs.json.JsResult.exists"),

// Scala 2.13.0-M4
ProblemFilters.exclude[IncompatibleMethTypeProblem]("play.api.libs.json.LowPriorityDefaultReads.traversableReads"),
Expand Down
14 changes: 14 additions & 0 deletions play-json/shared/src/main/scala/play/api/libs/json/JsResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ case class JsSuccess[T](value: T, path: JsPath = JsPath()) extends JsResult[T] {

def foreach(f: T => Unit): Unit = f(value)

def contains[AA >: T](elem: AA): Boolean = elem == value

def exists(p: T => Boolean): Boolean = p(value)

def repath(path: JsPath): JsResult[T] = JsSuccess(value, path ++ this.path)

def getOrElse[U >: T](t: => U): U = value
Expand Down Expand Up @@ -63,6 +67,10 @@ case class JsError(errors: collection.Seq[(JsPath, collection.Seq[JsonValidation

def foreach(f: Nothing => Unit): Unit = ()

def contains[AA >: Nothing](elem: AA): Boolean = false

def exists(p: Nothing => Boolean): Boolean = false

def repath(path: JsPath): JsResult[Nothing] =
JsError(errors.map { case (p, s) => path ++ p -> s })

Expand Down Expand Up @@ -207,6 +215,12 @@ sealed trait JsResult[+A] { self =>
def withFilter(q: A => Boolean) = new WithFilter(a => p(a) && q(a))
}

/** If this result is successful than checks for presence for '''elem''', otherwise return '''false''' */
def contains[AA >: A](elem: AA): Boolean

/** If this result is successful than check value with predicate '''p''', otherwise return '''false''' */
def exists(p: A => Boolean): Boolean

/** Updates the JSON path */
def repath(path: JsPath): JsResult[A]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,33 @@ class JsResultSpec extends WordSpec with MustMatchers {
JsError("err").recoverWith { _ => succ } mustEqual succ
}
}

"JsResult" should {

"return true for JsSuccess(x)#contains(x)" in {
assert(JsSuccess(1).contains(1))
}

"return false for JsSuccess(x)#contains(y)" in {
assert(!JsSuccess(1).contains(2))
}

"return false for JsError(_)#contains(_)" in {
assert(!JsError().contains(1))
}

"return true for JsSuccess(x)#exists(p) if p(x) == true" in {
assert(JsSuccess(1).exists(_ == 1))
}

"return false for JsSuccess(x)#exists(p) if p(x) == false" in {
assert(!JsSuccess(1).exists(_ == 2))
}

"return false for JsError(_).exists(_)" in {
assert(!JsError().exists(_ == 1))
}

}

}

0 comments on commit c1a80a5

Please sign in to comment.