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

Commit

Permalink
SCALA-69: Maps saved to DBObject are now eagerly converted to a
Browse files Browse the repository at this point in the history
          DBObject, from factory, builder and put methods.

Conflicts:

	casbah-commons/src/main/scala/MongoDBObject.scala
  • Loading branch information
Brendan W. McAdams committed May 16, 2012
1 parent 2e11d16 commit caacc84
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
48 changes: 34 additions & 14 deletions casbah-commons/src/main/scala/MongoDBObject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions casbah-commons/src/test/scala/MongoDBObjectSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
*/
}

}

}
}

}
Expand Down

0 comments on commit caacc84

Please sign in to comment.