From 214e0ec06ea87da3dba82193d9dae03b5fb07003 Mon Sep 17 00:00:00 2001 From: Brian Holt Date: Wed, 30 Oct 2024 12:50:57 -0500 Subject: [PATCH] use Liftable to clean up macro implementation and allow consulScheme to be package-private --- build.sbt | 1 + .../smithy4s/ConsulDiscoverablePlatform.scala | 17 ++++++++++++++--- .../dwolla/consul/smithy4s/UriFromService.scala | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index 2496d56..fe75f80 100644 --- a/build.sbt +++ b/build.sbt @@ -117,6 +117,7 @@ lazy val `smithy4s-consul-middleware` = crossProject(JSPlatform, JVMPlatform) libraryDependencies ++= Seq( "org.http4s" %%% "http4s-client" % http4sVersion, "com.disneystreaming.smithy4s" %%% "smithy4s-core" % smithy4sVersion.value, + "org.typelevel" %% "scalac-compat-annotation" % "0.1.4", ), ) .jsSettings( diff --git a/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/ConsulDiscoverablePlatform.scala b/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/ConsulDiscoverablePlatform.scala index 6d79035..8be3b0d 100644 --- a/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/ConsulDiscoverablePlatform.scala +++ b/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/ConsulDiscoverablePlatform.scala @@ -2,7 +2,8 @@ package com.dwolla.consul.smithy4s import cats.syntax.all._ import com.dwolla.consul.smithy._ -import org.http4s.Uri.Host +import org.http4s.Uri.{Host, Scheme} +import org.typelevel.scalaccompat.annotation.nowarn212 import smithy4s.Hints import scala.reflect.macros.whitebox @@ -14,9 +15,19 @@ trait ConsulDiscoverablePlatform { } object ConsulDiscoverableMacros { + @nowarn212("msg=local val (liftableScheme|liftableHost) in method makeInstance is never used") def makeInstance[Alg[_[_]]](c: whitebox.Context): c.Expr[ConsulDiscoverable[Alg]] = { import c.universe.{Try => _, _} + implicit val liftableScheme: Liftable[Scheme] = Liftable { scheme: Scheme => + q"""_root_.org.http4s.Uri.Scheme.unsafeFromString(${scheme.value})""" + } + implicit val liftableHost: Liftable[Host] = Liftable { host: Host => + q"""_root_.org.http4s.Uri.Host.unsafeFromString(${host.value})""" + } + + val consulScheme = DiscoveryMacros.consulScheme.some + def findHintsInTree(tpe: Tree): Either[String, Hints] = Try { tpe.collect { @@ -44,9 +55,9 @@ object ConsulDiscoverableMacros { c.Expr[ConsulDiscoverable[Alg]]( q""" new _root_.com.dwolla.consul.smithy4s.ConsulDiscoverable[$tpe] { - override def host: _root_.org.http4s.Uri.Host = _root_.org.http4s.Uri.Host.unsafeFromString(${host.value}) + override def host: _root_.org.http4s.Uri.Host = $host override def uriAuthority: _root_.org.http4s.Uri.Authority = _root_.org.http4s.Uri.Authority(host = host) - override def uri: _root_.org.http4s.Uri = _root_.org.http4s.Uri(scheme = _root_.scala.Option(_root_.com.dwolla.consul.smithy4s.DiscoveryMacros.consulScheme), authority = _root_.scala.Option(uriAuthority)) + override def uri: _root_.org.http4s.Uri = _root_.org.http4s.Uri(scheme = $consulScheme, authority = _root_.scala.Option(uriAuthority)) } """) diff --git a/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/UriFromService.scala b/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/UriFromService.scala index 2c5cfe7..4823efb 100644 --- a/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/UriFromService.scala +++ b/smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/UriFromService.scala @@ -10,7 +10,7 @@ import org.http4s.syntax.all._ import scala.reflect.macros.blackbox object DiscoveryMacros { - val consulScheme: Scheme = scheme"consul" + private[smithy4s] val consulScheme: Scheme = scheme"consul" def makeUri[Alg[_[_, _, _, _, _]]](c: blackbox.Context) (service: c.Expr[smithy4s.Service[Alg]]): c.Expr[Uri] = {