Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue 1066 - Add support to JObjectParser for Lists of BSON types

  • Loading branch information...
commit fbe22c1f93a4b5711dd068a4be6b570598cb0c45 1 parent de697c6
@eltimn eltimn authored
View
11 persistence/mongodb-record/src/test/scala/net/liftweb/mongodb/record/QueryExamplesSpec.scala
@@ -103,6 +103,11 @@ object QueryExamplesSpec extends Specification("QueryExamples Specification") wi
pebblesParents.length must_== 2
pebblesParents.map(_.id.is).filterNot(rubblesIds.contains(_)) must_== List()
+ // query for Bamm-Bamm's and Pebbles' parents using List[UUID]
+ val pebblesAndBammBammsParents = Person.findAll(("childId" -> ("$in" -> List(pebblesId, bammbammId))))
+
+ pebblesAndBammBammsParents.length must_== 4
+
// query for Dino's owners (ObjectId)
val dinosOwners = Person.findAll(("petId" -> dinoId))
@@ -121,6 +126,12 @@ object QueryExamplesSpec extends Specification("QueryExamples Specification") wi
flinstones.length must_== 2
flinstones.map(_.id.is).filterNot(flinstonesIds.contains(_)) must_== List()
+ // query for the Flinstones using a List[ObjectId]
+ val flinstones2 = Person.findAll(("_id" -> ("$in" -> flinstonesIds)))
+
+ flinstones2.length must_== 2
+ flinstones2.map(_.id.is).filterNot(flinstonesIds.contains(_)) must_== List()
+
// query using Dates
implicit val formats = Person.formats // this is needed for Dates
val qryDate = Calendar.getInstance
View
17 persistence/mongodb/src/main/scala/net/liftweb/mongodb/JObjectParser.scala
@@ -22,8 +22,7 @@ import scala.collection.JavaConversions._
import java.util.{Date, UUID}
import java.util.regex.Pattern
-import net.liftweb.json.Formats
-import net.liftweb.json.JsonAST._
+import net.liftweb.json._
import net.liftweb.common.Box
import com.mongodb.{BasicDBObject, BasicDBList, DBObject}
@@ -71,6 +70,14 @@ object JObjectParser {
val dbl = new BasicDBList
trimArr(arr).foreach { a =>
a match {
+ case JObject(JField("$oid", JString(s)) :: Nil) if (ObjectId.isValid(s)) =>
+ dbl.add(new ObjectId(s))
+ case JObject(JField("$regex", JString(s)) :: JField("$flags", JInt(f)) :: Nil) =>
+ dbl.add(Pattern.compile(s, f.intValue))
+ case JObject(JField("$dt", JString(s)) :: Nil) =>
+ formats.dateFormat.parse(s) foreach { d => dbl.add(d) }
+ case JObject(JField("$uuid", JString(s)) :: Nil) =>
+ dbl.add(UUID.fromString(s))
case JArray(arr) => dbl.add(parseArray(arr, formats))
case JObject(jo) => dbl.add(parseObject(jo, formats))
case jv: JValue => dbl.add(renderValue(jv, formats))
@@ -83,17 +90,15 @@ object JObjectParser {
val dbo = new BasicDBObject
trimObj(obj).foreach { jf =>
jf.value match {
- case JArray(arr) => dbo.put(jf.name, parseArray(arr, formats))
case JObject(JField("$oid", JString(s)) :: Nil) if (ObjectId.isValid(s)) =>
dbo.put(jf.name, new ObjectId(s))
case JObject(JField("$regex", JString(s)) :: JField("$flags", JInt(f)) :: Nil) =>
dbo.put(jf.name, Pattern.compile(s, f.intValue))
case JObject(JField("$dt", JString(s)) :: Nil) =>
- formats.dateFormat.parse(s) foreach {
- d => dbo.put(jf.name, d)
- }
+ formats.dateFormat.parse(s) foreach { d => dbo.put(jf.name, d) }
case JObject(JField("$uuid", JString(s)) :: Nil) =>
dbo.put(jf.name, UUID.fromString(s))
+ case JArray(arr) => dbo.put(jf.name, parseArray(arr, formats))
case JObject(jo) => dbo.put(jf.name, parseObject(jo, formats))
case jv: JValue => dbo.put(jf.name, renderValue(jv, formats))
}
View
46 persistence/mongodb/src/test/scala/net/liftweb/mongodb/BsonDSLSpec.scala
@@ -20,6 +20,7 @@ package mongodb
import BsonDSL._
import json._
+import scala.collection.JavaConversions._
import scala.util.matching.Regex
import java.util.{Date, UUID}
@@ -28,7 +29,7 @@ import java.util.regex.Pattern
import org.bson.types.ObjectId
import org.specs.Specification
-import com.mongodb.{DBObject}
+import com.mongodb.{BasicDBList, DBObject}
object BsonDSLSpec extends Specification("BsonDSL Specification") {
"BsonDSL" should {
@@ -40,6 +41,15 @@ object BsonDSLSpec extends Specification("BsonDSL Specification") {
dbo.get("id") must_== oid
}
+ "Convert List[ObjectId] properly" in {
+ val oidList = ObjectId.get :: ObjectId.get :: ObjectId.get :: Nil
+ val qry: JObject = ("ids" -> oidList)
+ val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
+ val oidList2: List[ObjectId] = dbo.get("ids").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[ObjectId])
+
+ oidList2 must_== oidList
+ }
+
"Convert Pattern properly" in {
val ptrn: Pattern = Pattern.compile("^Mongo", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE)
val qry: JObject = ("ptrn" -> ptrn)
@@ -50,6 +60,21 @@ object BsonDSLSpec extends Specification("BsonDSL Specification") {
ptrn2.flags must_== ptrn.flags
}
+ "Convert List[Pattern] properly" in {
+ val ptrnList =
+ Pattern.compile("^Mongo1", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE) ::
+ Pattern.compile("^Mongo2", Pattern.CASE_INSENSITIVE) ::
+ Pattern.compile("^Mongo3") :: Nil
+ val qry: JObject = ("ptrns" -> ptrnList)
+ val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
+ val ptrnList2: List[Pattern] = dbo.get("ptrns").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[Pattern])
+
+ for (i <- 0 to 2) {
+ ptrnList(i).pattern must_== ptrnList2(i).pattern
+ ptrnList(i).flags must_== ptrnList2(i).flags
+ }
+ }
+
"Convert Regex properly" in {
val regex: Regex = "^Mongo".r
val qry: JObject = ("regex" -> regex)
@@ -68,6 +93,15 @@ object BsonDSLSpec extends Specification("BsonDSL Specification") {
dbo.get("uuid") must_== uuid
}
+ "Convert List[UUID] properly" in {
+ val uuidList = UUID.randomUUID :: UUID.randomUUID :: UUID.randomUUID :: Nil
+ val qry: JObject = ("ids" -> uuidList)
+ val dbo: DBObject = JObjectParser.parse(qry)(DefaultFormats)
+ val uuidList2: List[UUID] = dbo.get("ids").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[UUID])
+
+ uuidList2 must_== uuidList
+ }
+
"Convert Date properly" in {
implicit val formats = DefaultFormats.lossless
val dt: Date = new Date
@@ -76,5 +110,15 @@ object BsonDSLSpec extends Specification("BsonDSL Specification") {
dbo.get("now") must_== dt
}
+
+ "Convert List[Date] properly" in {
+ implicit val formats = DefaultFormats.lossless
+ val dateList = new Date :: new Date :: new Date :: Nil
+ val qry: JObject = ("dts" -> dateList)
+ val dbo: DBObject = JObjectParser.parse(qry)
+ val dateList2: List[Date] = dbo.get("dts").asInstanceOf[BasicDBList].toList.map(_.asInstanceOf[Date])
+
+ dateList2 must_== dateList
+ }
}
}
View
15 persistence/mongodb/src/test/scala/net/liftweb/mongodb/QueryExamplesSpec.scala
@@ -75,12 +75,17 @@ object QueryExamplesSpec extends Specification("QueryExamples Specification") wi
val flinstonesIds = List(fred._id, wilma._id)
val rubblesIds = List(barney._id, betty._id)
- // query for Bamm-Bamm's parents (UUID)
+ // query for Bamm-Bamm's parents (UUID) by childId
val pebblesParents = Person.findAll(("childId" -> bammbammId))
pebblesParents.length must_== 2
pebblesParents.map(_._id).filterNot(rubblesIds.contains(_)) must_== List()
+ // query for Bamm-Bamm's and Pebbles' parents using List[UUID]
+ val pebblesAndBammBammsParents = Person.findAll(("childId" -> ("$in" -> List(pebblesId, bammbammId))))
+
+ pebblesAndBammBammsParents.length must_== 4
+
// query for Dino's owners (ObjectId)
val dinosOwners = Person.findAll(("petId" -> dinoId))
@@ -99,6 +104,12 @@ object QueryExamplesSpec extends Specification("QueryExamples Specification") wi
flinstones.length must_== 2
flinstones.map(_._id).filterNot(flinstonesIds.contains(_)) must_== List()
+ // query for the Flinstones using a List[ObjectId]
+ val flinstones2 = Person.findAll(("_id" -> ("$in" -> flinstonesIds)))
+
+ flinstones2.length must_== 2
+ flinstones2.map(_._id).filterNot(flinstonesIds.contains(_)) must_== List()
+
// query using Dates
implicit val formats = Person.formats // this is needed for Dates
val qryDate = Calendar.getInstance
@@ -114,7 +125,7 @@ object QueryExamplesSpec extends Specification("QueryExamples Specification") wi
people2.length must_== 3
people2.map(_._id).filterNot(List(wilma._id, barney._id, betty._id).contains(_)) must_== List()
- // query with Sort
+ // query all with Sort
val people3 = Person.findAll(JObject(Nil), ("birthDate" -> -1))
people3.length must_== 4
Please sign in to comment.
Something went wrong with that request. Please try again.