Last active
December 14, 2015 00:38
-
-
Save arturaz/4999882 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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