Skip to content

Commit 7026d2a

Browse files
sijarsumkrzemien
authored andcommitted
initial
1 parent 249a33c commit 7026d2a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

macros/src/main/scala/com/softwaremill/macwire/internals/DependencyResolver.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ private[macwire] class DependencyResolver[C <: blackbox.Context](val c: C, debug
1919
def resolve(param: Symbol, t: Type): Tree = {
2020

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

39+
private def isOption(t: Type): Boolean = getOptionArg(t).nonEmpty
40+
41+
private def getOptionArg(t: Type): Option[Type] = t.baseType(typeOf[Option[_]].typeSymbol) match {
42+
case TypeRef(_, _, arg :: Nil) => Some(arg)
43+
case NoType => None
44+
}
45+
46+
47+
3448
/** @return all the instances of type `t` that are accessible.
3549
*/
3650
def resolveAll(t: Type): Iterable[Tree] = {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class A()
2+
class B(val oa: Option[A])
3+
4+
object TestSome {
5+
val a = new A()
6+
val b = wire[B]
7+
}
8+
9+
object TestNone {
10+
val b = wire[B]
11+
}
12+
13+
require(TestSome.b.oa.contains(TestSome.a))
14+
require(TestNone.b.oa.isEmpty)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.softwaremill.macwire
2+
3+
object WireTest extends App {
4+
class A()
5+
class B(val oa: Option[A])
6+
7+
object TestSome {
8+
val a = new A()
9+
val b = wire[B]
10+
}
11+
12+
object TestNone {
13+
val b = wire[B]
14+
}
15+
16+
require(TestSome.b.oa.contains(TestSome.a))
17+
require(TestNone.b.oa.isEmpty)
18+
}

0 commit comments

Comments
 (0)