EDIT: For those who wonder how I plan to solve it according to the accepted answer, see Nested Values here.
I'm using Play Framework with Scala and Reactive Mongo.
Currently I'm creating my case classes and forms like this:
case class Person(
_id : Option[BSONObjectID],
name: string,
city: string)
object Person {
val form: Form[Person] = Form {
mapping(
"_id" -> optional(of[String] verifying pattern(
"""[a-fA-F0-9]{24}""".r,
"constraint.objectId",
"error.objectId")),
"name"-> text,
"city"-> text)
{ (id,name, city) => Person(id.map(new BSONObjectID(_)), name, city) }
{ person =>Some(person._id.map(_.stringify), person.name, person.city) }
}
}
If I was using a simple type in the _id
property, like String
, I could do something simpler, like:
object Person {
val form: Form[Person] = Form {
mapping(
"_id" -> text,
"name"-> text,
"city"-> text
)(Person.apply)(Person.unapply)
}
}
So I thought I could create my own apply method that would change the first parameter, using currying. I would define something like this:
def apply2(id: Option[String]) = {
val bsonid = id.map(new BSONObjectID(_))
(Person.apply _).curried(bsonid)
}
My theory, which implementation doesn't work, is that I would partially apply a BSONObjectID
parameter to the Person.apply
function, which value would come from the apply2
parameter called id
. It doesn't work.
I'm a lazy guy who doesn't want to type a bunch of things just because now I have a new situation which isn't supported by default... The currying is one of my bets, but any solution that would make it easier to create a Form is acceptable.
I just need a way to identify the object so I can delete or update it after, but the current way is kinda boring to type, and I think that the _id
field created by MongoDB is perfect.
Is there a way I could make things easier or I just need to stop being lazy?