I've been using Salat within a Play2 webapp for MongoDB interaction (via Leon Radley's neat SBT plugin). Works great, however as we have many case class DTOs due to nesting (to mirror nested JSON/BSON model definitions) we wanted to neatly define the nested case class DTOs in a companion object of the base model type.
This prevents namespace clashes in the soup of case class DTOs that have proliferated in our Salat data access layer. e.g., we define a "Description" case class, but it is redefined for different model objects so we wanted a neat way to namespace it. We tried using a companion Object for this purpose, like in this little snippet/example:
package models.mongo
import com.novus.salat._
import com.mongodb.casbah.Imports._
import se.radley.plugin.salat._
case class TestSalatDto(
id: ObjectId = new ObjectId,
title: String,
description: TestSalatDto.Description)
object TestSalatDto {
case class Description(
brief: String,
full: String)
}
This all compiles beautifully, but fails at runtime with a com.novus.salat.util.ToObjectGlitch: argument type mismatch during unmarshalling from Mongo.
If we simply switch the companion object definition into a package defintion (a slightly messier way of encapsulating the DTO soup) then all runs fine (the unmarshalling works a treat).
I'm confused as to why declaring the case classes within an Object fails at runtime; is there a trick to defining case classes (Salat DTOs) within an Object?