Skip to content

Commit

Permalink
use given instead of implicit if Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Nov 16, 2024
1 parent 8c4d504 commit ed4ef31
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 5 additions & 4 deletions library/src/main/scala/sbt/contraband/CodecCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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}.")
| }
Expand All @@ -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

}
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/scala/sbt/contraband/ScalaCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
10 changes: 5 additions & 5 deletions library/src/test/scala/GraphQLCodecCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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) =>
Expand Down Expand Up @@ -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
)

Expand All @@ -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) =>
Expand Down Expand Up @@ -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
)
}
Expand Down

0 comments on commit ed4ef31

Please sign in to comment.