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 4a453b1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 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
26 changes: 13 additions & 13 deletions library/src/test/scala/JsonCodecCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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.")
| }
Expand All @@ -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]].
Expand All @@ -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) =>
Expand Down Expand Up @@ -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
}
Expand All @@ -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.")
| }
Expand All @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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.")
| }
Expand Down
14 changes: 7 additions & 7 deletions library/src/test/scala/JsonSchemaExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]].
Expand All @@ -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) =>
Expand Down Expand Up @@ -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]].
Expand All @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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) =>
Expand Down

0 comments on commit 4a453b1

Please sign in to comment.