Skip to content

Commit

Permalink
Fix Xpath to get correct equals method
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Shashev authored and danslapman committed Mar 27, 2024
1 parent f0acf03 commit 3e966c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ import ru.tinkoff.tcb.bson.BsonEncoder
import ru.tinkoff.tcb.bson.BsonKeyDecoder
import ru.tinkoff.tcb.bson.BsonKeyEncoder

final case class Xpath(raw: String, toXPathExpr: XPathExpression) {
/*
* toXPathExpr inside extra braces is important. It's exclude this value
* from hash and equals method. XPathExpression doesn't have correct equals
* method, it is comparable only by reference.
*/
final case class Xpath(raw: String)(val toXPathExpr: XPathExpression) {
override def toString: String = raw
}

object Xpath {
def fromString(pathStr: String): Try[Xpath] =
Try(xPathFactory.newXPath().compile(pathStr)).map(Xpath(pathStr, _))
Try(xPathFactory.newXPath().compile(pathStr)).map(Xpath(pathStr))

def unapply(str: String): Option[Xpath] =
fromString(str).toOption
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.tinkoff.tcb.xpath

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class XpathSpec extends AnyFunSuite with Matchers {

test("Xpath equals") {
val xp1 = Xpath.fromString("//user/@id")
val xp2 = Xpath.fromString("//user/@id")

xp1 shouldBe xp2
}

}

0 comments on commit 3e966c2

Please sign in to comment.