Thank you for the suggestion.
I move the definition of the mapper to my repository file which already dealt with the imports.
However, I cannot confirm any progress because now, I am stuck with an Json format error. If I understand, I must explicitly define how to map the Scala Enum type (enumeration or with trait).
which is logical somewhat. However, there again, I'm unable to find a single example which works in my case.
this is how the CatStatus is defined:
sealed trait CatStatus
case object Awake extends CatStatus
case object Sleeping extends CatStatus
case object Dead extends CatStatus
I tried following
this page and got this:
object TaskStatus {
implicit val catStatusReads: Reads[CatStatus] = ((JsPath \"task_status").read[String])(CatStatus.apply _)
implicit val catStatusWrites: Writes[CatStatus] = ((JsPath \"task_status").write[CatStatus])(unlift(TaskStatus.unapply))
}
which clearly doesn't cut it. output is :
[error] XXX/app/models/Cat.scala:26: value apply is not a member of object models.CatStatus
[error] implicit val catStatusReads: Reads[CatStatus] = ((JsPath \"cat_status").read[String])(CatStatus.apply _)
[error] ^
[error] XXX/app/models/Cat.scala:26: type mismatch;
[error] found : play.api.libs.json.Reads[String]
[error] required: play.api.libs.json.Reads[models.CatStatus]
[error] implicit val catStatusReads: Reads[CatStatus] = ((JsPath \"cat_status").read[String])(CatStatus.apply _)
[error] ^
[error] XXX/app/models/Cat.scala:27: value unapply is not a member of object models.CatStatus
[error] implicit val catStatusWrites: Writes[CatStatus] = ((JsPath \"cat_status").write[CatStatus])(unlift(CatStatus.unapply))
it didn't help to define the enum this way:
object CatStatus extends Enumeration {
type CatStatus = Value
val Awake = Value("awake")
val Sleeping = Value("sleeping")
val Dead = Value("dead")
implicit val catStatusReads: Reads[CatStatus] = ((JsPath \"cat_status").read[String])(CatStatus.apply _)
implicit val catStatusWrites: Writes[CatStatus] = ((JsPath \"cat_status").write[CatStatus])(unlift(CatStatus.unapply))
}
output was:
[error] XXX/app/models/Cat.scala:12: overloaded method value read with alternatives:
[error] (t: String)play.api.libs.json.Reads[String] <and>
[error] (implicit r: play.api.libs.json.Reads[String])play.api.libs.json.Reads[String]
[error] cannot be applied to (Int => models.CatStatus.Value)
[error] implicit val catStatusReads: Reads[CatStatus] = ((JsPath \"cat_status").read[String])(CatStatus.apply _)
[error] ^
[error] XXX/app/models/Cat.scala:13: value unapply is not a member of object models.CatStatus
[error] implicit val catStatusWrites: Writes[CatStatus] = ((JsPath \"cat_status").write[CatStatus])(unlift(CatStatus.unapply))
[error] ^
Needless to say that I have no idea about what it is going on there and it just feels incredibly difficult to achieve something which is probably fairly easy.
If it was not clear before, I am trying to complete a simple CRUD application with the Play Framework using Scala, considering that my model (Cat) has an attribute (status) which has a defined set of values (Awake, Sleeping, Dead). This attribute is represented in the Postgresql database as an Enum.
So far, the only solid learning I have made is that I need the following 3:
- A way to represent the attribute with defined set of values in scala (Enumeration? trait?
Both?)
- A way to map this representation to the db enum
- A way to serialize this representation in Json
Is that right or did I get this part twisted as well? Is there anything else missing?
I would be thankful for any guidance concerning this, with as much details as possible, as I'm so lost, I'm not even sure in which files I am supposed to put the code snippets I find online.
Thank you.