From fdf98d9b0e9b36dd04b3bf0180b1a9f0f5f4fa17 Mon Sep 17 00:00:00 2001 From: SimY4 Date: Wed, 20 Mar 2024 16:51:46 +1000 Subject: [PATCH] [scala] Implicitly search for NamespaceContext in xpath macro. --- .../com/github/simy4/xpath/scala/xpath/XPathLiteral.scala | 8 +++++--- .../com/github/simy4/xpath/scala/xpath/XPathLiteral.scala | 7 ++++--- .../github/simy4/xpath/scala/xpath/XPathLiteralSpec.scala | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/xpath-to-xml-scala/src/main/scala-2-/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala b/xpath-to-xml-scala/src/main/scala-2-/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala index a64ac9a8..d85ccc44 100644 --- a/xpath-to-xml-scala/src/main/scala-2-/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala +++ b/xpath-to-xml-scala/src/main/scala-2-/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala @@ -19,6 +19,7 @@ import com.github.simy4.xpath.expr.Expr import com.github.simy4.xpath.parser.XPathParser import reflect.macros.blackbox +import javax.xml.namespace.NamespaceContext import javax.xml.xpath.XPathExpressionException final class XPathLiteral(private val sc: StringContext) extends AnyVal { @@ -28,13 +29,14 @@ final class XPathLiteral(private val sc: StringContext) extends AnyVal { @SuppressWarnings(Array("org.wartremover.warts.Null")) object XPathLiteral { def xpathImpl(c: blackbox.Context)(args: c.Expr[Any]*): c.Expr[Expr] = { - import c.universe._ + import c.universe.* c.prefix.tree match { case Apply(_, List(Apply(_, List(lit @ Literal(Constant(str: String)))))) => try { - val _ = new XPathParser(null).parse(str) - reify(new XPathParser(null).parse(c.Expr[String](lit).splice)) + val namespaceContext = c.inferImplicitValue(weakTypeOf[NamespaceContext], silent = true).orElse(q"null") + val _ = new XPathParser(null).parse(str) + reify(new XPathParser(c.Expr[NamespaceContext](namespaceContext).splice).parse(c.Expr[String](lit).splice)) } catch { case xpee: XPathExpressionException => c.abort(c.enclosingPosition, s"Illegal XPath expression: ${xpee.getMessage}") diff --git a/xpath-to-xml-scala/src/main/scala-3+/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala b/xpath-to-xml-scala/src/main/scala-3+/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala index f7a23855..ab635341 100644 --- a/xpath-to-xml-scala/src/main/scala-3+/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala +++ b/xpath-to-xml-scala/src/main/scala-3+/com/github/simy4/xpath/scala/xpath/XPathLiteral.scala @@ -18,9 +18,9 @@ package com.github.simy4.xpath.scala.xpath import com.github.simy4.xpath.expr.Expr as JExpr import com.github.simy4.xpath.parser.XPathParser +import javax.xml.namespace.NamespaceContext import javax.xml.xpath.XPathExpressionException - -import scala.quoted.{ quotes, Expr, Exprs, Quotes, Varargs } +import scala.quoted.{ Expr, Exprs, Quotes, Varargs, quotes } object XPathLiteral: @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.Null", "org.wartremover.warts.IterableOps")) @@ -30,8 +30,9 @@ object XPathLiteral: case '{ StringContext(${ Varargs(Exprs(args)) }*) } if args.size == 1 => try { val const = args.head + val namespaceContext = Expr.summon[NamespaceContext].getOrElse('{null}) val _ = new XPathParser(null).parse(const) - '{ new XPathParser(null).parse(${ Expr(const) }) } + '{ new XPathParser(${ namespaceContext }).parse(${ Expr(const) }) } } catch { case xpee: XPathExpressionException => report.errorAndAbort(s"Illegal XPath expression: ${xpee.getMessage}", sc) diff --git a/xpath-to-xml-scala/src/test/scala/com/github/simy4/xpath/scala/xpath/XPathLiteralSpec.scala b/xpath-to-xml-scala/src/test/scala/com/github/simy4/xpath/scala/xpath/XPathLiteralSpec.scala index 06f1b8bb..b7b2d048 100644 --- a/xpath-to-xml-scala/src/test/scala/com/github/simy4/xpath/scala/xpath/XPathLiteralSpec.scala +++ b/xpath-to-xml-scala/src/test/scala/com/github/simy4/xpath/scala/xpath/XPathLiteralSpec.scala @@ -22,7 +22,7 @@ import parser.XPathParser @SuppressWarnings(Array("org.wartremover.warts.Null")) class XPathLiteralSpec extends AnyFunSpec with Matchers { - import scala.implicits._ + import scala.implicits.* describe("xpathLiteral") { it("should compile")(assertCompiles("""val expr = xpath"ancestor::author[parent::book][1]""""))