From 4a453b172867aaeeb152930d4746115393fcb711 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sat, 16 Nov 2024 21:05:52 +0900 Subject: [PATCH] use given instead of implicit if Scala 3 --- .../scala/sbt/contraband/CodecCodeGen.scala | 9 ++++--- .../scala/sbt/contraband/ScalaCodeGen.scala | 4 +++ .../test/scala/GraphQLCodecCodeGenSpec.scala | 10 +++---- .../src/test/scala/JsonCodecCodeGenSpec.scala | 26 +++++++++---------- .../src/test/scala/JsonSchemaExample.scala | 14 +++++----- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/library/src/main/scala/sbt/contraband/CodecCodeGen.scala b/library/src/main/scala/sbt/contraband/CodecCodeGen.scala index ae8f8d7..7b3027d 100644 --- a/library/src/main/scala/sbt/contraband/CodecCodeGen.scala +++ b/library/src/main/scala/sbt/contraband/CodecCodeGen.scala @@ -38,6 +38,7 @@ class CodecCodeGen( } private def intersection: String = ScalaCodeGen.intersection(scalaVersion) + private def typeClassInstance: String = ScalaCodeGen.typeClassInstance(scalaVersion) override def generateEnum(s: Document, e: EnumTypeDefinition): ListMap[File, String] = { val fqn = fullyQualifiedName(e) @@ -66,7 +67,7 @@ class CodecCodeGen( |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait ${e.name.capitalize}Formats { $selfType - | implicit lazy val ${e.name}Format: JsonFormat[$fqn] = new JsonFormat[$fqn] { + | ${typeClassInstance} ${e.name}Format: JsonFormat[$fqn] = new JsonFormat[$fqn] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): $fqn = { | __jsOpt match { | case Some(__js) => @@ -120,7 +121,7 @@ class CodecCodeGen( |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait ${r.name.capitalize}Formats { $selfType - | implicit lazy val ${r.name}Format: JsonFormat[$fqn] = new JsonFormat[$fqn] { + | ${typeClassInstance} ${r.name}Format: JsonFormat[$fqn] = new JsonFormat[$fqn] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): $fqn = { | __jsOpt match { | case Some(__js) => @@ -156,7 +157,7 @@ class CodecCodeGen( |import _root_.sjsonnew.{ deserializationError, serializationError, Builder, JsonFormat, Unbuilder } | |trait ${name.capitalize}Formats { - | implicit lazy val ${name}Format: JsonFormat[$fqn] = new JsonFormat[$fqn] { + | ${typeClassInstance} ${name}Format: JsonFormat[$fqn] = new JsonFormat[$fqn] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): $fqn = { | deserializationError("No known implementation of ${i.name}.") | } @@ -181,7 +182,7 @@ class CodecCodeGen( |import _root_.sjsonnew.JsonFormat | |trait ${name.capitalize}Formats { $selfType - | implicit lazy val ${name}Format: JsonFormat[$fqn] = $flatUnionFormat + | ${typeClassInstance} ${name}Format: JsonFormat[$fqn] = $flatUnionFormat |}""".stripMargin } diff --git a/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala b/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala index 99d4574..7c60160 100644 --- a/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala +++ b/library/src/main/scala/sbt/contraband/ScalaCodeGen.scala @@ -488,4 +488,8 @@ object ScalaCodeGen { def intersection(scalaVersion: String): String = if (scalaVersion.startsWith("2.")) " with " else " & " + + def typeClassInstance(scalaVersion: String): String = + if (scalaVersion.startsWith("2.")) "implicit lazy val" + else "given" } diff --git a/library/src/test/scala/GraphQLCodecCodeGenSpec.scala b/library/src/test/scala/GraphQLCodecCodeGenSpec.scala index 0c23587..7461f09 100644 --- a/library/src/test/scala/GraphQLCodecCodeGenSpec.scala +++ b/library/src/test/scala/GraphQLCodecCodeGenSpec.scala @@ -23,7 +23,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines { |import _root_.sjsonnew.JsonFormat | |trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats => - | implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type") + | given InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type") |}""".stripMargin.stripSpace ) @@ -39,7 +39,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait ChildTypeFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val ChildTypeFormat: JsonFormat[com.example.ChildType] = new JsonFormat[com.example.ChildType] { + | given ChildTypeFormat: JsonFormat[com.example.ChildType] = new JsonFormat[com.example.ChildType] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.ChildType = { | __jsOpt match { | case Some(__js) => @@ -79,7 +79,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines { |import _root_.sjsonnew.JsonFormat | |trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats => - | implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type") + | given InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type") |}""".stripMargin.stripSpace ) @@ -97,7 +97,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait ChildTypeFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val ChildTypeFormat: JsonFormat[com.example.ChildType] = new JsonFormat[com.example.ChildType] { + | given ChildTypeFormat: JsonFormat[com.example.ChildType] = new JsonFormat[com.example.ChildType] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.ChildType = { | __jsOpt match { | case Some(__js) => @@ -137,7 +137,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines { |import _root_.sjsonnew.JsonFormat | |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") + | given InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type") |}""".stripMargin.stripSpace ) } diff --git a/library/src/test/scala/JsonCodecCodeGenSpec.scala b/library/src/test/scala/JsonCodecCodeGenSpec.scala index 8990653..ec52a34 100644 --- a/library/src/test/scala/JsonCodecCodeGenSpec.scala +++ b/library/src/test/scala/JsonCodecCodeGenSpec.scala @@ -28,7 +28,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait SimpleEnumerationExampleFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val simpleEnumerationExampleFormat: JsonFormat[_root_.simpleEnumerationExample] = new JsonFormat[_root_.simpleEnumerationExample] { + | given simpleEnumerationExampleFormat: JsonFormat[_root_.simpleEnumerationExample] = new JsonFormat[_root_.simpleEnumerationExample] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.simpleEnumerationExample = { | __jsOpt match { | case Some(__js) => @@ -67,7 +67,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ deserializationError, serializationError, Builder, JsonFormat, Unbuilder } | |trait SimpleInterfaceExampleFormats { - | implicit lazy val simpleInterfaceExampleFormat: JsonFormat[_root_.simpleInterfaceExample] = new JsonFormat[_root_.simpleInterfaceExample] { + | given simpleInterfaceExampleFormat: JsonFormat[_root_.simpleInterfaceExample] = new JsonFormat[_root_.simpleInterfaceExample] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.simpleInterfaceExample = { | deserializationError("No known implementation of simpleInterfaceExample.") | } @@ -93,7 +93,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.JsonFormat | |trait OneChildInterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol with generated.ChildRecordFormats => - | implicit lazy val oneChildInterfaceExampleFormat: JsonFormat[_root_.oneChildInterfaceExample] = flatUnionFormat1[_root_.oneChildInterfaceExample, _root_.childRecord]("type") + | given oneChildInterfaceExampleFormat: JsonFormat[_root_.oneChildInterfaceExample] = flatUnionFormat1[_root_.oneChildInterfaceExample, _root_.childRecord]("type") |}""".stripMargin.unindent) code(new File("generated", "childRecordFormats.scala")).unindent should equalLines("""/** | * This code is generated using [[https://www.scala-sbt.org/contraband]]. @@ -105,7 +105,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait ChildRecordFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val childRecordFormat: JsonFormat[_root_.childRecord] = new JsonFormat[_root_.childRecord] { + | given childRecordFormat: JsonFormat[_root_.childRecord] = new JsonFormat[_root_.childRecord] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.childRecord = { | __jsOpt match { | case Some(__js) => @@ -143,7 +143,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.JsonFormat | |trait NestedProtocolExampleFormats { self: sjsonnew.BasicJsonProtocol with generated.ChildRecordFormats => - | implicit lazy val nestedProtocolExampleFormat: JsonFormat[_root_.nestedProtocolExample] = flatUnionFormat1[_root_.nestedProtocolExample, _root_.ChildRecord]("type") + | given nestedProtocolExampleFormat: JsonFormat[_root_.nestedProtocolExample] = flatUnionFormat1[_root_.nestedProtocolExample, _root_.ChildRecord]("type") |}""".stripMargin.unindent) code.contains(new File("generated", "nestedProtocolFormats.scala")) shouldEqual false } @@ -163,7 +163,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ deserializationError, serializationError, Builder, JsonFormat, Unbuilder } | |trait GenerateArgDocExampleFormats { - | implicit lazy val generateArgDocExampleFormat: JsonFormat[_root_.generateArgDocExample] = new JsonFormat[_root_.generateArgDocExample] { + | given generateArgDocExampleFormat: JsonFormat[_root_.generateArgDocExample] = new JsonFormat[_root_.generateArgDocExample] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.generateArgDocExample = { | deserializationError("No known implementation of generateArgDocExample.") | } @@ -189,7 +189,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait SimpleRecordExampleFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val simpleRecordExampleFormat: JsonFormat[_root_.simpleRecordExample] = new JsonFormat[_root_.simpleRecordExample] { + | given simpleRecordExampleFormat: JsonFormat[_root_.simpleRecordExample] = new JsonFormat[_root_.simpleRecordExample] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.simpleRecordExample = { | __jsOpt match { | case Some(__js) => @@ -225,7 +225,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait GrowableAddOneFieldFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val growableAddOneFieldFormat: JsonFormat[_root_.growableAddOneField] = new JsonFormat[_root_.growableAddOneField] { + | given growableAddOneFieldFormat: JsonFormat[_root_.growableAddOneField] = new JsonFormat[_root_.growableAddOneField] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.growableAddOneField = { | __jsOpt match { | case Some(__js) => @@ -261,7 +261,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait FooFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val FooFormat: JsonFormat[_root_.Foo] = new JsonFormat[_root_.Foo] { + | given FooFormat: JsonFormat[_root_.Foo] = new JsonFormat[_root_.Foo] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.Foo = { | __jsOpt match { | case Some(__js) => @@ -299,7 +299,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") { package generated import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } trait PrimitiveTypesExample2Formats { self: sjsonnew.BasicJsonProtocol => -implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypesExample2] = new JsonFormat[_root_.primitiveTypesExample2] { +given primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypesExample2] = new JsonFormat[_root_.primitiveTypesExample2] { override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.primitiveTypesExample2 = { __jsOpt match { case Some(__js) => @@ -338,7 +338,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait PrimitiveTypesExampleFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val primitiveTypesExampleFormat: JsonFormat[_root_.primitiveTypesExample] = new JsonFormat[_root_.primitiveTypesExample] { + | given primitiveTypesExampleFormat: JsonFormat[_root_.primitiveTypesExample] = new JsonFormat[_root_.primitiveTypesExample] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.primitiveTypesExample = { | __jsOpt match { | case Some(__js) => @@ -384,7 +384,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes |import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } | |trait PrimitiveTypesNoLazyExampleFormats { self: sjsonnew.BasicJsonProtocol => - | implicit lazy val primitiveTypesNoLazyExampleFormat: JsonFormat[_root_.primitiveTypesNoLazyExample] = new JsonFormat[_root_.primitiveTypesNoLazyExample] { + | given primitiveTypesNoLazyExampleFormat: JsonFormat[_root_.primitiveTypesNoLazyExample] = new JsonFormat[_root_.primitiveTypesNoLazyExample] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.primitiveTypesNoLazyExample = { | __jsOpt match { | case Some(__js) => @@ -444,7 +444,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes |import _root_.sjsonnew.{ deserializationError, serializationError, Builder, JsonFormat, Unbuilder } | |trait GreetingFormats { - | implicit lazy val GreetingFormat: JsonFormat[_root_.Greeting] = new JsonFormat[_root_.Greeting] { + | given GreetingFormat: JsonFormat[_root_.Greeting] = new JsonFormat[_root_.Greeting] { | override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): _root_.Greeting = { | deserializationError("No known implementation of Greeting.") | } diff --git a/library/src/test/scala/JsonSchemaExample.scala b/library/src/test/scala/JsonSchemaExample.scala index 312a557..4beb070 100644 --- a/library/src/test/scala/JsonSchemaExample.scala +++ b/library/src/test/scala/JsonSchemaExample.scala @@ -958,7 +958,7 @@ package generated import _root_.sjsonnew.JsonFormat trait GreetingsFormats { self: generated.GreetingHeaderFormats with generated.PriorityLevelFormats with sjsonnew.BasicJsonProtocol with generated.SimpleGreetingFormats with generated.GreetingExtraImplFormats with generated.GreetingWithAttachmentsFormats => -implicit lazy val GreetingsFormat: JsonFormat[com.example.Greetings] = flatUnionFormat3[com.example.Greetings, com.example.SimpleGreeting, com.example.GreetingExtraImpl, com.example.GreetingWithAttachments]("type") +given GreetingsFormat: JsonFormat[com.example.Greetings] = flatUnionFormat3[com.example.Greetings, com.example.SimpleGreeting, com.example.GreetingExtraImpl, com.example.GreetingWithAttachments]("type") } /** * This code is generated using [[https://www.scala-sbt.org/contraband]]. @@ -970,7 +970,7 @@ package generated import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } trait SimpleGreetingFormats { self: generated.GreetingHeaderFormats with generated.PriorityLevelFormats with sjsonnew.BasicJsonProtocol => -implicit lazy val SimpleGreetingFormat: JsonFormat[com.example.SimpleGreeting] = new JsonFormat[com.example.SimpleGreeting] { +given SimpleGreetingFormat: JsonFormat[com.example.SimpleGreeting] = new JsonFormat[com.example.SimpleGreeting] { override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.SimpleGreeting = { __jsOpt match { case Some(__js) => @@ -1001,7 +1001,7 @@ package generated import _root_.sjsonnew.JsonFormat trait GreetingExtraFormats { self: generated.GreetingHeaderFormats with generated.PriorityLevelFormats with sjsonnew.BasicJsonProtocol with generated.GreetingExtraImplFormats => -implicit lazy val GreetingExtraFormat: JsonFormat[com.example.GreetingExtra] = flatUnionFormat1[com.example.GreetingExtra, com.example.GreetingExtraImpl]("type") +given GreetingExtraFormat: JsonFormat[com.example.GreetingExtra] = flatUnionFormat1[com.example.GreetingExtra, com.example.GreetingExtraImpl]("type") } /** * This code is generated using [[https://www.scala-sbt.org/contraband]]. @@ -1013,7 +1013,7 @@ package generated import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } trait GreetingExtraImplFormats { self: generated.GreetingHeaderFormats with generated.PriorityLevelFormats with sjsonnew.BasicJsonProtocol => -implicit lazy val GreetingExtraImplFormat: JsonFormat[com.example.GreetingExtraImpl] = new JsonFormat[com.example.GreetingExtraImpl] { +given GreetingExtraImplFormat: JsonFormat[com.example.GreetingExtraImpl] = new JsonFormat[com.example.GreetingExtraImpl] { override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.GreetingExtraImpl = { __jsOpt match { case Some(__js) => @@ -1048,7 +1048,7 @@ package generated import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } trait GreetingWithAttachmentsFormats { self: generated.GreetingHeaderFormats with generated.PriorityLevelFormats with sjsonnew.BasicJsonProtocol => -implicit lazy val GreetingWithAttachmentsFormat: JsonFormat[com.example.GreetingWithAttachments] = new JsonFormat[com.example.GreetingWithAttachments] { +given GreetingWithAttachmentsFormat: JsonFormat[com.example.GreetingWithAttachments] = new JsonFormat[com.example.GreetingWithAttachments] { override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.GreetingWithAttachments = { __jsOpt match { case Some(__js) => @@ -1081,7 +1081,7 @@ package generated import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } trait GreetingHeaderFormats { self: generated.PriorityLevelFormats with sjsonnew.BasicJsonProtocol => -implicit lazy val GreetingHeaderFormat: JsonFormat[com.example.GreetingHeader] = new JsonFormat[com.example.GreetingHeader] { +given GreetingHeaderFormat: JsonFormat[com.example.GreetingHeader] = new JsonFormat[com.example.GreetingHeader] { override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.GreetingHeader = { __jsOpt match { case Some(__js) => @@ -1114,7 +1114,7 @@ package generated import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError } trait PriorityLevelFormats { self: sjsonnew.BasicJsonProtocol => -implicit lazy val PriorityLevelFormat: JsonFormat[com.example.PriorityLevel] = new JsonFormat[com.example.PriorityLevel] { +given PriorityLevelFormat: JsonFormat[com.example.PriorityLevel] = new JsonFormat[com.example.PriorityLevel] { override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): com.example.PriorityLevel = { __jsOpt match { case Some(__js) =>