Permalink
Browse files

SCALA-69: Maps saved to DBObject are now eagerly converted to a

          DBObject, from factory, builder and put methods.

Conflicts:

	casbah-commons/src/main/scala/MongoDBObject.scala
  • Loading branch information...
Brendan W. McAdams
Brendan W. McAdams committed May 16, 2012
1 parent 2e11d16 commit caacc841149181c4c5b35acbb379144927a552ba
@@ -162,19 +162,23 @@ class MongoDBObject(val underlying: DBObject = new BasicDBObject) extends scala.
def isPartialObject = underlying.isPartialObject
def markAsPartialObject() = underlying.markAsPartialObject()
def partialObject = isPartialObject
override def put(k: String, v: AnyRef) = v match {
case x: MongoDBObject => put(k, x.asDBObject)
case _v: Option[_] =>
underlying.put(k, _v.orNull) match {
case null => None
case value => Some(value)
}
case _ =>
underlying.put(k, v) match {
case null => None
case value => Some(value)
override def put(k: String, v: AnyRef) = {
val cvt = MongoDBObject.convertValue(v)
cvt match {
case _v: Option[_] =>
underlying.put(k, _v.orNull) match {
case null => None
case value => Some(value)
}
case _ =>
underlying.put(k, cvt) match {
case null => None
case value => Some(value)
}
}
}
def putAll(o: DBObject) { underlying.putAll(o) }
def removeField(key: String) = underlying.removeField(key)
def toMap = underlying.toMap
@@ -209,17 +213,33 @@ object MongoDBObject {
def newBuilder[A <: String, B <: Any]: Builder[(String, Any), DBObject] = new MongoDBObjectBuilder
protected[mongodb] def convertValue(v: Any): Any = v match {
case x: MongoDBObject =>
x.asDBObject
case m: scala.collection.Map[String, _] =>
// attempt to convert it to a DBObject
m.asDBObject
case _v: Option[_] =>
val n = convertValue(_v.orNull)
val z = Option(n)
z
case _ =>
v
}
}
sealed class MongoDBObjectBuilder extends Builder[(String, Any), DBObject] {
import com.mongodb.BasicDBObjectBuilder
protected val empty = BasicDBObjectBuilder.start
protected var elems = empty
override def +=(x: (String, Any)) = {
x._2 match {
override def +=(x: (String, Any)) = {
val cvt = MongoDBObject.convertValue(x._2)
cvt match {
case _v: Option[_] => elems.add(x._1, _v.orNull)
case _ =>elems.add(x._1, x._2)
case _ => elems.add(x._1, cvt)
}
this
}
@@ -281,6 +281,42 @@ class MongoDBObjectSpec extends CasbahMutableSpecification {
}
}
}
"Eager conversions of nested values" in {
"Map values saved as DBObject values should convert" in {
"From the MongoDBObject constructor" in {
val dbObj = MongoDBObject("foo" -> "bar", "x" -> 5,
"map" -> Map("spam" -> 8.2, "eggs" -> "bacon"))
val map: Option[DBObject] = dbObj.getAs[DBObject]("map")
map.orNull must beDBObject
/*
*map must haveEntries("spam" -> 8.2, "eggs" -> "bacon")
*/
}
"From the MongoDBObjectBuilder" in {
val b = MongoDBObject.newBuilder
b += "foo" -> "bar"
b += "x" -> 5
b += "map" -> Map("spam" -> 8.2, "eggs" -> "bacon")
val dbObj = b.result
val map: Option[DBObject] = dbObj.getAs[DBObject]("map")
map.orNull must beDBObject
/*
*map must haveEntries("spam" -> 8.2, "eggs" -> "bacon")
*/
}
"From the put method" in {
val dbObj = MongoDBObject("foo" -> "bar", "x" -> 5)
dbObj += ("map" -> Map("spam" -> 8.2, "eggs" -> "bacon"))
val map: Option[DBObject] = dbObj.getAs[DBObject]("map")
map.orNull must beDBObject
/*
*map must haveEntries("spam" -> 8.2, "eggs" -> "bacon")
*/
}
}
}
}
}

0 comments on commit caacc84

Please sign in to comment.