im getting in my api some case class that looks like this:
case class UserUpdate(
name: Option[String],
age: Option[String],
city: Option[String])
from this case class I need to build update json for mongo that looks like:
{"basicInfo.name": "new nameeee","age.vatId": "23"}
since all the fields are optional, I need to go over the fields and build it by the ones that are defined.
so I did something like this:
val updateAsMap = Json.toJson(userUpdateObj).as[JsObject]
.fields.map(fieldTpl => fieldTpl.copy(_1 = s"basicInfo.${fieldTpl._1}")).toMap
val userUpdateJson = Json.toJson(updateAsMap)
val query = json("userId" -> userId)
val update = json("$set" -> userUpdateJson)
is there a better suggestion, something that will look more elegant?