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 1b61124
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 37 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
9 changes: 8 additions & 1 deletion 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 Down Expand Up @@ -482,3 +483,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
4 changes: 3 additions & 1 deletion library/src/test/scala/GraphQLScalaCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
)
}

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
19 changes: 15 additions & 4 deletions plugin/src/main/scala/ContrabandPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ object ContrabandPlugin extends AutoPlugin {
contrabandSjsonNewVersion := {
scalaBinaryVersion.value match {
case "3" =>
"0.13.1"
"0.14.0-M1"
case _ =>
"0.9.0"
"0.10.1"
}
}
)
Expand All @@ -131,6 +131,7 @@ object Generate {
scalaFileNames: Any => File,
scalaSealInterface: Boolean,
scalaPrivateConstructor: Boolean,
scalaVersion: String,
wrapOption: Boolean,
codecParents: List[String],
instantiateJavaLazy: String => String,
Expand Down Expand Up @@ -158,9 +159,18 @@ object Generate {
scalaFileNames,
scalaSealInterface,
scalaPrivateConstructor,
wrapOption
wrapOption,
scalaVersion,
)
val jsonFormatsGenerator = new CodecCodeGen(
codecParents = codecParents,
instantiateJavaLazy = instantiateJavaLazy,
javaOption = javaOption,
scalaArray = scalaArray,
formatsForType = formatsForType,
includedSchemas = input,
scalaVersion = scalaVersion,
)
val jsonFormatsGenerator = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, input)

val datatypes =
if (createDatatypes) {
Expand Down Expand Up @@ -234,6 +244,7 @@ object Generate {
scalaFileNames,
scalaSealInterface,
scalaPrivateConstructor,
scalaVersion,
wrapOption,
codecParents,
instantiateJavaLazy,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
scalaVersion := "2.13.15"
scalaVersion := "3.5.1"
name := "example"

enablePlugins(ContrabandPlugin, JsonCodecPlugin)
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.example

object Example extends App {
@main def hello(): Unit =
val martin = Person("Martin", Some(24))

println("Who is Martin? " + martin)
Dis.x.toString
}

0 comments on commit 1b61124

Please sign in to comment.