Skip to content

Commit

Permalink
Scala 3 play controller generator (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek authored Jun 30, 2024
1 parent 7e2d2bd commit 47cacaf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
10 changes: 10 additions & 0 deletions generator/app/controllers/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions scala-generator/src/main/scala/models/Play26Controllers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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})"
}
}

Expand Down
26 changes: 15 additions & 11 deletions scala-generator/src/main/scala/models/Play2ClientGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -189,16 +193,16 @@ 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}")"""
}

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"
Expand Down Expand Up @@ -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))
}
}
Expand Down

0 comments on commit 47cacaf

Please sign in to comment.