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 ) }