Skip to content

Instantly share code, notes, and snippets.

@arturaz
Last active December 14, 2015 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturaz/4999882 to your computer and use it in GitHub Desktop.
Save arturaz/4999882 to your computer and use it in GitHub Desktop.
object Model {
trait Mutation[A]{
val doc: A
}
case class Upsert[A](doc: A) extends Mutation[A]
case class Delete[A](doc: A) extends Mutation[A]
// Json format
import play.api.libs.functional.syntax._
class MutationFormat[A] extends Format[Mutation[A]] {
def writes(o: Mutation[A]) = Json.obj(
"kind" -> o match {
case _: Upsert[_] => "upsert"
case _: Delete[_] => "delete"
},
"doc" -> Json.toJson(o.doc)
)
def reads(js: JsValue) = {
(
(__ \ "kind").reads[String] ~
(__ \ "doc").reads[A]
).validate(js)
// Return JsResult (either JsSuccess or JsError)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment