Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use given instead of implicit if Scala 3 #188

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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