Skip to content

Commit a7c6a56

Browse files
authored
Merge pull request #208 from xuwei-k/copy-method-zero-or-one
update `copy` method implementation if field size is 0 or 1
2 parents d5b73b5 + a07aabd commit a7c6a56

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,24 @@ class ScalaCodeGen(
492492

493493
private def genCopy(r: ObjectTypeDefinition, intfLang: String) = {
494494
val allFields = r.fields filter { _.arguments.isEmpty }
495-
def genParam(f: FieldDefinition) = s"${bq(f.name)}: ${genRealTpe(f.fieldType, isParam = true, intfLang)} = ${bq(f.name)}"
496-
val params = allFields map genParam mkString ", "
497-
val constructorCall = allFields map (f => bq(f.name)) mkString ", "
498-
s"""private def copy($params): ${r.name} = {
499-
| new ${r.name}($constructorCall)
500-
|}""".stripMargin
495+
allFields.size match {
496+
case 0 =>
497+
""
498+
case size =>
499+
def genParam(f: FieldDefinition): String = {
500+
if (size == 1) {
501+
// https://github.com/sbt/contraband/issues/149
502+
s"${bq(f.name)}: ${genRealTpe(f.fieldType, isParam = true, intfLang)}"
503+
} else {
504+
s"${bq(f.name)}: ${genRealTpe(f.fieldType, isParam = true, intfLang)} = ${bq(f.name)}"
505+
}
506+
}
507+
val params = allFields map genParam mkString ", "
508+
val constructorCall = allFields map (f => bq(f.name)) mkString ", "
509+
s"""private def copy($params): ${r.name} = {
510+
| new ${r.name}($constructorCall)
511+
|}""".stripMargin
512+
}
501513
}
502514

503515
private def genWith(r: ObjectTypeDefinition, intfLang: String) = {

library/src/test/scala/GraphQLScalaCodeGenSpec.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
5252
| override def toString: String = {
5353
| "TypeExample(" + field + ")"
5454
| }
55-
| private def copy(field: Option[java.net.URL] = field): TypeExample = {
55+
| private def copy(field: Option[java.net.URL]): TypeExample = {
5656
| new TypeExample(field)
5757
| }
5858
| def withField(field: Option[java.net.URL]): TypeExample = {
@@ -92,7 +92,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
9292
| override def toString: String = {
9393
| "TypeExample(" + field + ")"
9494
| }
95-
| private def copy(field: Option[java.net.URL] = field): TypeExample = {
95+
| private def copy(field: Option[java.net.URL]): TypeExample = {
9696
| new TypeExample(field)
9797
| }
9898
| def withField(field: Option[java.net.URL]): TypeExample = {
@@ -129,7 +129,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
129129
| override def toString: String = {
130130
| "TypeExample(" + field + ")"
131131
| }
132-
| private def copy(field: scala.collection.immutable.Map[String, String] = field): TypeExample = {
132+
| private def copy(field: scala.collection.immutable.Map[String, String]): TypeExample = {
133133
| new TypeExample(field)
134134
| }
135135
| def withField(field: scala.collection.immutable.Map[String, String]): TypeExample = {
@@ -163,7 +163,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
163163
| override def toString: String = {
164164
| "Growable(" + field + ")"
165165
| }
166-
| private def copy(field: Option[Int] = field): Growable = {
166+
| private def copy(field: Option[Int]): Growable = {
167167
| new Growable(field)
168168
| }
169169
| def withField(field: Option[Int]): Growable = {
@@ -249,7 +249,7 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
249249
| override def toString: String = {
250250
| "ModifierExample(" + field + ")"
251251
| }
252-
| private def copy(field: Int = field): ModifierExample = {
252+
| private def copy(field: Int): ModifierExample = {
253253
| new ModifierExample(field)
254254
| }
255255
| def withField(field: Int): ModifierExample = {

library/src/test/scala/JsonScalaCodeGenSpec.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
145145
| override def toString: String = {
146146
| "ChildRecord()"
147147
| }
148-
| private def copy(): ChildRecord = {
149-
| new ChildRecord()
150-
| }
151148
|}
152149
|object ChildRecord {
153150
| def apply(): ChildRecord = new ChildRecord()
@@ -204,7 +201,7 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
204201
| override def toString: String = {
205202
| "simpleRecordExample(" + field + ")"
206203
| }
207-
| private def copy(field: java.net.URL = field): simpleRecordExample = {
204+
| private def copy(field: java.net.URL): simpleRecordExample = {
208205
| new simpleRecordExample(field)
209206
| }
210207
| def withField(field: java.net.URL): simpleRecordExample = {
@@ -234,7 +231,7 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
234231
| override def toString: String = {
235232
| "growableAddOneField(" + field + ")"
236233
| }
237-
| private def copy(field: Int = field): growableAddOneField = {
234+
| private def copy(field: Int): growableAddOneField = {
238235
| new growableAddOneField(field)
239236
| }
240237
| def withField(field: Int): growableAddOneField = {
@@ -342,7 +339,7 @@ object primitiveTypesExample2 {
342339
| override def toString: String = {
343340
| "modifierExample(" + field + ")"
344341
| }
345-
| private def copy(field: Int = field): modifierExample = {
342+
| private def copy(field: Int): modifierExample = {
346343
| new modifierExample(field)
347344
| }
348345
| def withField(field: Int): modifierExample = {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
scalaVersion := "3.7.3"
2+
3+
// https://github.com/sbt/contraband/issues/149
4+
scalacOptions += "-Wunused:all"
5+
scalacOptions += "-Werror"
26
name := "example"
37
enablePlugins(ContrabandPlugin, JsonCodecPlugin)

0 commit comments

Comments
 (0)