Skip to content

Commit

Permalink
Refactor Document#equals method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill5k committed Feb 9, 2024
1 parent 71af55d commit d2f9995
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private[circe] object CirceJsonMapper extends JsonMapper[Json] {
def toBsonValue: BsonValue =
jNumber.getClass.getName match {
case "io.circe.JsonDouble" | "io.circe.JsonFloat" => BsonValue.double(jNumber.toDouble)
case "io.circe.JsonLong" => BsonValue.long(jNumber.toLong.get)
case "io.circe.JsonLong" => jNumber.toInt.map(BsonValue.int).orElse(jNumber.toLong.map(BsonValue.long)).get
case _ => BsonValue.bigDecimal(jNumber.toBigDecimal.get)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CirceJsonMapperSpec extends AnyWordSpec with Matchers {
)

CirceJsonMapper.fromBson(bson) mustBe Right(json)
CirceJsonMapper.toBson(json).toString mustBe bson.toString
CirceJsonMapper.toBson(json) mustBe bson
}

"handle numeric conversions" in {
Expand All @@ -110,7 +110,7 @@ class CirceJsonMapperSpec extends AnyWordSpec with Matchers {
)

CirceJsonMapper.fromBson(bson) mustBe Right(json)
CirceJsonMapper.toBson(json).toString mustBe bson.toString
CirceJsonMapper.toBson(json) mustBe bson
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions modules/kernel/src/main/scala/mongo4cats/bson/Document.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ final private class ListMapDocument(
override def hashCode(): Int = fields.hashCode()
override def equals(other: Any): Boolean =
Option(other) match {
case Some(doc: Document) => doc.toSet == fields.toSet
case _ => false
case Some(doc: Document) if doc.keys == this.keys =>
doc.toMap.forall { case (k, v) =>
(v, fields.get(k)) match {
case (BsonValue.BBinary(bin1), Some(BsonValue.BBinary(bin2))) => bin1.sameElements(bin2)
case (bv1, Some(bv2)) => bv1 == bv2
case _ => false
}
}
case _ => false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ZioJsonMapperSpec extends AnyWordSpec with Matchers {
)

ZioJsonMapper.fromBson(bson) mustBe Right(json)
ZioJsonMapper.toBson(json).toString mustBe bson.toString
ZioJsonMapper.toBson(json) mustBe bson
}

"handle numeric conversions" in {
Expand All @@ -123,7 +123,7 @@ class ZioJsonMapperSpec extends AnyWordSpec with Matchers {
)

ZioJsonMapper.fromBson(bson) mustBe Right(json)
ZioJsonMapper.toBson(json).toString mustBe bson.toString
ZioJsonMapper.toBson(json) mustBe bson
}
}
}
Expand Down

0 comments on commit d2f9995

Please sign in to comment.