Skip to content

Commit

Permalink
Merge pull request #153 from jtjeferreira/reference_equaliy
Browse files Browse the repository at this point in the history
implement reference equality for scala
  • Loading branch information
eed3si9n committed Nov 21, 2020
2 parents 50aefce + cb1739b commit 9bd5a80
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 66 deletions.
4 changes: 2 additions & 2 deletions docs/02-code-generation.md
Expand Up @@ -36,10 +36,10 @@ package com.example
final class Person private (
val name: String,
val age: Option[Int]) extends Serializable {
override def equals(o: Any): Boolean = o match {
override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
case x: Person => (this.name == x.name) && (this.age == x.age)
case _ => false
}
})
override def hashCode: Int = {
37 * (37 * (17 + name.##) + age.##)
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/scala/sbt/contraband/ScalaCodeGen.scala
Expand Up @@ -235,10 +235,10 @@ class ScalaCodeGen(javaLazy: String, javaOptional: String, instantiateJavaOption
}).mkString(" && "))
}

s"""override def equals(o: Any): Boolean = o match {
s"""override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case $x: ${cl.name} => $comparisonCode
| case _ => false
|}""".stripMargin
|})""".stripMargin
}

private def genHashCode(cl: RecordLikeDefinition, intfLang: String) = {
Expand Down
4 changes: 2 additions & 2 deletions library/src/test/scala/GraphQLMixedCodeGenSpec.scala
Expand Up @@ -83,10 +83,10 @@ final class SimpleGreeting private (
message: String,
s: java.util.Optional[String]) extends com.example.Greeting(message, s) with Serializable {
private def this(message: String) = this(message, java.util.Optional.ofNullable[String]("1"))
override def equals(o: Any): Boolean = o match {
override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
case x: SimpleGreeting => this.message.equals(x.message) && this.s.equals(x.s)
case _ => false
}
})
override def hashCode: Int = {
37 * (37 * (37 * (17 + "com.example.SimpleGreeting".##) + message.hashCode()) + s.hashCode())
}
Expand Down
40 changes: 20 additions & 20 deletions library/src/test/scala/GraphQLScalaCodeGenSpec.scala
Expand Up @@ -42,10 +42,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|final class TypeExample private (
|val field: Option[java.net.URL]) extends Serializable {
| // Some extra code
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: TypeExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.TypeExample".##) + field.##)
| }
Expand Down Expand Up @@ -82,10 +82,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|*/
|final class TypeExample private (
|val field: Option[java.net.URL]) extends Intf1 with Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: TypeExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.TypeExample".##) + field.##)
| }
Expand Down Expand Up @@ -119,10 +119,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|/** Example of a type */
|final class TypeExample private (
|val field: scala.collection.immutable.Map[String, String]) extends Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: TypeExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.TypeExample".##) + field.##)
| }
Expand Down Expand Up @@ -153,10 +153,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|final class Growable private (
| val field: Option[Int]) extends Serializable {
| private def this() = this(Option(0))
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: Growable => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.Growable".##) + field.##)
| }
Expand Down Expand Up @@ -195,10 +195,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
| val y: Vector[Int]) extends Serializable {
| private def this() = this(None, Vector())
| private def this(x: Option[Int]) = this(x, Vector())
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: Foo => (this.x == x.x) && (this.y == x.y)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (37 * (17 + "com.example.Foo".##) + x.##) + y.##)
| }
Expand Down Expand Up @@ -239,10 +239,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
"""package com.example
|sealed class ModifierExample private (
|val field: Int) extends Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: ModifierExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.ModifierExample".##) + field.##)
| }
Expand Down Expand Up @@ -273,10 +273,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|sealed abstract class InterfaceExample(
|val field: Option[Int]) extends Serializable {
| // Some extra code
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: InterfaceExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.InterfaceExample".##) + field.##)
| }
Expand All @@ -289,10 +289,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|final class ChildType private (
| val name: Option[String],
| field: Option[Int]) extends com.example.InterfaceExample(field) with Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: ChildType => (this.name == x.name) && (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (37 * (17 + "com.example.ChildType".##) + name.##) + field.##)
| }
Expand Down Expand Up @@ -339,10 +339,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
| * @param arg1 This argument is not important, so it gets single line doc.
| */
| def messageExample(arg0: => Vector[Int], arg1: Option[Boolean]): Vector[Int]
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: IntfExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.IntfExample".##) + field.##)
| }
Expand All @@ -367,10 +367,10 @@ object GraphQLScalaCodeGenSpec extends BasicTestSuite with EqualLines {
|sealed abstract class IntfExample(
| val field: Option[Int]) extends Interface1 with Interface2 with Serializable {
| // Some extra code...
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: IntfExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "com.example.IntfExample".##) + field.##)
| }
Expand Down
56 changes: 28 additions & 28 deletions library/src/test/scala/JsonScalaCodeGenSpec.scala
Expand Up @@ -31,10 +31,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|sealed abstract class simpleInterfaceExample(
| val field: type) extends Interface1 with Interface2 with Serializable {
| // Some extra code...
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: simpleInterfaceExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "simpleInterfaceExample".##) + field.##)
| }
Expand All @@ -57,10 +57,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
"""/** example of interface */
|sealed abstract class oneChildInterfaceExample(
| val field: Int) extends Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: oneChildInterfaceExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "oneChildInterfaceExample".##) + field.##)
| }
Expand All @@ -74,10 +74,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|final class childRecord private (
| field: Int,
| val x: Int) extends oneChildInterfaceExample(field) with Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: childRecord => (this.field == x.field) && (this.x == x.x)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (37 * (17 + "childRecord".##) + field.##) + x.##)
| }
Expand Down Expand Up @@ -108,10 +108,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
code.head._2.unindent should equalLines (
"""/** example of nested protocols */
|sealed abstract class nestedProtocolExample() extends Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case _: nestedProtocolExample => true
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (17 + "nestedProtocolExample".##)
| }
Expand All @@ -123,10 +123,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|}
|
|sealed abstract class nestedProtocol() extends nestedProtocolExample() with Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case _: nestedProtocol => true
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (17 + "nestedProtocol".##)
| }
Expand All @@ -139,10 +139,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|}
|
|final class ChildRecord private () extends nestedProtocol() with Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case _: ChildRecord => true
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (17 + "ChildRecord".##)
| }
Expand Down Expand Up @@ -174,10 +174,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
| * @param arg1 This argument is not important, so it gets single line doc.
| */
| def messageExample(arg0: => Vector[Int], arg1: Boolean): Vector[Int]
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: generateArgDocExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "generateArgDocExample".##) + field.##)
| }
Expand All @@ -200,10 +200,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|final class simpleRecordExample private (
|val field: java.net.URL) extends Serializable {
| // Some extra code...
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: simpleRecordExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "simpleRecordExample".##) + field.##)
| }
Expand Down Expand Up @@ -231,10 +231,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
"""final class growableAddOneField private (
| val field: Int) extends Serializable {
| private def this() = this(0)
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: growableAddOneField => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "growableAddOneField".##) + field.##)
| }
Expand Down Expand Up @@ -265,10 +265,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
| val y: Vector[Int]) extends Serializable {
| private def this() = this(Option(0), Vector(0))
| private def this(x: Option[Int]) = this(x, Vector(0))
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: Foo => (this.x == x.x) && (this.y == x.y)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (37 * (17 + "Foo".##) + x.##) + y.##)
| }
Expand Down Expand Up @@ -309,10 +309,10 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
override def equals(o: Any): Boolean = o match {
override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
case x: primitiveTypesExample2 => (this.smallBoolean == x.smallBoolean) && (this.bigBoolean == x.bigBoolean)
case _ => false
}
})
override def hashCode: Int = {
37 * (37 * (37 * (17 + "primitiveTypesExample2".##) + smallBoolean.##) + bigBoolean.##)
}
Expand Down Expand Up @@ -342,10 +342,10 @@ object primitiveTypesExample2 {
code.head._2.unindent should equalLines (
"""sealed class modifierExample private (
|val field: Int) extends Serializable {
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: modifierExample => (this.field == x.field)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (17 + "modifierExample".##) + field.##)
| }
Expand Down Expand Up @@ -381,10 +381,10 @@ object primitiveTypesExample2 {
| lazy val lazyInteger: Int = _lazyInteger
| lazy val lazyArrayInteger: Vector[Int] = _lazyArrayInteger
| lazy val lazyOptionInteger: Option[Int] = _lazyOptionInteger
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case _: primitiveTypesExample => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
| case _ => false
| }
| })
| override def hashCode: Int = {
| super.hashCode // Avoid evaluating lazy members in hashCode to avoid circularity.
| }
Expand Down Expand Up @@ -439,10 +439,10 @@ object primitiveTypesExample2 {
| val arrayInteger: Vector[Int]) extends Serializable {
|
|
| override def equals(o: Any): Boolean = o match {
| override def equals(o: Any): Boolean = this.eq(o.asInstanceOf[AnyRef]) || (o match {
| case x: primitiveTypesNoLazyExample => (this.simpleInteger == x.simpleInteger) && (this.arrayInteger == x.arrayInteger)
| case _ => false
| }
| })
| override def hashCode: Int = {
| 37 * (37 * (37 * (17 + "primitiveTypesNoLazyExample".##) + simpleInteger.##) + arrayInteger.##)
| }
Expand Down

0 comments on commit 9bd5a80

Please sign in to comment.