Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macrosPekko and macrosPekkoTests #302

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ val tagging = "com.softwaremill.common" %% "tagging" % "2.3.1"
val scalatest = "org.scalatest" %% "scalatest" % "3.2.9"
val javassist = "org.javassist" % "javassist" % "3.29.2-GA"
val akkaActor = "com.typesafe.akka" %% "akka-actor" % "2.6.20"
val pekkoActor = "org.apache.pekko" %% "pekko-actor" % "1.0.1"
val javaxInject = "javax.inject" % "javax.inject" % "1"
val cats = "org.typelevel" %% "cats-core" % "2.10.0"
val catsEffect = "org.typelevel" %% "cats-effect" % "3.5.1"
Expand Down Expand Up @@ -160,6 +161,14 @@ lazy val macrosAkka = projectMatrix
.jvmPlatform(scalaVersions = scala2)
.jsPlatform(scalaVersions = scala2)

lazy val macrosPekko = projectMatrix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be included in the root aggregate project list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ I will add it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

.in(file("macrosPekko"))
.settings(commonSettings)
.settings(libraryDependencies ++= Seq(pekkoActor % "provided"))
.dependsOn(macros)
.jvmPlatform(scalaVersions = scala2)
.jsPlatform(scalaVersions = scala2)

lazy val macrosAkkaTests = projectMatrix
.in(file("macrosAkkaTests"))
.settings(
Expand All @@ -173,6 +182,19 @@ lazy val macrosAkkaTests = projectMatrix
.dependsOn(macrosAkka, testUtil)
.jvmPlatform(scalaVersions = scala2)

lazy val macrosPekkoTests = projectMatrix
.in(file("macrosPekkoTests"))
.settings(
// Needed to avoid cryptic EOFException crashes in forked tests in Travis
// example failure: https://travis-ci.org/adamw/macwire/builds/191382122
// see: https://github.com/travis-ci/travis-ci/issues/3775
javaOptions += "-Xmx1G"
)
.settings(testSettings)
.settings(libraryDependencies ++= Seq(scalatest, tagging, pekkoActor))
.dependsOn(macrosPekko, testUtil)
.jvmPlatform(scalaVersions = scala2)

lazy val macrosAutoCats = projectMatrix
.in(file("macrosAutoCats"))
.settings(commonSettings)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.softwaremill.macwire.pekkosupport

import org.apache.pekko.actor.{ActorRef, Props}
import com.softwaremill.macwire.internals.Logger

import scala.reflect.macros.blackbox

object MacwirePekkoMacros {
private val log = new Logger()

def wireProps_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[Props] =
new internals.Crimper[c.type, T](c, log).wireProps
def wireAnonymousActor_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[ActorRef] =
new internals.Crimper[c.type, T](c, log).wireAnonymousActor
def wireActor_Impl[T: c.WeakTypeTag](c: blackbox.Context)(name: c.Expr[String]): c.Expr[ActorRef] =
new internals.Crimper[c.type, T](c, log).wireActor(name)

def wirePropsWithProducer_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[Props] =
new internals.Crimper[c.type, T](c, log).wirePropsWithProducer
def wireAnonymousActorWithProducer_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[ActorRef] =
new internals.Crimper[c.type, T](c, log).wireAnonymousActorWithProducer
def wireActorWithProducer_Impl[T: c.WeakTypeTag](c: blackbox.Context)(name: c.Expr[String]): c.Expr[ActorRef] =
new internals.Crimper[c.type, T](c, log).wireActorWithProducer(name)

def wirePropsWithFactory_Impl[T: c.WeakTypeTag](c: blackbox.Context)(factory: c.Tree): c.Expr[Props] =
new internals.Crimper[c.type, T](c, log).wirePropsWithFactory(factory)
def wireAnonymousActorWithFactory_Impl[T: c.WeakTypeTag](c: blackbox.Context)(factory: c.Tree): c.Expr[ActorRef] =
new internals.Crimper[c.type, T](c, log).wireAnonymousActorWithFactory(factory)
def wireActorWithFactory_Impl[T: c.WeakTypeTag](c: blackbox.Context)(factory: c.Tree)(
name: c.Expr[String]
): c.Expr[ActorRef] = new internals.Crimper[c.type, T](c, log).wireActorWithFactory(factory)(name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.softwaremill.macwire.pekkosupport.internals

import org.apache.pekko.actor.{ActorRef, ActorRefFactory, IndirectActorProducer, Props}
import com.softwaremill.macwire.internals.{ConstructorCrimper, Logger, DependencyResolver}

import scala.reflect.macros.blackbox

private[macwire] final class Crimper[C <: blackbox.Context, T: C#WeakTypeTag](val c: C, log: Logger) {
import c.universe._
lazy val dependencyResolver = DependencyResolver.throwErrorOnResolutionFailure[c.type, Type, Tree](c, log)
lazy val cc = new ConstructorCrimper[c.type, T](c, new Logger)(implicitly[c.type#WeakTypeTag[T]])

lazy val targetType: Type = cc.targetType

lazy val args: List[Tree] = cc
.constructorArgsWithImplicitLookups(dependencyResolver)
.getOrElse(c.abort(c.enclosingPosition, s"Cannot find a public constructor for [$targetType]"))
.flatten

lazy val propsTree = q"org.apache.pekko.actor.Props(classOf[$targetType], ..$args)"

lazy val wireProps: c.Expr[Props] = log.withBlock(s"wireProps[$targetType]: at ${c.enclosingPosition}") {
log("Generated code: " + showRaw(propsTree))
c.Expr[Props](propsTree)
}

lazy val actorRefFactoryTree: Tree = log.withBlock("Looking for ActorRefFactory") {
val actorRefType = typeOf[ActorRefFactory]
val tree = dependencyResolver.resolve(actorRefType.typeSymbol, actorRefType)
log(s"Found ${showCode(tree)}")
tree
}

lazy val wireAnonymousActor: c.Expr[ActorRef] = log.withBlock(
s"Constructing ActorRef. Trying to find arguments for constructor of: [$targetType] at ${c.enclosingPosition}"
) {
val tree = q"$actorRefFactoryTree.actorOf($propsTree)"
log("Generated code: " + showRaw(tree))
c.Expr[ActorRef](tree)
}

def wireActor(name: c.Expr[String]): c.Expr[ActorRef] =
log.withBlock(s"wireActor[$targetType]: at ${c.enclosingPosition}") {
val tree = q"$actorRefFactoryTree.actorOf($propsTree, ${name.tree})"
log("Generated code: " + showRaw(tree))
c.Expr[ActorRef](tree)
}

lazy val wirePropsWithProducer: c.Expr[Props] = weakTypeOf[T] match {
case t if t <:< typeOf[IndirectActorProducer] => wireProps
case _ => c.abort(c.enclosingPosition, s"wirePropsWith does not support the type: [$targetType]")
}

lazy val wireAnonymousActorWithProducer: c.Expr[ActorRef] = weakTypeOf[T] match {
case t if t <:< typeOf[IndirectActorProducer] => wireAnonymousActor
case _ => c.abort(c.enclosingPosition, s"wireAnonymousActorWith does not support the type: [$targetType]")
}

def wireActorWithProducer(name: c.Expr[String]): c.Expr[ActorRef] = weakTypeOf[T] match {
case t if t <:< typeOf[IndirectActorProducer] => wireActor(name)
case _ => c.abort(c.enclosingPosition, s"wireActorWith does not support the type: [$targetType]")
}

def wirePropsWithFactory(factory: c.Tree): c.Expr[Props] =
log.withBlock(s"wireProps[$targetType]: at ${c.enclosingPosition}") {
import com.softwaremill.macwire.MacwireMacros._

val funTree = wireWith_impl(c)(factory)
val propsTree = q"org.apache.pekko.actor.Props($funTree)"
log("Generated code: " + showRaw(propsTree))
c.Expr[Props](propsTree)
}

def wireAnonymousActorWithFactory(factory: c.Tree): c.Expr[ActorRef] = log.withBlock(
s"Constructing ActorRef. Trying to find arguments for constructor of: [$targetType] at ${c.enclosingPosition}"
) {
import com.softwaremill.macwire.MacwireMacros._

val funTree = wireWith_impl(c)(factory)
val propsTree = q"org.apache.pekko.actor.Props($funTree)"
val tree = q"$actorRefFactoryTree.actorOf($propsTree)"
log("Generated code: " + showRaw(tree))
c.Expr[ActorRef](tree)
}

def wireActorWithFactory(factory: c.Tree)(name: c.Expr[String]): c.Expr[ActorRef] =
log.withBlock(s"wireActorWithProducer[$targetType]: at ${c.enclosingPosition}") {
import com.softwaremill.macwire.MacwireMacros._

val funTree = wireWith_impl(c)(factory)
val propsTree = q"org.apache.pekko.actor.Props($funTree)"
val tree = q"$actorRefFactoryTree.actorOf($propsTree, ${name.tree})"
log("Generated code: " + showRaw(tree))
c.Expr[ActorRef](tree)
}
}
Loading
Loading