Skip to content

Commit

Permalink
Add TODO for primitive wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jul 10, 2024
1 parent 9a87181 commit 170c697
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 9 additions & 6 deletions scala-generator/src/main/scala/models/Play2Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,18 @@ case class Play2Json(
case Nil => {
s"${play2JsonCommon.implicitUnionReader(union)} = (json: play.api.libs.json.JsValue) => play.api.libs.json.JsError(\"Union has no declared types\")"
}
case first :: rest => {
val types = (Seq("default") ++ rest.map { scalaUnionType =>
case all => {
val types = all.map { scalaUnionType =>
if (PrimitiveWrapper.needsWrapper(scalaUnionType.datatype)) {
// TODO: add a deserializer for the wrapper type
}
s"${readerUnqualified(union, scalaUnionType)}.reads(json).map(_.asInstanceOf[T])"
}).mkString("Seq(\n ", ",\n ", "\n )")
}.mkString("Seq(\n ", ",\n ", "\n )")
s"""
|${play2JsonCommon.implicitUnionReader(union)} = (json: play.api.libs.json.JsValue) => {
| val default = ${readerUnqualified(union, first)}.reads(json).map(_.asInstanceOf[T])
| val all: Seq[play.api.libs.json.JsResult[T]] = $types
| all.view.find(_.isSuccess).getOrElse(default)
| $types.view.find(_.isSuccess).getOrElse {
| play.api.libs.json.JsSuccess(${union.name}UndefinedType(json.toString).asInstanceOf[T])
| }
|}
|""".stripMargin
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ object PrimitiveWrapper {
}
}

def needsWrapper(datatype: ScalaDatatype): Boolean = {
datatype match {
case p: ScalaPrimitive if PrimitiveWrapper.isBasicType(p) => true
case _ => false
}
}
}

case class PrimitiveWrapper(ssd: ScalaService) {
Expand Down

0 comments on commit 170c697

Please sign in to comment.