-
Notifications
You must be signed in to change notification settings - Fork 76
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions
32
macrosPekko/src/main/scala/com/softwaremill/macwire/pekkosupport/MacwirePekkoMacros.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,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) | ||
} |
96 changes: 96 additions & 0 deletions
96
macrosPekko/src/main/scala/com/softwaremill/macwire/pekkosupport/internals/Crimper.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,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) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️ I will add it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.