From 170c697c565b1e98f61ed065fba96b5c76eb9328 Mon Sep 17 00:00:00 2001 From: Michael Bryzek Date: Wed, 10 Jul 2024 15:58:56 -0400 Subject: [PATCH] Add TODO for primitive wrappers --- .../src/main/scala/models/Play2Json.scala | 15 +++++++++------ .../scala/models/generator/PrimitiveWrapper.scala | 6 ++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/scala-generator/src/main/scala/models/Play2Json.scala b/scala-generator/src/main/scala/models/Play2Json.scala index 6d04eed4..e9e4a2e1 100644 --- a/scala-generator/src/main/scala/models/Play2Json.scala +++ b/scala-generator/src/main/scala/models/Play2Json.scala @@ -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 } diff --git a/scala-generator/src/main/scala/models/generator/PrimitiveWrapper.scala b/scala-generator/src/main/scala/models/generator/PrimitiveWrapper.scala index e48f5b5a..53639e7d 100644 --- a/scala-generator/src/main/scala/models/generator/PrimitiveWrapper.scala +++ b/scala-generator/src/main/scala/models/generator/PrimitiveWrapper.scala @@ -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) {