Skip to content

Instantly share code, notes, and snippets.

@teigen
Created April 3, 2012 19:09
Show Gist options
  • Save teigen/2294799 to your computer and use it in GitHub Desktop.
Save teigen/2294799 to your computer and use it in GitHub Desktop.
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))
}
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