I'm looking for a way to use two different forms for one case class. I was trying to do that with additional constructor, but failed with that. Look at the code snippet:
case class LoginDetails(password: String, field3: Option[Int], field4: String)
case class User(username: String, loginDetails: LoginDetails) {
def this(username: String, password: String, field3: Option[Int], field4: String) = this(username, LoginDetails(password, field3, field4))
// some logic inside
}
val loginDetailsForm = Form(
mapping(
"password" -> text,
"field3" -> optional(number),
"field4" -> text
)(LoginDetails.apply)(LoginDetails.unapply))
val oldForm = Form(
mapping(
"username" -> email,
"password" -> text,
"field3" -> optional(number),
"field4" -> text
)(User.apply)(User.unapply))
val newForm = Form(
mapping(
"username" -> email,
"loginDetails" -> loginDetailsForm.mapping
)(User.apply)(User.unapply))
What I'm trying to do is to support two API's (new and old one), but if I will have two copies of the same case class, I will have to make code logic duplicates in many many places in the rest of the code. If there is a way to do that?
Of course given code doesn't work and is used like an example.
Thanks!
You could try to add methods
applyOld
andunapplyOld
to companion object like this:You just need methods
(String, String, Option[Int], String) => User
andUser => Option[(String, String, Option[Int], String)]
, you could place them anywhere you like. For instance you could create objectUserOld
with methodsapply
andunapply
.Usage:
You can do the apply/unapply directly where the form is defined instead. Then you don't have to add any new constructors to the case class.