Basic scenario:
case class Something(
date: Option[Date],
timestamp: Option[Date] = Some(new Date)
);
class Users private() extends MongoRecord[Users] with ObjectIdPk[Users] {
def meta = Users;
object things extends MongoCaseClassListField[Users, Something](this);
};
object Users extends Users with MongoMetaRecord[Users] {
};
def something(json: JValue) = {
val something = json.extract[Something];// does not have a timestamp field.
decompose(something); // again no timestamp field.
Users.where(_.email eqs email).findAndModify(_.things addToSet something).updateOne(true);
};
Problem: When a JSON without a timestamp
field is sent as a request, the database entry does not have a timestamp
field at all.
If timestamp: Date
instead of timestamp: Option[Date]
is used, the JSON extraction throws a MappingException
.
Q: How can a missing JSON field/case class param default to a value?
I would probably try something like this:
case class Something(date: Option[Date], timestamp: Option[Date]){
def this(date:Option[Date]) = this(date, Some(new Date))
}
This creates a separate, one argument constructor and passes the default date to the two-argument constructor. Running it through REPL, you can see that the timestamp seems to get set correctly:
scala> parse(""" { "date":"2013-07-08T21:37:10Z" } """)
res11: net.liftweb.json.JValue = JObject(List(JField(date,JString(2013-07-08T21:37:10Z))))
scala> res11.extract[Something]
res16: Something = Something(Some(Mon Jul 08 17:37:10 EDT 2013),Some(Mon Jul 08 17:43:52 EDT 2013))
scala> parse(""" {
| "date":"2013-07-08T21:37:10Z",
| "timestamp":"2013-07-08T21:37:10Z"
| } """)
res14: net.liftweb.json.JValue = JObject(List(JField(date,JString(2013-07-08T21:37:10Z)), JField(timestamp,JString(2013-07-08T21:37:10Z))))
scala> res14.extract[Something]
res17: Something = Something(Some(Mon Jul 08 17:37:10 EDT 2013),Some(Mon Jul 08 17:37:10 EDT 2013))