Skip to content

Commit fe46133

Browse files
committed
Added implicit headOption method SCALA-334
1 parent 61485c1 commit fe46133

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

docs/reference/content/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Changes between released versions
1212

1313
### 2.2.0
1414

15+
* Added implicit headOption method [SCALA-334](https://jira.mongodb.org/browse/SCALA-334)
1516
* Added BsonProperty annotation [SCALA-321](https://jira.mongodb.org/browse/SCALA-321)
1617
* Updated Mongodb Driver Async dependency to [3.5.0](https://jira.mongodb.org/browse/SCALA-335)
1718
* CaseClassCodec - Added support for internal vals. [SCALA-314](https://jira.mongodb.org/browse/SCALA-314)

driver/src/main/scala/org/mongodb/scala/ObservableImplicits.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ trait ObservableImplicits {
347347
* @return the head result of the [[Observable]].
348348
*/
349349
def head(): Future[T] = {
350+
import scala.concurrent.ExecutionContext.Implicits.global
350351
headOption().map {
351352
case Some(result) => result
352353
case None => null.asInstanceOf[T] // scalastyle:ignore null

driver/src/test/scala/org/mongodb/scala/internal/ScalaObservableSpec.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ class ScalaObservableSpec extends FlatSpec with Matchers {
384384
errorSeen.getOrElse(None) shouldBe a[Throwable]
385385
}
386386

387+
it should "provide a headOption method" in {
388+
Await.result(observable[Int]().headOption(), Duration(10, TimeUnit.SECONDS)) should equal(Some(1))
389+
Await.result(observable[Int](fail = true).headOption(), Duration(10, TimeUnit.SECONDS)) should equal(Some(1))
390+
391+
intercept[MongoException] {
392+
Await.result(TestObservable[Int](Observable[Int](1 to 10), failOn = 1).headOption(), Duration(10, TimeUnit.SECONDS))
393+
}
394+
395+
Await.result(TestObservable[Int](Observable(List[Int]())).headOption(), Duration(10, TimeUnit.SECONDS)) should equal(None)
396+
}
397+
387398
it should "provide a head method" in {
388399
Await.result(observable[Int]().head(), Duration(10, TimeUnit.SECONDS)) should equal(1)
389400
Await.result(observable[Int](fail = true).head(), Duration(10, TimeUnit.SECONDS)) should equal(1)

0 commit comments

Comments
 (0)