I have got a case class with optional parameters (Option[String]) and an according shape with optional fields (see below). When I insert an object into the collection it is saved as expected:
> var cursor = db.name.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("4cc00de3133d2675eff9e887"), "de" : "DE" }
name: com.osinka.mongodb.shape.ShapedCollection[ch.petag.ism.model.I18nName] = ShapedCollection[ch.petag.ism.model.I18nNameShape$](name)()
trait I18nNameIn[A] extends ObjectIn[I18nName, A] {
lazy val de = Field.optional("de", _.de)
lazy val en = Field.optional("en", _.en)
lazy val fr = Field.optional("fr", _.fr)
lazy val it = Field.optional("it", _.it)
lazy val * = de :: en :: fr :: it :: Nil
def factory(dbo: DBObject): Option[I18nName] = {
for {
de(d) <- Some(dbo)
en(e) <- Some(dbo)
fr(f) <- Some(dbo)
it(i) <- Some(dbo)
} yield I18nName(Option(d), Option(e), Option(f), Option(i))
}
}
object I18nNameShape extends MongoObjectShape[I18nName] with I18nNameIn[I18nName]
case class I18nName(
de: Option[String] = None,
en: Option[String] = None,
fr: Option[String] = None,
it: Option[String] = None) extends MongoObject {
def name(implicit locale: Locale): String = {
import Locale._
locale match {
case GERMAN => de getOrElse mongoOID.toString
case ENGLISH => de getOrElse mongoOID.toString
case FRENCH => de getOrElse mongoOID.toString
case ITALIAN => de getOrElse mongoOID.toString
case _ => mongoOID.toString
}
}
}