Skip to content

Commit

Permalink
Scala 3 standalone generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jul 10, 2024
1 parent 33d5e3b commit 9a87181
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
13 changes: 12 additions & 1 deletion generator/app/controllers/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,18 @@ object Generators {
attributes = Seq("scala_generator")
),
status = lib.generator.Status.Production,
codeGenerator = Some(scala.models.Play2StandaloneModelsJson)
codeGenerator = Some(scala.models.Play2Scala2StandaloneModelsJson)
),
CodeGenTarget(
metaData = Generator(
key = "play_2_x_scala_3_standalone_json",
name = "Play 2.x Scala 3 standalone json",
description = Some("Generate case class with json serialization based on play-json, but do NOT include any other features that depend on the play framework (like QueryStringBindable)"),
language = Some("Scala"),
attributes = Seq("scala_generator")
),
status = lib.generator.Status.Production,
codeGenerator = Some(scala.models.Play2Scala3StandaloneModelsJson)
),
CodeGenTarget(
metaData = Generator(
Expand Down
7 changes: 4 additions & 3 deletions scala-generator/src/main/scala/models/Play2Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import generator.ServiceFileNames

import scala.generator.ScalaPrimitive.{DateIso8601Joda, DateTimeIso8601Joda, Uuid}

object Play2ModelsScala2 extends Play2Models(scala3Support = false)
object Play2ModelsScala3 extends Play2Models(scala3Support = true)
object Play2ModelsScala2 extends Play2Models(ScalaVersion(2))
object Play2ModelsScala3 extends Play2Models(ScalaVersion(3))

object Play2ModelImplicits {
val play: String = {
Expand All @@ -30,7 +30,8 @@ object Play2ModelImplicits {
}
}

case class Play2Models(scala3Support: Boolean) extends CodeGenerator {
case class Play2Models(version: ScalaVersion) extends CodeGenerator {
private[this] val scala3Support: Boolean = version.major >= 3

override def invoke(
form: InvocationForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package scala.models
import io.apibuilder.generator.v0.models.{File, InvocationForm}
import lib.generator.CodeGenerator

object Play2StandaloneModelsJson extends CodeGenerator {
object Play2Scala2StandaloneModelsJson extends PlayStandaloneModelsJson(ScalaVersion(2))
object Play2Scala3StandaloneModelsJson extends PlayStandaloneModelsJson(ScalaVersion(3))

case class PlayStandaloneModelsJson(version: ScalaVersion) extends CodeGenerator {

override def invoke(
form: InvocationForm
): Either[Seq[String], Seq[File]] = {
Right(Play2ModelsScala2.generateCode(form = form, addBindables = false, addHeader = true, useBuiltInImplicits = false))
Right(Play2Models(version).generateCode(form = form, addBindables = false, addHeader = true, useBuiltInImplicits = false))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Play2StandaloneModelsJsonSpec extends AnyFunSpec with Matchers {
it("quality") {
val quality = models.TestHelper.parseFile("/examples/quality.json")

Play2StandaloneModelsJson.invoke(InvocationForm(quality)) match {
Play2Scala2StandaloneModelsJson.invoke(InvocationForm(quality)) match {
case Left(errors) => fail(errors.mkString(", "))
case Right(files) => {
files.map(_.name) should be (Seq("GiltQualityV0Models.scala"))
Expand Down
4 changes: 2 additions & 2 deletions scala-generator/src/test/scala/models/RecursiveJsonSpec.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package scala.models

import io.apibuilder.generator.v0.models.InvocationForm
import org.scalatest.funspec.AnyFunSpec

import scala.generator.ScalaService
import org.scalatest.funspec.AnyFunSpec

class RecursiveJsonSpec extends AnyFunSpec {
private val service: ScalaService = ScalaService(models.TestHelper.parseFile("/examples/recursive-types.json"))
Expand All @@ -18,7 +18,7 @@ class RecursiveJsonSpec extends AnyFunSpec {
}

it("should work with imports") {
Play2StandaloneModelsJson.invoke(InvocationForm(service.service)) match {
Play2Scala2StandaloneModelsJson.invoke(InvocationForm(service.service)) match {
case Left(errors) => fail(errors.mkString(", "))
case Right(_) => //println(files.head.contents)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package models.generator
import io.apibuilder.generator.v0.models.InvocationForm
import models.FieldDefaultHelper._

import scala.models.Play2StandaloneModelsJson
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers

import scala.models.Play2Scala2StandaloneModelsJson

class ScalaDefaultsSpec extends AnyFunSpec with Matchers {
it("Should produce models with defaults and scala_generator.model_hint missing") {
val json = models.TestHelper.buildJson(requiredFalseWithDefaultsNoHints)

val form = InvocationForm(models.TestHelper.service(json.format()))

val Right(res) = Play2StandaloneModelsJson.invoke(form)
val Right(res) = Play2Scala2StandaloneModelsJson.invoke(form)

val jsOnly = res.head

Expand All @@ -36,7 +36,7 @@ class ScalaDefaultsSpec extends AnyFunSpec with Matchers {

val form = InvocationForm(models.TestHelper.service(json.format()))

val Right(res) = Play2StandaloneModelsJson.invoke(form)
val Right(res) = Play2Scala2StandaloneModelsJson.invoke(form)

val jsOnly = res.head

Expand All @@ -60,7 +60,7 @@ class ScalaDefaultsSpec extends AnyFunSpec with Matchers {

val form = InvocationForm(models.TestHelper.service(json.format()))

val Right(res) = Play2StandaloneModelsJson.invoke(form)
val Right(res) = Play2Scala2StandaloneModelsJson.invoke(form)

val jsOnly = res.head

Expand All @@ -84,7 +84,7 @@ class ScalaDefaultsSpec extends AnyFunSpec with Matchers {

val form = InvocationForm(models.TestHelper.service(json.format()))

val Right(res) = Play2StandaloneModelsJson.invoke(form)
val Right(res) = Play2Scala2StandaloneModelsJson.invoke(form)

val jsOnly = res.head

Expand All @@ -111,7 +111,7 @@ class ScalaDefaultsSpec extends AnyFunSpec with Matchers {

val form = InvocationForm(models.TestHelper.service(json.format()))

val Right(res) = Play2StandaloneModelsJson.invoke(form)
val Right(res) = Play2Scala2StandaloneModelsJson.invoke(form)

val jsOnly = res.head

Expand Down

0 comments on commit 9a87181

Please sign in to comment.