diff --git a/generator/app/controllers/Generators.scala b/generator/app/controllers/Generators.scala index ccf3a8dd..23db4c16 100644 --- a/generator/app/controllers/Generators.scala +++ b/generator/app/controllers/Generators.scala @@ -268,6 +268,16 @@ object Generators { status = lib.generator.Status.Production, codeGenerator = Some(scala.models.Play26Controllers) ), + CodeGenTarget( + metaData = Generator( + key = "play_2_9_scala_3_controllers", + name = "Play 2.9 Scala 3 controllers", + description = Some("""Generate Play Controllers for the resources with scala 3 support."""), + language = Some("Scala") + ), + status = lib.generator.Status.Production, + codeGenerator = Some(scala.models.Play29Scala3Controllers) + ), CodeGenTarget( metaData = Generator( key = "scala_models", diff --git a/scala-generator/src/main/scala/models/Play26Controllers.scala b/scala-generator/src/main/scala/models/Play26Controllers.scala index b78281b0..2a76d5c0 100644 --- a/scala-generator/src/main/scala/models/Play26Controllers.scala +++ b/scala-generator/src/main/scala/models/Play26Controllers.scala @@ -7,7 +7,11 @@ import lib.generator.CodeGenerator import scala.generator._ -object Play26Controllers extends CodeGenerator { + +object Play26Controllers extends PlayControllers(scalaVersion = ScalaVersion(3)) +object Play29Scala3Controllers extends PlayControllers(scalaVersion = ScalaVersion(3)) + +abstract class PlayControllers(scalaVersion: ScalaVersion) extends CodeGenerator { private val BodyArgName: String = "body" @@ -125,7 +129,7 @@ object Play26Controllers extends CodeGenerator { if (obj.headerArg.isEmpty) { base } else { - s"$base.withHeaders(r.headers: _*)" + s"$base.withHeaders(r.headers${scalaVersion.splat})" } } diff --git a/scala-generator/src/main/scala/models/Play2ClientGenerator.scala b/scala-generator/src/main/scala/models/Play2ClientGenerator.scala index aa9f3f0d..b7037ee0 100644 --- a/scala-generator/src/main/scala/models/Play2ClientGenerator.scala +++ b/scala-generator/src/main/scala/models/Play2ClientGenerator.scala @@ -7,6 +7,10 @@ import lib.generator.CodeGenerator import scala.generator._ +case class ScalaVersion(major: Int) { + val splat: String = if (major >= 3) { "*" } else { ": _*" } +} + case class PlayFrameworkVersion( name: String, config: ScalaClientMethodConfig, @@ -16,7 +20,7 @@ case class PlayFrameworkVersion( useSpecificAddMethods: Boolean, scala3Support: Boolean ) { - val splat: String = if (scala3Support) { "*" } else { ": _*" } + val scalaVersion: ScalaVersion = ScalaVersion(if (scala3Support) { 3 } else { 2 }) } trait Play2CodeGenerator extends CodeGenerator { @@ -189,7 +193,7 @@ case class Play2ClientGeneratorImpl( } val patchMethod = if (version.supportsHttpPatch) { - s"""_logRequest("PATCH", _requestHolder(path).$addHeadersMethod(requestHeaders${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).patch(body.getOrElse(play.api.libs.json.Json.obj()))""" + s"""_logRequest("PATCH", _requestHolder(path).$addHeadersMethod(requestHeaders${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).patch(body.getOrElse(play.api.libs.json.Json.obj()))""" } else { s"""sys.error("PATCH method is not supported in Play Framework Version ${version.name}")""" } @@ -197,8 +201,8 @@ case class Play2ClientGeneratorImpl( val headers = Headers(form) val headerString = headers.scala. - map { case (name, value) => s""""$name" -> ${value}""" }. - mkString(s".$addHeadersMethod(\n ", ",\n ", "") + s"\n ).$addHeadersMethod(defaultHeaders${version.splat})" + map { case (name, value) => s""""$name" -> $value""" }. + mkString(s".$addHeadersMethod(\n ", ",\n ", "") + s"\n ).$addHeadersMethod(defaultHeaders${version.scalaVersion.splat})" val responseEnvelopeString = version.config.responseEnvelopeClassName match { case None => "" case Some(name) => PlayScalaClientCommon.responseEnvelopeTrait(name).indentString() + "\n\n" @@ -262,28 +266,28 @@ ${if (version.config.expectsInjectedWsClient) "" else " import play.api.Pla ): scala.concurrent.Future[${version.config.responseClass}] = {$scala3Imports method.toUpperCase match { case "GET" => { - _logRequest("GET", _requestHolder(path).$addHeadersMethod(requestHeaders${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).get() + _logRequest("GET", _requestHolder(path).$addHeadersMethod(requestHeaders${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).get() } case "POST" => { - _logRequest("POST", _requestHolder(path).$addHeadersMethod(_withJsonContentType(requestHeaders)${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).post(body.getOrElse(play.api.libs.json.Json.obj())) + _logRequest("POST", _requestHolder(path).$addHeadersMethod(_withJsonContentType(requestHeaders)${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).post(body.getOrElse(play.api.libs.json.Json.obj())) } case "PUT" => { - _logRequest("PUT", _requestHolder(path).$addHeadersMethod(_withJsonContentType(requestHeaders)${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).put(body.getOrElse(play.api.libs.json.Json.obj())) + _logRequest("PUT", _requestHolder(path).$addHeadersMethod(_withJsonContentType(requestHeaders)${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).put(body.getOrElse(play.api.libs.json.Json.obj())) } case "PATCH" => { $patchMethod } case "DELETE" => { - _logRequest("DELETE", _requestHolder(path).$addHeadersMethod(requestHeaders${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).delete() + _logRequest("DELETE", _requestHolder(path).$addHeadersMethod(requestHeaders${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).delete() } case "HEAD" => { - _logRequest("HEAD", _requestHolder(path).$addHeadersMethod(requestHeaders${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).head() + _logRequest("HEAD", _requestHolder(path).$addHeadersMethod(requestHeaders${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).head() } case "OPTIONS" => { - _logRequest("OPTIONS", _requestHolder(path).$addHeadersMethod(requestHeaders${version.splat}).$addQueryStringMethod(queryParameters${version.splat})).options() + _logRequest("OPTIONS", _requestHolder(path).$addHeadersMethod(requestHeaders${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})).options() } case _ => { - _logRequest(method, _requestHolder(path).$addHeadersMethod(requestHeaders${version.splat}).$addQueryStringMethod(queryParameters${version.splat})) + _logRequest(method, _requestHolder(path).$addHeadersMethod(requestHeaders${version.scalaVersion.splat}).$addQueryStringMethod(queryParameters${version.scalaVersion.splat})) sys.error("Unsupported method[%s]".format(method)) } }