Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactoring for compliance with type class based sjson serialization

  • Loading branch information...
commit efa4f9343f88d53f3f59ff7e90ab246104a37833 1 parent 2308240
Debasish Ghosh authored
View
48 src/main/scala/scouch/db/Database.scala
@@ -35,6 +35,8 @@ object Couch {
def apply(hostname: String, user: String, pass: String): Couch = Couch(hostname, 5984, Some((user, pass)))
}
+import sjson.json._
+
/** Requests on a particular database and CouchDB host. */
case class Db(couch: Couch, name: String) extends Request(couch / name) with Js {
val all_docs = this / "_all_docs" ># ('rows ! list andThen { _ map 'id ! str })
@@ -49,6 +51,11 @@ case class Db(couch: Couch, name: String) extends Request(couch / name) with Js
}
/** create a doc from an object with auto id generation */
+ def doc_tc[T](obj: T)(implicit tjs: Writes[T]) = {
+ this <:< Map("Content-Type" -> "application/json") << JsonSerialization.tojson(obj)(tjs) ># %('id ! str, 'rev ! str)
+ }
+
+ /** create a doc from an object with auto id generation */
def doc(obj: JsValue) = {
this <:< Map("Content-Type" -> "application/json") << obj ># %('id ! str, 'rev ! str)
}
@@ -139,6 +146,15 @@ case class Db(couch: Couch, name: String) extends Request(couch / name) with Js
case (_, _, x) => (null, null, x.asInstanceOf[T])
}
}
+
+ /** get an entity of type <tt>T</tt> based on its id. Returns a
+ <tt>Tuple3</tt> of <tt>(id, ref, T)</tt> */
+ def get_tc[T](id: String)(implicit fjs: Reads[T]) =
+ this / encode(id, Request.factoryCharset) ># {
+ case s@_ =>
+ val (id, ref) = getIdAndRef(s)
+ (id, ref, JsonSerialization.fromjson[T](s)(fjs))
+ }
/** get an entity of type <tt>T</tt> based on its id and rev. Returns a
<tt>Tuple3</tt> of <tt>(id, ref, T)</tt>
@@ -164,6 +180,15 @@ case class Db(couch: Couch, name: String) extends Request(couch / name) with Js
case (_, _, x) => (null, null, x.asInstanceOf[T])
}
}
+
+ /** get an entity of type <tt>T</tt> based on its id and rev. Returns a
+ <tt>Tuple3</tt> of <tt>(id, ref, T)</tt> */
+ def get_tc[T](id: String, rev: String)(implicit fjs: Reads[T]) =
+ this / encode(id, Request.factoryCharset) <<? Map("rev" -> rev) ># {
+ case s@_ =>
+ val (id, ref) = getIdAndRef(s)
+ (id, ref, JsonSerialization.fromjson(s)(fjs))
+ }
/** conditional get with ETag support on revision of CouchDB. Returns a
<tt>Tuple3</tt> of <tt>(id, ref, T)</tt> if doing an actual fetch. Otherwise
@@ -179,6 +204,16 @@ case class Db(couch: Couch, name: String) extends Request(couch / name) with Js
}
}
+ /** conditional get with ETag support on revision of CouchDB. Returns a
+ <tt>Tuple3</tt> of <tt>(id, ref, T)</tt> if doing an actual fetch. Otherwise
+ throws a <tt>dispatch.StatusCode(304, _)</tt> */
+ def conditionalGet_tc[T](id: String, rev: String)(implicit fjs: Reads[T]) =
+ this / encode(id, Request.factoryCharset) <:< Map("If-None-Match" -> ("\"" + rev + "\"")) ># {
+ case s@_ =>
+ val (id, ref) = getIdAndRef(s)
+ (id, ref, JsonSerialization.fromjson(s)(fjs))
+ }
+
/** fetch the view for the query. The query can be built using the dsl as
specified in <tt>ViewQuery</tt> */
def view(v: Query) = {
@@ -262,6 +297,11 @@ case class Doc(val db: Db, val id: String) extends Request(db / encode(id, Reque
def add[T <: AnyRef](obj: T) = {
this <<< JsBean.toJSON(obj) >|
}
+
+ /** add an object (bean) to the document */
+ def add_tc[T](obj: T)(implicit tjs: Writes[T]) = {
+ this <<< JsValue.toJson(JsonSerialization.tojson(obj)(tjs)) >|
+ }
/** add attachment to a document. None as the <tt>rev</tt> will create a new document
as well */
@@ -290,6 +330,14 @@ case class Doc(val db: Db, val id: String) extends Request(db / encode(id, Reque
case Updated.rev(rev) => (Id._rev << rev)(js)
}
}
+
+ /** update the document of specified revision, with the specified object */
+ def update_tc[T](obj: T, r: String)(implicit tjs: Writes[T]) = {
+ val js = (Id._rev << r)(JsonSerialization.tojson(obj)(tjs))
+ this <<< JsValue.toJson(js) ># {
+ case Updated.rev(rev) => (Id._rev << rev)(js)
+ }
+ }
def update(js: JsValue) = this <<< JsValue.toJson(js) ># {
case Updated.rev(rev) => (Id._rev << rev)(js)
View
18 src/main/scala/scouch/db/Document.scala
@@ -4,7 +4,7 @@ import sjson.json._
import scala.reflect._
import scala.annotation.target._
-object DesignDocument {
+object DesignDocumentX {
val PREFIX = "_design/"
def extendId(id: String) =
@@ -24,12 +24,13 @@ case class DesignDocument(var _id: String,
views: Map[String, View],
@(JSONProperty @getter)(ignoreIfNull = true, ignore = false)
- validate_doc_update: String) {
+ validate_doc_update: String,
+ language: String = "javascript") {
if (_id != null)
- if (!_id.startsWith(DesignDocument.PREFIX))
- _id = DesignDocument.extendId(_id)
+ if (!_id.startsWith(DesignDocumentX.PREFIX))
+ _id = DesignDocumentX.extendId(_id)
- var language = "javascript"
+ // var language = "javascript"
private [db] def this() = this(null, null, Map[String, View](), null)
@@ -49,3 +50,10 @@ case class DesignDocument(var _id: String,
)
}
}
+
+object DesignDocumentFormats {
+ import sjson.json.DefaultProtocol._
+ import ViewFormats._
+ implicit val DesignDocumentFormat: Format[DesignDocument] =
+ asProduct5("_id", "_rev", "views", "validate_doc_update", "language")(DesignDocument)(DesignDocument.unapply(_).get)
+}
View
6 src/main/scala/scouch/db/View.scala
@@ -18,3 +18,9 @@ case class View(
override def toString =
"map: " + map + " reduce: " + reduce
}
+
+object ViewFormats {
+ import sjson.json.DefaultProtocol._
+ implicit val ViewFormat: Format[View] =
+ asProduct2("map", "reduce")(View)(View.unapply(_).get)
+}
View
3  src/test/scala/scouch/db/ScalaValidationSpec.scala
@@ -46,8 +46,7 @@ class ScalaValidationSpec extends Spec with ShouldMatchers with BeforeAndAfterEa
val vfn = """(ndoc: dispatch.json.JsValue,
odoc: dispatch.json.JsValue, req: Any) => {}"""
- val d = DesignDocument("foo_1", null, Map[String, View](), vfn)
- d.language = "scala"
+ val d = DesignDocument("foo_1", null, Map[String, View](), vfn, "scala")
val de = Doc(test, d._id)
View
12 src/test/scala/scouch/db/ScalaViewServerSpec.scala
@@ -40,8 +40,7 @@ class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfterA
}
describe("Create a design document, for scala view") {
- val d = DesignDocument("power", null, Map[String, View](), null)
- d.language = "scala"
+ val d = DesignDocument("power", null, Map[String, View](), null, "scala")
val mapfn1 = """(doc: dispatch.json.JsValue) => {
val it = sjson.json.JsBean.fromJSON(doc, Some(classOf[scouch.db.TestBeans.Item_1]));
for (st <- it.prices)
@@ -81,8 +80,7 @@ class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfterA
revision = sh._2
}
it("update the document with 2 views") {
- val doc = DesignDocument(d._id, revision, Map("power_lunch" -> vi_1, "mega_lunch" -> vi_2), null)
- doc.language = "scala"
+ val doc = DesignDocument(d._id, revision, Map("power_lunch" -> vi_1, "mega_lunch" -> vi_2), null, "scala")
http(de update(doc, revision))
nir = http(de ># %(Id._id, Id._rev))
nir._1 should equal(d._id)
@@ -103,8 +101,7 @@ class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfterA
}
describe("Create a design document, for scala view with map and reduce") {
- val d = DesignDocument("big", null, Map[String, View](), null)
- d.language = "scala"
+ val d = DesignDocument("big", null, Map[String, View](), null, "scala")
val mapfn1 = """(doc: dispatch.json.JsValue) => {
val it = sjson.json.JsBean.fromJSON(doc, Some(classOf[scouch.db.TestBeans.Item_1]));
for (st <- it.prices)
@@ -138,8 +135,7 @@ class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfterA
revision = sh._2
}
it("update the document with the view") {
- val doc = DesignDocument(d._id, revision, Map("big_lunch" -> vi_1), null)
- doc.language = "scala"
+ val doc = DesignDocument(d._id, revision, Map("big_lunch" -> vi_1), null, "scala")
http(de update(doc, revision))
nir = http(de ># %(Id._id, Id._rev))
nir._1 should equal(d._id)
View
24 src/test/scala/scouch/db/ViewServerWithObjectsSpec.scala
@@ -73,11 +73,9 @@ class ViewServerWithObjectsSpec extends Spec with ShouldMatchers with BeforeAndA
val redCarsView = new View(redCars, null)
val redCarsPriceView = new View(redCarsPrice, null)
- val cv = DesignDocument("car_views", null, Map[String, View](), null)
- cv.language = "scala"
+ val cv = DesignDocument("car_views", null, Map[String, View](), null, "scala")
- val rcv = DesignDocument(cv._id, null, Map("red_cars" -> redCarsView, "red_cars_price" -> redCarsPriceView), null)
- rcv.language = "scala"
+ val rcv = DesignDocument(cv._id, null, Map("red_cars" -> redCarsView, "red_cars_price" -> redCarsPriceView), null, "scala")
http(Doc(carDb, rcv._id) add rcv)
val ls1 = http(carDb view(
@@ -145,11 +143,9 @@ class ViewServerWithObjectsSpec extends Spec with ShouldMatchers with BeforeAndA
val redCarsPriceView = new View(redCarsPrice, redfn1)
- val cv = DesignDocument("red_car_views", null, Map[String, View](), null)
- cv.language = "scala"
+ val cv = DesignDocument("red_car_views", null, Map[String, View](), null, "scala")
- val rcv = DesignDocument(cv._id, null, Map("red_cars_price" -> redCarsPriceView), null)
- rcv.language = "scala"
+ val rcv = DesignDocument(cv._id, null, Map("red_cars_price" -> redCarsPriceView), null, "scala")
http(Doc(carDb, rcv._id) add rcv)
val ls2 = http(carDb view(
@@ -176,11 +172,9 @@ class ViewServerWithObjectsSpec extends Spec with ShouldMatchers with BeforeAndA
val redCarsPriceView = new View(mapForRedCars, reduceToPriceSum)
- val cv = DesignDocument("sum_car_views", null, Map[String, View](), null)
- cv.language = "scala"
+ val cv = DesignDocument("sum_car_views", null, Map[String, View](), null, "scala")
- val rcv = DesignDocument(cv._id, null, Map("red_cars_sum_price" -> redCarsPriceView), null)
- rcv.language = "scala"
+ val rcv = DesignDocument(cv._id, null, Map("red_cars_sum_price" -> redCarsPriceView), null, "scala")
http(Doc(carDb, rcv._id) add rcv)
val ls1 = http(carDb view(
@@ -215,11 +209,9 @@ class ViewServerWithObjectsSpec extends Spec with ShouldMatchers with BeforeAndA
val redCarsPriceView = new View(mapForRedCars, reduceToMaxPrice)
- val cv = DesignDocument("max_car_views", null, Map[String, View](), null)
- cv.language = "scala"
+ val cv = DesignDocument("max_car_views", null, Map[String, View](), null, "scala")
- val rcv = DesignDocument(cv._id, null, Map("red_cars_max_price" -> redCarsPriceView), null)
- rcv.language = "scala"
+ val rcv = DesignDocument(cv._id, null, Map("red_cars_max_price" -> redCarsPriceView), null, "scala")
http(Doc(carDb, rcv._id) add rcv)
val ls1 = http(carDb view(

0 comments on commit efa4f93

Please sign in to comment.
Something went wrong with that request. Please try again.