Skip to content

Commit

Permalink
use Liftable to clean up macro implementation and allow consulScheme …
Browse files Browse the repository at this point in the history
…to be package-private
  • Loading branch information
bpholt committed Oct 30, 2024
1 parent 17e3598 commit 214e0ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
}
""")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down

0 comments on commit 214e0ec

Please sign in to comment.