Skip to content

Commit

Permalink
Add BSONReader.collect combinator (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
cchantep authored Oct 31, 2024
1 parent d37704e commit 4b0ba58
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
24 changes: 20 additions & 4 deletions api/src/main/scala/BSONReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ trait BSONReader[T] { self =>
def afterRead[U](f: T => U): BSONReader[U] =
new BSONReader.MappedReader[T, U](self, f)

/**
* '''EXPERIMENTAL:''' (API may change without notice)
*/
def collect[U](read: PartialFunction[T, U]): BSONReader[U] =
BSONReader.from[U] { bson =>
self.readTry(bson).flatMap { v =>
read.lift(v) match {
case Some(result) =>
Success(result)

case None =>
Failure(exceptions.ValueDoesNotMatchException(s"${v}"))
}
}
}

/**
* $beforeReadDescription
*
Expand Down Expand Up @@ -250,7 +266,7 @@ object BSONReader extends BSONReaderCompat with BSONReaderInstances {
iterable[T, Seq](read)

/**
* '''EXPERIMENTAL:''' Creates a [[BSONDocumentReader]] that reads
* '''EXPERIMENTAL:''' Creates a [[BSONReader]] that reads
* the [[BSONArray]] elements.
*
* {{{
Expand All @@ -276,7 +292,7 @@ object BSONReader extends BSONReaderCompat with BSONReaderInstances {
}

/**
* '''EXPERIMENTAL:''' Creates a [[BSONDocumentReader]] that reads
* '''EXPERIMENTAL:''' Creates a [[BSONReader]] that reads
* the [[BSONArray]] elements.
*
* @see [[tuple2]]
Expand All @@ -295,7 +311,7 @@ object BSONReader extends BSONReaderCompat with BSONReaderInstances {
}

/**
* '''EXPERIMENTAL:''' Creates a [[BSONDocumentReader]] that reads
* '''EXPERIMENTAL:''' Creates a [[BSONReader]] that reads
* the [[BSONArray]] elements.
*
* @see [[tuple2]]
Expand All @@ -319,7 +335,7 @@ object BSONReader extends BSONReaderCompat with BSONReaderInstances {
}

/**
* '''EXPERIMENTAL:''' Creates a [[BSONDocumentReader]] that reads
* '''EXPERIMENTAL:''' Creates a [[BSONReader]] that reads
* the [[BSONArray]] elements.
*
* @see [[tuple2]]
Expand Down
30 changes: 27 additions & 3 deletions api/src/test/scala/HandlerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,33 @@ final class HandlerSpec
}
}

"Complex Array" should {
"Complex array" should {
"be of size = 6" in {
array.size must_=== 6
}

"be collect'ed" in {
val reader =
implicitly[BSONReader[Seq[BSONValue]]].collect[::[BSONValue]] {
case head :: tail => ::(head, tail)
}

reader.readTry(array) must beSuccessfulTry(
::(
BSONString("elem0"),
List(
BSONInteger(1),
BSONDouble(2.222),
BSONDocument("name" -> "Joe"),
BSONArray(0L),
BSONString("pp[4]")
)
)
) and {
reader.readTry(BSONArray.empty) must beFailedTry[Seq[BSONValue]]
}
}

"have a an int = 2 at index 2" in {
array.get(1) must beSome(BSONInteger(1)) and (array.getAsOpt[Int](
1
Expand Down Expand Up @@ -399,8 +421,10 @@ final class HandlerSpec
"fails from array" in {
BSONDocument("foo" -> BSONArray.empty).getAsTry[BSONDocument](
"foo"
) must_=== Failure(TypeDoesNotMatchException("BSONDocument", "[]"))
}
) must_=== Failure(
TypeDoesNotMatchException("BSONDocument", "Array ([])")
)
} tag "wip"
}

"BSONDateTime" should {
Expand Down

0 comments on commit 4b0ba58

Please sign in to comment.