Created
April 3, 2012 19:09
-
-
Save teigen/2294799 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
package com.mongodb.casbah | |
package commons | |
object Syntax extends Syntax | |
trait Syntax { | |
implicit def name(name: String) = Name(name) | |
} | |
case class Name(name: String) { | |
def :=[A](a: A)(implicit value: Value[A]) = name -> value.value(a) | |
} | |
@annotation.implicitNotFound("${A} is not a valid MongoDb value") | |
trait Value[-A] { | |
def value(a:A):Any | |
} | |
object Value extends Values { | |
def apply[A](f:A => Any):Value[A] = new Value[A]{ | |
def value(a: A) = f(a) | |
} | |
} | |
trait Values extends LowPriority { | |
implicit val string = Value[String](identity) | |
implicit val int = Value[Int](identity) | |
implicit val double = Value[Double](identity) | |
implicit def option[A](implicit value:Value[A]) = Value[Option[A]](_.map(value.value)) | |
implicit val none = Value[None.type](identity) | |
implicit val nil = Value[Seq[Nothing]](identity) | |
} | |
trait LowPriority { | |
implicit def seq[A](implicit value:Value[A]) = Value[Seq[A]](_.map(value.value)) | |
} |
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
import com.mongodb.casbah.commons._ | |
class DocumentTest { | |
import Syntax._ | |
MongoDbObject( | |
"string" := "string", | |
"int" := 5, | |
"double" := 5.5, | |
"option" := Option[String]("x"), | |
"literal some" := Some("y"), | |
"literal none" := None, | |
"seq" := Seq("a", "b"), | |
"nil" := Nil | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment