Created
November 20, 2011 22:33
-
-
Save behaghel/1381068 to your computer and use it in GitHub Desktop.
This file contains 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
case class Person(firstname: String, lastname: String) | |
type MongoReader[T] = MongoDBObject => T | |
type MongoWriter[T] = T => MongoDBObject | |
def readArray[T : MongoReader](name: String) = new MongoReader[List[T]] { | |
def apply(dbo: MongoDBObject) = { | |
println("dbo = %s".format(dbo.toString)) | |
println("dbo.%s = %s".format(name, dbo.as[BasicDBList](name).toString)) | |
dbo.as[BasicDBList](name).map { e => | |
implicitly[MongoReader[T]].apply(e.asInstanceOf[DBObject]) | |
}.toList | |
} | |
} | |
def writeArray[T : MongoWriter](name: String, dbo: MongoDBObject) = new MongoWriter[List[T]] { | |
def apply(xs: List[T]) = dbo += (name -> | |
xs.map { e => implicitly[MongoWriter[T]].apply(e) }.toList) | |
} | |
implicit object PersonWriter extends MongoWriter[Person] { | |
def apply(p: Person) = MongoDBObject( | |
"firstname" -> p.firstname, | |
"lastname" -> p.lastname) | |
} | |
implicit object PersonReader extends MongoReader[Person] { | |
def apply(dbo: MongoDBObject) = | |
Person(dbo.as[String]("firstname"), dbo.as[String]("lastname")) | |
} | |
scala> readArray[Person]("people").apply(writeArray[Person]("people", MongoDBObject()).apply(xs)) | |
23:21:31.080 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Registering Scala Conversions. | |
23:21:31.086 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Deserializers for Scala Conversions registering | |
23:21:31.087 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Serializers for Scala Conversions registering | |
23:21:31.088 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up OptionSerializer | |
23:21:31.093 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaJCollectionSerializer | |
23:21:31.100 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaRegexSerializers | |
23:21:31.102 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Hooking up scala.util.matching.Regex serializer | |
23:21:31.103 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Reached base registration method on MongoConversionHelper. | |
23:21:31.162 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Registering Scala Conversions. | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Deserializers for Scala Conversions registering | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Serializers for Scala Conversions registering | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up OptionSerializer | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaJCollectionSerializer | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaRegexSerializers | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Hooking up scala.util.matching.Regex serializer | |
23:21:31.163 [run-main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Reached base registration method on MongoConversionHelper. | |
dbo = Map(people -> List(Map(firstname -> me, lastname -> myself), Map(firstname -> you, lastname -> yourself))) | |
java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to com.mongodb.BasicDBList | |
at anima.casbah.MongoUtils$Bindings$$anon$4.apply(CasbahDomain.scala:310) | |
at anima.casbah.MongoUtils$Bindings$$anon$4.apply(CasbahDomain.scala:307) | |
at .<init>(<console>:24) | |
at .<clinit>(<console>) | |
at .<init>(<console>:11) | |
at .<clinit>(<console>) | |
at $print(<console>) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | |
at java.lang.reflect.Method.invoke(Method.java:597) | |
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) | |
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914) | |
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546) | |
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577) | |
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543) | |
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694) | |
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745) | |
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651) | |
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542) | |
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550) | |
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822) | |
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851) | |
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:51) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) | |
at java.lang.reflect.Method.invoke(Method.java:597) | |
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:57) | |
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:48) | |
at sbt.Console.console0$1(Console.scala:23) | |
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24) | |
at sbt.TrapExit$.executeMain$1(TrapExit.scala:33) | |
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment