Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 1077fb7

Browse files
committed
for consistency: change behaviour regarding present-but-null
values in MongoDBObject#get. If the value is present and null, the method will now return Some(None), if it is not present, it will return None.
1 parent 93234fd commit 1077fb7

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

casbah-commons/src/main/scala/MongoDBObject.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ class MongoDBObject(val underlying: DBObject = new BasicDBObject) extends mutabl
9191
}
9292
}
9393

94-
// scalastyle:off null
95-
override def get(key: String): Option[AnyRef] = underlying.get(key) match {
96-
case null => None
97-
case value => Some(value)
98-
}
99-
// scalastyle:on null
94+
override def get(key: String): Option[AnyRef] =
95+
/* underlying.get returns null both when the value is missing and when
96+
* it is null. Unlike nulls, Options can be nested, so we can allow clients
97+
* to see the difference. */
98+
Option(underlying.get(key)).orElse(if (containsField(key)) Some(None) else None)
10099

101100
// scalastyle:off method.name
102101
def ++(pairs: (String, Any)*): DBObject = {
@@ -170,6 +169,7 @@ class MongoDBObject(val underlying: DBObject = new BasicDBObject) extends mutabl
170169
case (tryObj, n) =>
171170
tryObj.flatMap(obj => Try(obj.get(n).get.asInstanceOf[DBObject]))
172171
}
172+
173173
val res = tryLeaf.flatMap(obj => Try(obj.get(path.last).get.asInstanceOf[A])) match {
174174
case Success(list: BasicDBList) => Some(new MongoDBList(list))
175175
case Success(x) => Some(x)

casbah-query/src/test/scala/DSLCoreOperatorsSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class DSLCoreOperatorsSpec extends CasbahMutableSpecification {
155155
timeQuery must haveEntry("foo.$ne" -> timestamp)
156156
}
157157

158-
"Accept a right hand value of None" in skipped {
158+
"Accept a right hand value of None" in {
159159
val neQuery = "foo" $ne None
160160
neQuery must haveEntry("foo.$ne" -> None)
161161
}

0 commit comments

Comments
 (0)