Skip to content

Commit 3935a4c

Browse files
sijarsumkrzemien
authored andcommitted
cleanup
1 parent 7026d2a commit 3935a4c

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,54 @@ private[macwire] class DependencyResolver[C <: blackbox.Context](val c: C, debug
1818
*/
1919
def resolve(param: Symbol, t: Type): Tree = {
2020

21-
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-
}
27-
case Nil => c.abort(c.enclosingPosition, s"Cannot find a value of type: [$t]")
21+
def resolve(t: Type, handler: (Type, List[Tree]) => Tree): Tree = {
22+
val trees: List[Tree] = eligibleValues.findInFirstScope(t).toList match {
23+
case Nil if isOption(t) => List(resolve(getOptionArg(t).get, optionHandler))
24+
case ts => ts
25+
}
26+
handler(t, trees)
27+
}
28+
29+
def basicHandler(t: Type, trees: List[Tree]): Tree = trees match {
30+
case Nil => noValueError(t)
2831
case value :: Nil =>
29-
val forwardValues = eligibleValues.findInScope(t, LocalForward)
30-
if (forwardValues.nonEmpty) {
31-
c.warning(c.enclosingPosition, s"Found [$value] for parameter [${param.name}], " +
32-
s"but a forward reference [${forwardValues.mkString(", ")}] was also eligible")
33-
}
32+
forwardValuesWarn(value)
3433
value
35-
case values => c.abort(c.enclosingPosition, s"Found multiple values of type [$t]: [$values]")
34+
case values => multipleValuesError(t, values)
3635
}
37-
}
3836

39-
private def isOption(t: Type): Boolean = getOptionArg(t).nonEmpty
37+
def optionHandler(t: Type, trees: List[Tree]): Tree = trees match {
38+
case Nil => q"None"
39+
case value :: Nil if value.equalsStructure(q"None") => q"None"
40+
case value :: Nil =>
41+
forwardValuesWarn(value)
42+
q"Some($value)"
43+
case values => multipleValuesError(t, values)
44+
}
4045

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-
}
46+
def noValueError(t: Type): Nothing =
47+
c.abort(c.enclosingPosition, s"Cannot find a value of type: [$t]")
48+
49+
def multipleValuesError(t: Type, values: List[Tree]): Nothing =
50+
c.abort(c.enclosingPosition, s"Found multiple values of type [$t]: [$values]")
4551

46-
52+
def forwardValuesWarn(value: Tree): Unit = {
53+
val forwardValues = eligibleValues.findInScope(t, LocalForward)
54+
if (forwardValues.nonEmpty) {
55+
c.warning(c.enclosingPosition, s"Found [$value] for parameter [${param.name}], " +
56+
s"but a forward reference [${forwardValues.mkString(", ")}] was also eligible")
57+
}
58+
}
59+
60+
def isOption(t: Type): Boolean = getOptionArg(t).nonEmpty
61+
62+
def getOptionArg(t: Type): Option[Type] = t.baseType(typeOf[Option[_]].typeSymbol) match {
63+
case TypeRef(_, _, arg :: Nil) => Some(arg)
64+
case NoType => None
65+
}
66+
67+
resolve(t, basicHandler)
68+
}
4769

4870
/** @return all the instances of type `t` that are accessible.
4971
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class A()
2+
class B(val oa: Option[Option[A]])
3+
4+
object TestSome {
5+
val a = Some(new A())
6+
val b = wire[B]
7+
}
8+
9+
object TestNestedSome {
10+
val a = new A()
11+
val b = wire[B]
12+
}
13+
14+
object TestNone {
15+
val b = wire[B]
16+
}
17+
18+
require(TestSome.b.oa.contains(TestSome.a))
19+
require(TestNestedSome.b.oa.get.contains(TestNestedSome.a))
20+
require(TestNone.b.oa.isEmpty)

tests/src/test/scala/com/softwaremill/macwire/WireTest.scala

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)