-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ConsulDiscoverable typeclass and Scala 2 macro implementation
- Loading branch information
Showing
6 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 36 additions & 1 deletion
37
...hy4s-tests/src/test/scala-2/com/dwolla/consul/smithy4s/ConsulMiddlewareSpecPlatform.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
package com.dwolla.consul.smithy4s | ||
|
||
import cats.syntax.all._ | ||
import com.dwolla.test.HelloService | ||
import munit.FunSuite | ||
import org.http4s.Uri | ||
import org.http4s.Uri.Host | ||
import scala.reflect.runtime.currentMirror | ||
import scala.reflect.runtime.universe._ | ||
import scala.tools.reflect.ToolBox | ||
|
||
trait ConsulMiddlewareSpecPlatform { | ||
trait ConsulMiddlewareSpecPlatform { self: FunSuite => | ||
val serviceUri: Uri = UriFromService(HelloService) | ||
private val serviceHost: Host = HostFromService(HelloService) | ||
private val serviceUriAuthority: Uri.Authority = UriAuthorityFromService(HelloService) | ||
|
||
private val toolbox = currentMirror.mkToolBox() | ||
|
||
private def typecheck(input: String): Either[Throwable, Tree] = | ||
for { | ||
parsed <- Either.catchNonFatal(toolbox.parse(input)) | ||
typechecked <- Either.catchNonFatal(toolbox.typecheck(parsed)) | ||
} yield typechecked | ||
|
||
test("ConsulDiscoverable typeclass macro constructs a working instance of the typeclass") { | ||
assertEquals(ConsulDiscoverable[HelloService].host, serviceHost) | ||
assertEquals(ConsulDiscoverable[HelloService].uriAuthority, serviceUriAuthority) | ||
assertEquals(ConsulDiscoverable[HelloService].uri, serviceUri) | ||
} | ||
|
||
test("ConsulDiscoverable typeclass macro returns no instance when the type parameter isn't a Smithy Service") { | ||
val typechecked = typecheck("""com.dwolla.consul.smithy4s.ConsulDiscoverable[com.dwolla.consul.smithy4s.NotSmithy]""") | ||
|
||
assertEquals( | ||
typechecked.leftMap(_.getMessage), | ||
"reflective typecheck has failed: Instances are only available for Smithy4s Services annotated with @discoverable".asLeft | ||
) | ||
} | ||
} | ||
|
||
trait NotSmithy[F[_]] { | ||
def foo(int: Int): F[Unit] | ||
} |
66 changes: 66 additions & 0 deletions
66
smithy4s/src/main/scala-2/com/dwolla/consul/smithy4s/ConsulDiscoverablePlatform.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.dwolla.consul.smithy4s | ||
|
||
import cats.syntax.all._ | ||
import com.dwolla.consul.smithy._ | ||
import org.http4s.Uri.Host | ||
import smithy4s.Hints | ||
|
||
import scala.reflect.macros.whitebox | ||
import scala.util.Try | ||
|
||
trait ConsulDiscoverablePlatform { | ||
implicit def derivedInstance[Alg[_[_]]]: ConsulDiscoverable[Alg] = | ||
macro ConsulDiscoverableMacros.makeInstance[Alg] | ||
} | ||
|
||
object ConsulDiscoverableMacros { | ||
def makeInstance[Alg[_[_]]](c: whitebox.Context): c.Expr[ConsulDiscoverable[Alg]] = { | ||
import c.universe.{Try => _, _} | ||
|
||
def findHintsInTree(tpe: Tree): Either[String, Hints] = | ||
Try { | ||
tpe.collect { | ||
case x: TypTree => | ||
c.eval(c.Expr[Hints](q"${x.symbol.companion}.hints")) | ||
} | ||
.headOption | ||
.toRight(s"could not find hints for $tpe") | ||
} | ||
.toEither | ||
.leftMap(_.toString ++ s" (is $tpe a Smithy4s Service?)") | ||
.flatten | ||
|
||
def getDiscoverableFromHints(tpe: Tree): Hints => Either[String, Discoverable] = | ||
_.get(Discoverable.tagInstance) | ||
.toRight(s"could not find Discoverable hint for $tpe") | ||
|
||
val getHostFromDiscoverable: PartialFunction[Discoverable, Either[String, Host]] = { | ||
case Discoverable(ServiceName(serviceName)) => | ||
Host.fromString(serviceName) | ||
.leftMap(_.message) | ||
} | ||
|
||
def hostToConsulDiscoverableExpr(tpe: Tree): Host => c.Expr[ConsulDiscoverable[Alg]] = host => | ||
c.Expr[ConsulDiscoverable[Alg]]( | ||
q""" | ||
new com.dwolla.consul.smithy4s.ConsulDiscoverable[$tpe] { | ||
override def host: org.http4s.Uri.Host = org.http4s.Uri.Host.unsafeFromString(${host.value}) | ||
override def uriAuthority: org.http4s.Uri.Authority = org.http4s.Uri.Authority(host = host) | ||
override def uri: org.http4s.Uri = org.http4s.Uri(scheme = com.dwolla.consul.smithy4s.DiscoveryMacros.consulScheme.some, authority = uriAuthority.some) | ||
} | ||
""") | ||
|
||
c.macroApplication match { | ||
case TypeApply(_, List(tpe)) if tpe.symbol.companion != NoSymbol => | ||
val maybeExpr = findHintsInTree(tpe) | ||
.flatMap(getDiscoverableFromHints(tpe)) | ||
.flatMap(getHostFromDiscoverable) | ||
.map(hostToConsulDiscoverableExpr(tpe)) | ||
|
||
maybeExpr.fold(c.abort(c.enclosingPosition, _), identity) | ||
case TypeApply(_, List(tpe)) if tpe.symbol.companion == NoSymbol => | ||
c.abort(c.enclosingPosition, s"$tpe is not a Smithy4s Service") | ||
case other => c.abort(c.enclosingPosition, s"found $other, which is not a Smithy4s Service") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
smithy4s/src/main/scala-3/com/dwolla/consul/smithy4s/ConsulDiscoverablePlatform.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.dwolla.consul.smithy4s | ||
|
||
trait ConsulDiscoverablePlatform |
17 changes: 17 additions & 0 deletions
17
smithy4s/src/main/scala/com/dwolla/consul/smithy4s/ConsulDiscoverable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.dwolla.consul.smithy4s | ||
|
||
import org.http4s.Uri | ||
import org.http4s.Uri.Host | ||
|
||
import scala.annotation.implicitNotFound | ||
|
||
@implicitNotFound("Instances are only available for Smithy4s Services annotated with @discoverable") | ||
trait ConsulDiscoverable[Alg[_[_]]] { | ||
def host: Host | ||
def uriAuthority: Uri.Authority | ||
def uri: Uri | ||
} | ||
|
||
object ConsulDiscoverable extends ConsulDiscoverablePlatform { | ||
def apply[Alg[_[_]] : ConsulDiscoverable]: ConsulDiscoverable[Alg] = implicitly | ||
} |