Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

resolve GH-1: slightly improved runtime grater discovery to be more o…

…r less acceptable for now
  • Loading branch information...
commit db5784fd48c25c6b9af578e96edae636643218a8 1 parent 707d1b5
@maxaf maxaf authored
View
7 src/main/scala/salat/Grater.scala
@@ -10,7 +10,12 @@ import com.bumnetworks.salat.annotations.util._
abstract class Grater[X <: CaseClass](val clazz: Class[X])(implicit val ctx: Context) extends CasbahLogging {
ctx.accept(this)
- protected lazy val sym = ScalaSigParser.parse(clazz).get.topLevelClasses.head
+ protected lazy val sym = ScalaSigParser.parse(clazz) match {
+ case Some(x) =>
+ x.topLevelClasses.headOption.getOrElse(throw new Exception("parsed pickled Scala signature, but no expected type found: %s".format(clazz)))
+ case _ => throw new Exception("failed to parse pickled Scala signature from %s".format(clazz))
+ }
+
protected lazy val indexedFields = {
sym.children
.filter(_.isCaseAccessor)
View
24 src/main/scala/salat/package.scala
@@ -17,10 +17,32 @@ trait Context extends Logging {
log.trace("Context(%s) accepted Grater[%s]", name.getOrElse("<no name>"), grater.clazz)
}
+ // XXX: This check needs to be a little bit less naive. There are
+ // other types (Joda Time, anyone?) that are either directly
+ // interoperable with MongoDB, or are handled by Casbah's BSON
+ // encoders.
+ protected def suitable_?(clazz: String): Boolean =
+ !(clazz.startsWith("scala.") || clazz.startsWith("java.") || clazz.startsWith("javax."))
+
+ protected def suitable_?(clazz: Class[_]): Boolean = suitable_?(clazz.getName)
+
+ protected def generate_?(c: String): Option[Grater[_ <: CaseClass]] =
+ if (suitable_?(c)) {
+ try {
+ val clazz = Class.forName(c).asInstanceOf[Class[CaseClass]]
+ if (clazz.isInterface) None
+ else Some({ new Grater[CaseClass](clazz)(this) {} }.asInstanceOf[Grater[CaseClass]])
+ }
+ catch { case _ => None }
+ } else None
+
protected def generate(clazz: String): Grater[_ <: CaseClass] =
{ new Grater[CaseClass](Class.forName(clazz).asInstanceOf[Class[CaseClass]])(this) {} }.asInstanceOf[Grater[CaseClass]]
- def lookup(clazz: String): Option[Grater[_ <: CaseClass]] = graters.get(clazz)
+ def lookup(clazz: String): Option[Grater[_ <: CaseClass]] = graters.get(clazz) match {
+ case yes @ Some(_) => yes
+ case _ => generate_?(clazz)
+ }
def lookup_!(clazz: String): Grater[_ <: CaseClass] =
lookup(clazz).getOrElse(generate(clazz))
View
4 src/test/scala/gistfile1.scala
@@ -33,8 +33,8 @@ case class Item (
@RunWith(classOf[JUnitRunner])
class ItemRepositoryTest extends FlatSpec with ShouldMatchers {
- grater[Tweet]
- grater[Item]
+ // grater[Tweet]
+ // grater[Item]
"Serialization of a nested case class instance with Salat" should "work as expected" in {
object ItemSerializer extends Grater(classOf[Item])
Please sign in to comment.
Something went wrong with that request. Please try again.