Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
sijarsu authored and mkrzemien committed Jun 7, 2020
1 parent 249a33c commit 7026d2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ private[macwire] class DependencyResolver[C <: blackbox.Context](val c: C, debug
def resolve(param: Symbol, t: Type): Tree = {

eligibleValues.findInFirstScope(t).toList match {
case Nil if isOption(t) =>
getOptionArg(t).flatMap(u => eligibleValues.findInFirstScope(u).toList.headOption) match { //FIXME: handle multiple values (tests, tests...)
case Some(argTree) => q"Some($argTree)"
case None => q"None"
}
case Nil => c.abort(c.enclosingPosition, s"Cannot find a value of type: [$t]")
case value :: Nil =>
val forwardValues = eligibleValues.findInScope(t, LocalForward)
Expand All @@ -31,6 +36,15 @@ private[macwire] class DependencyResolver[C <: blackbox.Context](val c: C, debug
}
}

private def isOption(t: Type): Boolean = getOptionArg(t).nonEmpty

private def getOptionArg(t: Type): Option[Type] = t.baseType(typeOf[Option[_]].typeSymbol) match {
case TypeRef(_, _, arg :: Nil) => Some(arg)
case NoType => None
}



/** @return all the instances of type `t` that are accessible.
*/
def resolveAll(t: Type): Iterable[Tree] = {
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/resources/test-cases/_t142.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class A()
class B(val oa: Option[A])

object TestSome {
val a = new A()
val b = wire[B]
}

object TestNone {
val b = wire[B]
}

require(TestSome.b.oa.contains(TestSome.a))
require(TestNone.b.oa.isEmpty)
18 changes: 18 additions & 0 deletions tests/src/test/scala/com/softwaremill/macwire/WireTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.softwaremill.macwire

object WireTest extends App {
class A()
class B(val oa: Option[A])

object TestSome {
val a = new A()
val b = wire[B]
}

object TestNone {
val b = wire[B]
}

require(TestSome.b.oa.contains(TestSome.a))
require(TestNone.b.oa.isEmpty)
}

0 comments on commit 7026d2a

Please sign in to comment.