Skip to content

Commit

Permalink
Use & for Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Oct 27, 2024
1 parent fd8671d commit a63970e
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 43 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ lazy val plugin = (project in file("plugin"))
crossScalaVersions := Seq(scala212, scala3),
pluginCrossBuild / sbtVersion := {
scalaBinaryVersion.value match {
case "2.13" => "1.2.8"
case "2.12" => "1.2.8" // set minimum sbt version
case "2.13" => "1.5.8"
case "2.12" => "1.5.8" // set minimum sbt version
case _ => "2.0.0-M2"
}
},
Expand Down
9 changes: 6 additions & 3 deletions library/src/main/scala/sbt/contraband/CodecCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class CodecCodeGen(
javaOption: String,
scalaArray: String,
formatsForType: ast.Type => List[String],
includedSchemas: List[Document]
includedSchemas: List[Document],
scalaVersion: String,
) extends CodeGenerator {
import CodecCodeGen._

Expand All @@ -36,6 +37,8 @@ class CodecCodeGen(
override def exitMultilineJavadoc(s: String) = s == "*/"
}

private def intersection: String = ScalaCodeGen.intersection(scalaVersion)

override def generateEnum(s: Document, e: EnumTypeDefinition): ListMap[File, String] = {
val fqn = fullyQualifiedName(e)
// Java enum can have additional parameter such as MERCURY (3.303e+23, 2.4397e6)
Expand Down Expand Up @@ -168,7 +171,7 @@ class CodecCodeGen(
val rfs = getAllRequiredFormats(s, i).distinct filter { _ != fmt }
val selfType = rfs match {
case Nil => ""
case fms => fms.mkString("self: ", " with ", " =>")
case fms => fms.mkString("self: ", intersection, " =>")
}
val typeFieldName = (toCodecTypeField(i.directives) orElse toCodecTypeField(s)).getOrElse("type")
val flatUnionFormat =
Expand Down Expand Up @@ -301,7 +304,7 @@ class CodecCodeGen(
private def makeSelfType(s: Document, d: TypeDefinition): String =
getRequiredFormats(s, d).distinct match {
case Nil => ""
case fms => fms.mkString("self: ", " with ", " =>")
case fms => fms.mkString("self: ", intersection, " =>")
}

private def genPackage(s: Document): String =
Expand Down
6 changes: 4 additions & 2 deletions library/src/main/scala/sbt/contraband/MixedCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class MixedCodeGen(
genScalaFileName: Any => File,
scalaSealProtocols: Boolean,
scalaPrivateConstructor: Boolean,
wrapOption: Boolean
wrapOption: Boolean,
scalaVersion: String,
) extends CodeGenerator {
val javaGen = new JavaCodeGen(javaLazy, javaOptional, instantiateJavaOptional, wrapOption)
val scalaGen = new ScalaCodeGen(
Expand All @@ -27,7 +28,8 @@ class MixedCodeGen(
genScalaFileName,
scalaSealProtocols,
scalaPrivateConstructor,
wrapOption
wrapOption,
scalaVersion,
)

def generate(s: Document): ListMap[File, String] =
Expand Down
14 changes: 11 additions & 3 deletions library/src/main/scala/sbt/contraband/ScalaCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class ScalaCodeGen(
genFile: Any => File,
scalaSealProtocols: Boolean,
scalaPrivateConstructor: Boolean,
wrapOption: Boolean
wrapOption: Boolean,
scalaVersion: String,
) extends CodeGenerator {

implicit object indentationConfiguration extends IndentationConfiguration {
Expand All @@ -30,6 +31,7 @@ class ScalaCodeGen(
override def enterMultilineJavadoc(s: String) = s == "/**"
override def exitMultilineJavadoc(s: String) = s == "*/"
}
def intersection: String = ScalaCodeGen.intersection(scalaVersion)

override def generate(s: Document): ListMap[File, String] =
(s.definitions collect { case td: TypeDefinition =>
Expand Down Expand Up @@ -176,12 +178,12 @@ class ScalaCodeGen(
private def genExtendsCode(parent: Option[InterfaceTypeDefinition], extraParents: List[String], superCtorArguments: String): String = {
val parentInterface = parent.map(p => s"${fullyQualifiedName(p)}($superCtorArguments)")
val allParents = parentInterface.toList ::: extraParents ::: List("Serializable")
val extendsCode = allParents.mkString(" with ")
val extendsCode = allParents.mkString(intersection)
if (extendsCode == "") "" else s"extends $extendsCode"
}

private def genExtendsCodeCompanion(companion: List[String]): String = {
val extendsCodeCompanion = companion.mkString(" with ")
val extendsCodeCompanion = companion.mkString(intersection)
if (extendsCodeCompanion == "") "" else s" extends $extendsCodeCompanion"
}

Expand Down Expand Up @@ -482,3 +484,9 @@ class ScalaCodeGen(
} mkString (EOL + EOL)
}
}

object ScalaCodeGen {
def intersection(scalaVersion: String): String =
if (scalaVersion.startsWith("2.")) " with "
else " & "
}
17 changes: 13 additions & 4 deletions library/src/test/scala/GraphQLCodecCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
|
|import _root_.sjsonnew.JsonFormat
|
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol with generated.ChildTypeFormats =>
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats =>
| implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type")
|}""".stripMargin.stripSpace
)
Expand Down Expand Up @@ -78,7 +78,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
|
|import _root_.sjsonnew.JsonFormat
|
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol with generated.ChildTypeFormats =>
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats =>
| implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type")
|}""".stripMargin.stripSpace
)
Expand Down Expand Up @@ -136,7 +136,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
|
|import _root_.sjsonnew.JsonFormat
|
|trait InterfaceExampleFormats { self: generated.TestItemDetailFormats with com.example.StatusFormats with sjsonnew.BasicJsonProtocol with generated.ChildTypeFormats =>
|trait InterfaceExampleFormats { self: generated.TestItemDetailFormats & com.example.StatusFormats & sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats =>
| implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type")
|}""".stripMargin.stripSpace
)
Expand All @@ -153,7 +153,16 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
case other =>
CodecCodeGen.formatsForType(other)
}
val scalaVersion = "3.5.1"

def mkCodecCodeGen: CodecCodeGen =
new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
new CodecCodeGen(
codecParents = codecParents,
instantiateJavaLazy = instantiateJavaLazy,
javaOption = javaOption,
scalaArray = scalaArray,
formatsForType = formatsForType,
includedSchemas = Nil,
scalaVersion = scalaVersion,
)
}
4 changes: 3 additions & 1 deletion library/src/test/scala/GraphQLMixedCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GraphQLMixedCodeGenSpec extends AnyFlatSpec with Matchers with Inside with
"generate(Record)" should "handle mixed Java-Scala inheritance" in {
val Success(ast) = SchemaParser.parse(mixedExample)
// println(ast)
val scalaVersion = "2.13.15"
val gen = new MixedCodeGen(
javaLazy,
CodeGen.javaOptional,
Expand All @@ -21,7 +22,8 @@ class GraphQLMixedCodeGenSpec extends AnyFlatSpec with Matchers with Inside with
genFileName,
scalaSealProtocols = true,
scalaPrivateConstructor = true,
wrapOption = true
wrapOption = true,
scalaVersion = scalaVersion,
)
val code = gen.generate(ast)

Expand Down
12 changes: 7 additions & 5 deletions library/src/test/scala/GraphQLScalaCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|* @param field something
|*/
|final class TypeExample private (
|val field: Option[java.net.URL]) extends Intf1 with Serializable {
|val field: Option[java.net.URL]) extends Intf1 & Serializable {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: TypeExample => (this.field == x.field)
| case _ => false
Expand Down Expand Up @@ -288,7 +288,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|}
|final class ChildType private (
| val name: Option[String],
| field: Option[Int]) extends com.example.InterfaceExample(field) with Serializable {
| field: Option[Int]) extends com.example.InterfaceExample(field) & Serializable {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: ChildType => (this.name == x.name) && (this.field == x.field)
| case _ => false
Expand Down Expand Up @@ -365,7 +365,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
"""package com.example
|/** Example of an interface */
|sealed abstract class IntfExample(
| val field: Option[Int]) extends Interface1 with Interface2 with Serializable {
| val field: Option[Int]) extends Interface1 & Interface2 & Serializable {
| // Some extra code...
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: IntfExample => (this.field == x.field)
Expand All @@ -379,13 +379,14 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
| }
|}
|
|object IntfExample extends CompanionInterface1 with CompanionInterface2 {
|object IntfExample extends CompanionInterface1 & CompanionInterface2 {
| // Some extra companion code...
|}
|""".stripMargin.stripSpace
)
}

def scalaVersion: String = "3.5.1"
def mkScalaCodeGen: ScalaCodeGen =
new ScalaCodeGen(
javaLazy,
Expand All @@ -395,7 +396,8 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
genFileName,
scalaSealProtocols = true,
scalaPrivateConstructor = true,
wrapOption = true
wrapOption = true,
scalaVersion = scalaVersion,
)
val javaLazy = "com.example.Lazy"
val outputFile = new File("output.scala")
Expand Down
29 changes: 15 additions & 14 deletions library/src/test/scala/JsonCodecCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
val javaOption = "com.example.Option"
val scalaArray = "Vector"
val formatsForType: ast.Type => List[String] = CodecCodeGen.formatsForType
val scalaVersion = "2.13.15"

override def enumerationGenerateSimple = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val enumeration = JsonParser.EnumTypeDefinition.parse(simpleEnumerationExample)
val code = gen generate enumeration

Expand Down Expand Up @@ -52,7 +53,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def interfaceGenerateSimple = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val intf = JsonParser.InterfaceTypeDefinition.parseInterface(simpleInterfaceExample)
val code = gen generate intf

Expand All @@ -78,7 +79,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def interfaceGenerateOneChild = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val intf = JsonParser.InterfaceTypeDefinition.parseInterface(oneChildInterfaceExample)
val code = gen generate intf

Expand Down Expand Up @@ -128,7 +129,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def interfaceGenerateNested = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val intf = JsonParser.InterfaceTypeDefinition.parseInterface(nestedInterfaceExample)
val code = gen generate intf

Expand All @@ -149,7 +150,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {

def interfaceGenerateMessages = {
val schema = JsonParser.Document.parse(generateArgDocExample)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
val code = gen generate schema

code.head._2.unindent should equalLines("""/**
Expand All @@ -174,7 +175,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def recordGenerateSimple = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val record = JsonParser.ObjectTypeDefinition.parse(simpleRecordExample)
val code = gen generate record

Expand Down Expand Up @@ -210,7 +211,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def recordGrowZeroToOneField = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val record = JsonParser.ObjectTypeDefinition.parse(growableAddOneFieldExample)
val code = gen generate record

Expand Down Expand Up @@ -246,7 +247,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def recordGrowZeroToOneToTwoFields: Unit = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val record = JsonParser.ObjectTypeDefinition.parse(growableZeroToOneToTwoFieldsExample)
val code = gen generate record

Expand Down Expand Up @@ -284,7 +285,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
}

override def recordPrimitives: Unit = {
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
val record = JsonParser.ObjectTypeDefinition.parse(primitiveTypesExample2)
val code = gen generate record

Expand Down Expand Up @@ -325,7 +326,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes

override def schemaGenerateTypeReferences = {
val schema = JsonParser.Document.parse(primitiveTypesExample)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
val code = gen generate schema

code.head._2.unindent should equalLines("""/**
Expand Down Expand Up @@ -371,7 +372,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes

override def schemaGenerateTypeReferencesNoLazy = {
val schema = JsonParser.Document.parse(primitiveTypesNoLazyExample)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
val code = gen generate schema

code.head._2.unindent should equalLines("""/**
Expand Down Expand Up @@ -408,14 +409,14 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes

override def schemaGenerateComplete = {
val schema = JsonParser.Document.parse(completeExample)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
val code = gen generate schema
code.values.mkString.unindent should equalLines(completeExampleCodeCodec.unindent)
}

override def schemaGenerateCompletePlusIndent = {
val schema = JsonParser.Document.parse(completeExample)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
val code = gen generate schema

code.values.mkString.withoutEmptyLines should equalLines(completeExampleCodeCodec.withoutEmptyLines)
Expand All @@ -431,7 +432,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes
| }
| ]
|}""".stripMargin)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
val code = gen generate schema

code.head._2.unindent should equalLines("""/**
Expand Down
4 changes: 3 additions & 1 deletion library/src/test/scala/JsonScalaCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ object primitiveTypesExample2 {
code.head._2.withoutEmptyLines should equalLines(completeExampleCodeScala.withoutEmptyLines)
}

def scalaVersion: String = "2.13.15"
def mkScalaCodeGen: ScalaCodeGen =
new ScalaCodeGen(
javaLazy,
Expand All @@ -475,7 +476,8 @@ object primitiveTypesExample2 {
genFileName,
scalaSealProtocols = true,
scalaPrivateConstructor = true,
wrapOption = true
wrapOption = true,
scalaVersion = scalaVersion,
)
val javaLazy = "com.example.Lazy"
val outputFile = new File("output.scala")
Expand Down
Loading

0 comments on commit a63970e

Please sign in to comment.