Skip to content

Commit

Permalink
Add constructor helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmicansaglam committed Jul 7, 2014
1 parent 3eee99d commit 6aa397b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
14 changes: 12 additions & 2 deletions bson/src/main/scala/dsl/BsonDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ trait BsonDsl {
BSONDocument.pretty(document)
}

def $doc(item: Producer[BSONElement], items: Producer[BSONElement]*): BSONDocument = {
BSONDocument((Seq(item) ++ items): _*)
//**********************************************************************************************//
// Helpers
def $empty: BSONDocument = BSONDocument.empty

def $doc(elements: Producer[BSONElement]*): BSONDocument = {
BSONDocument(elements: _*)
}

def $arr(elements: Producer[BSONValue]*): BSONArray = {
BSONArray(elements: _*)
}

def $id[T](id: T)(implicit writer: BSONWriter[T, _ <: BSONValue]): BSONDocument = {
BSONDocument("_id" -> id)
}
// End of Helpers
//**********************************************************************************************//

//**********************************************************************************************//
// Top Level Logical Operators
Expand Down
42 changes: 42 additions & 0 deletions bson/src/test/scala/dsl/BsonDslSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package reactivemongo.extensions.dsl
import org.scalatest._
import reactivemongo.bson._
import reactivemongo.extensions.util.Logger
import reactivemongo.extensions.dao.Handlers._
import BsonDsl._
import org.joda.time.DateTime

class BsonDslSpec extends FlatSpec with Matchers {

Expand All @@ -30,6 +32,46 @@ class BsonDslSpec extends FlatSpec with Matchers {
dsl shouldBe expected
}

it should "construct bson document" in {
val dateTime = DateTime.now

val document = $doc(
"name" -> "haydar",
"surname" -> "cabbar",
"age" -> 32,
"salary" -> 999.99,
"birthday" -> dateTime,
"prefs" -> $doc(
"pref1" -> "value1",
"pref2" -> 1341453453L
),
"sizes" -> $arr("M", "L"),
"notices" -> $arr(
$doc("key1" -> "value1"),
$doc("key2" -> "value2")
)
)

val expected = BSONDocument(
"name" -> "haydar",
"surname" -> "cabbar",
"age" -> 32,
"salary" -> 999.99,
"birthday" -> BSONDateTime(dateTime.getMillis),
"prefs" -> BSONDocument(
"pref1" -> "value1",
"pref2" -> 1341453453L
),
"sizes" -> BSONArray("M", "L"),
"notices" -> BSONArray(
BSONDocument("key1" -> "value1"),
BSONDocument("key2" -> "value2")
)
)

document shouldBe expected
}

//**********************************************************************************************//
// Comparison Operators
it should "create complex document 2" in {
Expand Down
14 changes: 11 additions & 3 deletions json/src/main/scala/dsl/JsonDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ trait JsonDsl {

type Element = (Field, Value)

//**********************************************************************************************//
// Helpers
def $empty: JsObject = Json.obj()

def $doc(element: Element, elements: Element*): JsObject = {
Json.obj((Seq(element) ++ elements): _*)
def $doc(elements: Element*): JsObject = {
Json.obj(elements: _*)
}

def $arr(elements: Value*): JsArray = {
Json.arr(elements: _*)
}

def $id(id: Value): JsObject = {
$doc("_id" -> id)
Json.obj("_id" -> id)
}
// End of Helpers
//**********************************************************************************************//

//**********************************************************************************************//
// Top Level Logical Operators
Expand Down
41 changes: 41 additions & 0 deletions json/src/test/scala/dsl/JsonDslSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import reactivemongo.bson._
import play.api.libs.json._
import play.modules.reactivemongo.json.BSONFormats._
import JsonDsl._
import org.joda.time.DateTime

class JsonDslSpec extends FlatSpec with Matchers {

Expand All @@ -37,6 +38,46 @@ class JsonDslSpec extends FlatSpec with Matchers {
dsl shouldBe expected
}

it should "construct json document" in {
val dateTime = DateTime.now

val document = $doc(
"name" -> "haydar",
"surname" -> "cabbar",
"age" -> 32,
"salary" -> 999.99,
"birthday" -> dateTime,
"prefs" -> $doc(
"pref1" -> "value1",
"pref2" -> 1341453453L
),
"sizes" -> $arr("M", "L"),
"notices" -> $arr(
$doc("key1" -> "value1"),
$doc("key2" -> "value2")
)
)

val expected = Json.obj(
"name" -> "haydar",
"surname" -> "cabbar",
"age" -> 32,
"salary" -> 999.99,
"birthday" -> dateTime.getMillis,
"prefs" -> Json.obj(
"pref1" -> "value1",
"pref2" -> 1341453453L
),
"sizes" -> Json.arr("M", "L"),
"notices" -> Json.arr(
Json.obj("key1" -> "value1"),
Json.obj("key2" -> "value2")
)
)

document shouldBe expected
}

it should "create id document" in {
val id = BSONObjectID.generate
val dsl = $id(id)
Expand Down

0 comments on commit 6aa397b

Please sign in to comment.