Skip to content

Commit 8c4d504

Browse files
authored
Merge pull request #187 from eed3si9n/wip/with
Use & for Scala 3
2 parents fd8671d + 1b61124 commit 8c4d504

File tree

12 files changed

+74
-37
lines changed

12 files changed

+74
-37
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ lazy val plugin = (project in file("plugin"))
6969
crossScalaVersions := Seq(scala212, scala3),
7070
pluginCrossBuild / sbtVersion := {
7171
scalaBinaryVersion.value match {
72-
case "2.13" => "1.2.8"
73-
case "2.12" => "1.2.8" // set minimum sbt version
72+
case "2.13" => "1.5.8"
73+
case "2.12" => "1.5.8" // set minimum sbt version
7474
case _ => "2.0.0-M2"
7575
}
7676
},

library/src/main/scala/sbt/contraband/CodecCodeGen.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class CodecCodeGen(
2020
javaOption: String,
2121
scalaArray: String,
2222
formatsForType: ast.Type => List[String],
23-
includedSchemas: List[Document]
23+
includedSchemas: List[Document],
24+
scalaVersion: String,
2425
) extends CodeGenerator {
2526
import CodecCodeGen._
2627

@@ -36,6 +37,8 @@ class CodecCodeGen(
3637
override def exitMultilineJavadoc(s: String) = s == "*/"
3738
}
3839

40+
private def intersection: String = ScalaCodeGen.intersection(scalaVersion)
41+
3942
override def generateEnum(s: Document, e: EnumTypeDefinition): ListMap[File, String] = {
4043
val fqn = fullyQualifiedName(e)
4144
// Java enum can have additional parameter such as MERCURY (3.303e+23, 2.4397e6)
@@ -168,7 +171,7 @@ class CodecCodeGen(
168171
val rfs = getAllRequiredFormats(s, i).distinct filter { _ != fmt }
169172
val selfType = rfs match {
170173
case Nil => ""
171-
case fms => fms.mkString("self: ", " with ", " =>")
174+
case fms => fms.mkString("self: ", intersection, " =>")
172175
}
173176
val typeFieldName = (toCodecTypeField(i.directives) orElse toCodecTypeField(s)).getOrElse("type")
174177
val flatUnionFormat =
@@ -301,7 +304,7 @@ class CodecCodeGen(
301304
private def makeSelfType(s: Document, d: TypeDefinition): String =
302305
getRequiredFormats(s, d).distinct match {
303306
case Nil => ""
304-
case fms => fms.mkString("self: ", " with ", " =>")
307+
case fms => fms.mkString("self: ", intersection, " =>")
305308
}
306309

307310
private def genPackage(s: Document): String =

library/src/main/scala/sbt/contraband/MixedCodeGen.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class MixedCodeGen(
1616
genScalaFileName: Any => File,
1717
scalaSealProtocols: Boolean,
1818
scalaPrivateConstructor: Boolean,
19-
wrapOption: Boolean
19+
wrapOption: Boolean,
20+
scalaVersion: String,
2021
) extends CodeGenerator {
2122
val javaGen = new JavaCodeGen(javaLazy, javaOptional, instantiateJavaOptional, wrapOption)
2223
val scalaGen = new ScalaCodeGen(
@@ -27,7 +28,8 @@ class MixedCodeGen(
2728
genScalaFileName,
2829
scalaSealProtocols,
2930
scalaPrivateConstructor,
30-
wrapOption
31+
wrapOption,
32+
scalaVersion,
3133
)
3234

3335
def generate(s: Document): ListMap[File, String] =

library/src/main/scala/sbt/contraband/ScalaCodeGen.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class ScalaCodeGen(
1717
genFile: Any => File,
1818
scalaSealProtocols: Boolean,
1919
scalaPrivateConstructor: Boolean,
20-
wrapOption: Boolean
20+
wrapOption: Boolean,
21+
scalaVersion: String,
2122
) extends CodeGenerator {
2223

2324
implicit object indentationConfiguration extends IndentationConfiguration {
@@ -482,3 +483,9 @@ class ScalaCodeGen(
482483
} mkString (EOL + EOL)
483484
}
484485
}
486+
487+
object ScalaCodeGen {
488+
def intersection(scalaVersion: String): String =
489+
if (scalaVersion.startsWith("2.")) " with "
490+
else " & "
491+
}

library/src/test/scala/GraphQLCodecCodeGenSpec.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
2222
|
2323
|import _root_.sjsonnew.JsonFormat
2424
|
25-
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol with generated.ChildTypeFormats =>
25+
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats =>
2626
| implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type")
2727
|}""".stripMargin.stripSpace
2828
)
@@ -78,7 +78,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
7878
|
7979
|import _root_.sjsonnew.JsonFormat
8080
|
81-
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol with generated.ChildTypeFormats =>
81+
|trait InterfaceExampleFormats { self: sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats =>
8282
| implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type")
8383
|}""".stripMargin.stripSpace
8484
)
@@ -136,7 +136,7 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
136136
|
137137
|import _root_.sjsonnew.JsonFormat
138138
|
139-
|trait InterfaceExampleFormats { self: generated.TestItemDetailFormats with com.example.StatusFormats with sjsonnew.BasicJsonProtocol with generated.ChildTypeFormats =>
139+
|trait InterfaceExampleFormats { self: generated.TestItemDetailFormats & com.example.StatusFormats & sjsonnew.BasicJsonProtocol & generated.ChildTypeFormats =>
140140
| implicit lazy val InterfaceExampleFormat: JsonFormat[com.example.InterfaceExample] = flatUnionFormat1[com.example.InterfaceExample, com.example.ChildType]("type")
141141
|}""".stripMargin.stripSpace
142142
)
@@ -153,7 +153,16 @@ object GraphQLCodecCodeGenSpec extends BasicTestSuite with EqualLines {
153153
case other =>
154154
CodecCodeGen.formatsForType(other)
155155
}
156+
val scalaVersion = "3.5.1"
156157

157158
def mkCodecCodeGen: CodecCodeGen =
158-
new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
159+
new CodecCodeGen(
160+
codecParents = codecParents,
161+
instantiateJavaLazy = instantiateJavaLazy,
162+
javaOption = javaOption,
163+
scalaArray = scalaArray,
164+
formatsForType = formatsForType,
165+
includedSchemas = Nil,
166+
scalaVersion = scalaVersion,
167+
)
159168
}

library/src/test/scala/GraphQLMixedCodeGenSpec.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class GraphQLMixedCodeGenSpec extends AnyFlatSpec with Matchers with Inside with
1313
"generate(Record)" should "handle mixed Java-Scala inheritance" in {
1414
val Success(ast) = SchemaParser.parse(mixedExample)
1515
// println(ast)
16+
val scalaVersion = "2.13.15"
1617
val gen = new MixedCodeGen(
1718
javaLazy,
1819
CodeGen.javaOptional,
@@ -21,7 +22,8 @@ class GraphQLMixedCodeGenSpec extends AnyFlatSpec with Matchers with Inside with
2122
genFileName,
2223
scalaSealProtocols = true,
2324
scalaPrivateConstructor = true,
24-
wrapOption = true
25+
wrapOption = true,
26+
scalaVersion = scalaVersion,
2527
)
2628
val code = gen.generate(ast)
2729

library/src/test/scala/GraphQLScalaCodeGenSpec.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
386386
)
387387
}
388388

389+
def scalaVersion: String = "3.5.1"
389390
def mkScalaCodeGen: ScalaCodeGen =
390391
new ScalaCodeGen(
391392
javaLazy,
@@ -395,7 +396,8 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
395396
genFileName,
396397
scalaSealProtocols = true,
397398
scalaPrivateConstructor = true,
398-
wrapOption = true
399+
wrapOption = true,
400+
scalaVersion = scalaVersion,
399401
)
400402
val javaLazy = "com.example.Lazy"
401403
val outputFile = new File("output.scala")

library/src/test/scala/JsonCodecCodeGenSpec.scala

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
1111
val javaOption = "com.example.Option"
1212
val scalaArray = "Vector"
1313
val formatsForType: ast.Type => List[String] = CodecCodeGen.formatsForType
14+
val scalaVersion = "2.13.15"
1415

1516
override def enumerationGenerateSimple = {
16-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
17+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
1718
val enumeration = JsonParser.EnumTypeDefinition.parse(simpleEnumerationExample)
1819
val code = gen generate enumeration
1920

@@ -52,7 +53,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
5253
}
5354

5455
override def interfaceGenerateSimple = {
55-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
56+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
5657
val intf = JsonParser.InterfaceTypeDefinition.parseInterface(simpleInterfaceExample)
5758
val code = gen generate intf
5859

@@ -78,7 +79,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
7879
}
7980

8081
override def interfaceGenerateOneChild = {
81-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
82+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
8283
val intf = JsonParser.InterfaceTypeDefinition.parseInterface(oneChildInterfaceExample)
8384
val code = gen generate intf
8485

@@ -128,7 +129,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
128129
}
129130

130131
override def interfaceGenerateNested = {
131-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
132+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
132133
val intf = JsonParser.InterfaceTypeDefinition.parseInterface(nestedInterfaceExample)
133134
val code = gen generate intf
134135

@@ -149,7 +150,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
149150

150151
def interfaceGenerateMessages = {
151152
val schema = JsonParser.Document.parse(generateArgDocExample)
152-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
153+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
153154
val code = gen generate schema
154155

155156
code.head._2.unindent should equalLines("""/**
@@ -174,7 +175,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
174175
}
175176

176177
override def recordGenerateSimple = {
177-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
178+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
178179
val record = JsonParser.ObjectTypeDefinition.parse(simpleRecordExample)
179180
val code = gen generate record
180181

@@ -210,7 +211,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
210211
}
211212

212213
override def recordGrowZeroToOneField = {
213-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
214+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
214215
val record = JsonParser.ObjectTypeDefinition.parse(growableAddOneFieldExample)
215216
val code = gen generate record
216217

@@ -246,7 +247,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
246247
}
247248

248249
override def recordGrowZeroToOneToTwoFields: Unit = {
249-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
250+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
250251
val record = JsonParser.ObjectTypeDefinition.parse(growableZeroToOneToTwoFieldsExample)
251252
val code = gen generate record
252253

@@ -284,7 +285,7 @@ class JsonCodecCodeGenSpec extends GCodeGenSpec("Codec") {
284285
}
285286

286287
override def recordPrimitives: Unit = {
287-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil)
288+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, Nil, scalaVersion)
288289
val record = JsonParser.ObjectTypeDefinition.parse(primitiveTypesExample2)
289290
val code = gen generate record
290291

@@ -325,7 +326,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes
325326

326327
override def schemaGenerateTypeReferences = {
327328
val schema = JsonParser.Document.parse(primitiveTypesExample)
328-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
329+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
329330
val code = gen generate schema
330331

331332
code.head._2.unindent should equalLines("""/**
@@ -371,7 +372,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes
371372

372373
override def schemaGenerateTypeReferencesNoLazy = {
373374
val schema = JsonParser.Document.parse(primitiveTypesNoLazyExample)
374-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
375+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
375376
val code = gen generate schema
376377

377378
code.head._2.unindent should equalLines("""/**
@@ -408,14 +409,14 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes
408409

409410
override def schemaGenerateComplete = {
410411
val schema = JsonParser.Document.parse(completeExample)
411-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
412+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
412413
val code = gen generate schema
413414
code.values.mkString.unindent should equalLines(completeExampleCodeCodec.unindent)
414415
}
415416

416417
override def schemaGenerateCompletePlusIndent = {
417418
val schema = JsonParser.Document.parse(completeExample)
418-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
419+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
419420
val code = gen generate schema
420421

421422
code.values.mkString.withoutEmptyLines should equalLines(completeExampleCodeCodec.withoutEmptyLines)
@@ -431,7 +432,7 @@ implicit lazy val primitiveTypesExample2Format: JsonFormat[_root_.primitiveTypes
431432
| }
432433
| ]
433434
|}""".stripMargin)
434-
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil)
435+
val gen = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, schema :: Nil, scalaVersion)
435436
val code = gen generate schema
436437

437438
code.head._2.unindent should equalLines("""/**

library/src/test/scala/JsonScalaCodeGenSpec.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ object primitiveTypesExample2 {
466466
code.head._2.withoutEmptyLines should equalLines(completeExampleCodeScala.withoutEmptyLines)
467467
}
468468

469+
def scalaVersion: String = "2.13.15"
469470
def mkScalaCodeGen: ScalaCodeGen =
470471
new ScalaCodeGen(
471472
javaLazy,
@@ -475,7 +476,8 @@ object primitiveTypesExample2 {
475476
genFileName,
476477
scalaSealProtocols = true,
477478
scalaPrivateConstructor = true,
478-
wrapOption = true
479+
wrapOption = true,
480+
scalaVersion = scalaVersion,
479481
)
480482
val javaLazy = "com.example.Lazy"
481483
val outputFile = new File("output.scala")

plugin/src/main/scala/ContrabandPlugin.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ object ContrabandPlugin extends AutoPlugin {
111111
contrabandSjsonNewVersion := {
112112
scalaBinaryVersion.value match {
113113
case "3" =>
114-
"0.13.1"
114+
"0.14.0-M1"
115115
case _ =>
116-
"0.9.0"
116+
"0.10.1"
117117
}
118118
}
119119
)
@@ -131,6 +131,7 @@ object Generate {
131131
scalaFileNames: Any => File,
132132
scalaSealInterface: Boolean,
133133
scalaPrivateConstructor: Boolean,
134+
scalaVersion: String,
134135
wrapOption: Boolean,
135136
codecParents: List[String],
136137
instantiateJavaLazy: String => String,
@@ -158,9 +159,18 @@ object Generate {
158159
scalaFileNames,
159160
scalaSealInterface,
160161
scalaPrivateConstructor,
161-
wrapOption
162+
wrapOption,
163+
scalaVersion,
164+
)
165+
val jsonFormatsGenerator = new CodecCodeGen(
166+
codecParents = codecParents,
167+
instantiateJavaLazy = instantiateJavaLazy,
168+
javaOption = javaOption,
169+
scalaArray = scalaArray,
170+
formatsForType = formatsForType,
171+
includedSchemas = input,
172+
scalaVersion = scalaVersion,
162173
)
163-
val jsonFormatsGenerator = new CodecCodeGen(codecParents, instantiateJavaLazy, javaOption, scalaArray, formatsForType, input)
164174

165175
val datatypes =
166176
if (createDatatypes) {
@@ -234,6 +244,7 @@ object Generate {
234244
scalaFileNames,
235245
scalaSealInterface,
236246
scalaPrivateConstructor,
247+
scalaVersion,
237248
wrapOption,
238249
codecParents,
239250
instantiateJavaLazy,

0 commit comments

Comments
 (0)