diff --git a/build.sbt b/build.sbt index 3762bb8..5309d8d 100644 --- a/build.sbt +++ b/build.sbt @@ -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" } }, diff --git a/library/src/main/scala/sbt/contraband/CodecCodeGen.scala b/library/src/main/scala/sbt/contraband/CodecCodeGen.scala index 2aeb834..ae8f8d7 100644 --- a/library/src/main/scala/sbt/contraband/CodecCodeGen.scala +++ b/library/src/main/scala/sbt/contraband/CodecCodeGen.scala @@ -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._ @@ -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) @@ -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 = @@ -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 = diff --git a/library/src/main/scala/sbt/contraband/MixedCodeGen.scala b/library/src/main/scala/sbt/contraband/MixedCodeGen.scala index d9d43f0..c2525ac 100644 --- a/library/src/main/scala/sbt/contraband/MixedCodeGen.scala +++ b/library/src/main/scala/sbt/contraband/MixedCodeGen.scala @@ -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( @@ -27,7 +28,8 @@ class MixedCodeGen( genScalaFileName, scalaSealProtocols, scalaPrivateConstructor, - wrapOption + wrapOption, + scalaVersion, ) def generate(s: Document): ListMap[File, String] = diff --git a/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala b/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala index 3235d9b..99d4574 100644 --- a/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala +++ b/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala @@ -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 { @@ -482,3 +483,9 @@ class ScalaCodeGen( } mkString (EOL + EOL) } } + +object ScalaCodeGen { + def intersection(scalaVersion: String): String = + if (scalaVersion.startsWith("2.")) " with " + else " & " +} diff --git a/library/src/test/scala/GraphQLCodecCodeGenSpec.scala b/library/src/test/scala/GraphQLCodecCodeGenSpec.scala index 2209e54..0c23587 100644 --- a/library/src/test/scala/GraphQLCodecCodeGenSpec.scala +++ b/library/src/test/scala/GraphQLCodecCodeGenSpec.scala @@ -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 ) @@ -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 ) @@ -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 ) @@ -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, + ) } diff --git a/library/src/test/scala/GraphQLMixedCodeGenSpec.scala b/library/src/test/scala/GraphQLMixedCodeGenSpec.scala index 95edda6..a246b9b 100644 --- a/library/src/test/scala/GraphQLMixedCodeGenSpec.scala +++ b/library/src/test/scala/GraphQLMixedCodeGenSpec.scala @@ -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, @@ -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) diff --git a/library/src/test/scala/GraphQLScalaCodeGenSpec.scala b/library/src/test/scala/GraphQLScalaCodeGenSpec.scala index 3d47d8e..6545e78 100644 --- a/library/src/test/scala/GraphQLScalaCodeGenSpec.scala +++ b/library/src/test/scala/GraphQLScalaCodeGenSpec.scala @@ -386,6 +386,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines { ) } + def scalaVersion: String = "3.5.1" def mkScalaCodeGen: ScalaCodeGen = new ScalaCodeGen( javaLazy, @@ -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") diff --git a/library/src/test/scala/JsonCodecCodeGenSpec.scala b/library/src/test/scala/JsonCodecCodeGenSpec.scala index 5f6f369..8990653 100644 --- a/library/src/test/scala/JsonCodecCodeGenSpec.scala +++ b/library/src/test/scala/JsonCodecCodeGenSpec.scala @@ -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 @@ -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 @@ -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 @@ -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 @@ -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("""/** @@ -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 @@ -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 @@ -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 @@ -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 @@ -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("""/** @@ -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("""/** @@ -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) @@ -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("""/** diff --git a/library/src/test/scala/JsonScalaCodeGenSpec.scala b/library/src/test/scala/JsonScalaCodeGenSpec.scala index 8a11d16..5d67882 100644 --- a/library/src/test/scala/JsonScalaCodeGenSpec.scala +++ b/library/src/test/scala/JsonScalaCodeGenSpec.scala @@ -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, @@ -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") diff --git a/plugin/src/main/scala/ContrabandPlugin.scala b/plugin/src/main/scala/ContrabandPlugin.scala index 422f613..fdcc038 100644 --- a/plugin/src/main/scala/ContrabandPlugin.scala +++ b/plugin/src/main/scala/ContrabandPlugin.scala @@ -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" } } ) @@ -131,6 +131,7 @@ object Generate { scalaFileNames: Any => File, scalaSealInterface: Boolean, scalaPrivateConstructor: Boolean, + scalaVersion: String, wrapOption: Boolean, codecParents: List[String], instantiateJavaLazy: String => String, @@ -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) { @@ -234,6 +244,7 @@ object Generate { scalaFileNames, scalaSealInterface, scalaPrivateConstructor, + scalaVersion, wrapOption, codecParents, instantiateJavaLazy, diff --git a/plugin/src/sbt-test/sbt-contraband/simple-new-schema/build.sbt b/plugin/src/sbt-test/sbt-contraband/simple-new-schema/build.sbt index b4bfe3d..2cde5fd 100644 --- a/plugin/src/sbt-test/sbt-contraband/simple-new-schema/build.sbt +++ b/plugin/src/sbt-test/sbt-contraband/simple-new-schema/build.sbt @@ -1,4 +1,3 @@ -scalaVersion := "2.13.15" +scalaVersion := "3.5.1" name := "example" - enablePlugins(ContrabandPlugin, JsonCodecPlugin) diff --git a/plugin/src/sbt-test/sbt-contraband/simple-new-schema/src/main/scala/com/example/Example.scala b/plugin/src/sbt-test/sbt-contraband/simple-new-schema/src/main/scala/com/example/Example.scala index e21d9f0..e7224ac 100644 --- a/plugin/src/sbt-test/sbt-contraband/simple-new-schema/src/main/scala/com/example/Example.scala +++ b/plugin/src/sbt-test/sbt-contraband/simple-new-schema/src/main/scala/com/example/Example.scala @@ -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 -}