This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mkubala/feature/moreElegantBuilderLookup
Improved custom builder lookup (solves #6)
- Loading branch information
Showing
2 changed files
with
32 additions
and
32 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
macros/src/main/scala/com/softwaremill/macmemo/BuilderResolver.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,29 @@ | ||
package com.softwaremill.macmemo | ||
|
||
import scala.language.experimental.macros | ||
|
||
object BuilderResolver { | ||
|
||
def resolve(methodFullName: String): MemoCacheBuilder = macro builderResolverMacro_impl | ||
|
||
def builderResolverMacro_impl(c: scala.reflect.macros.whitebox.Context)(methodFullName: c.Expr[String]): c.Expr[MemoCacheBuilder] = { | ||
import c.universe._ | ||
|
||
def bringDefaultBuilder: Tree = { | ||
val Literal(Constant(mfn: String)) = methodFullName.tree | ||
val msg = s"Cannot find custom memo builder for '$mfn' - default builder will be used" | ||
c.info(c.enclosingPosition, msg, false) | ||
reify { | ||
MemoCacheBuilder.guavaMemoCacheBuilder | ||
}.tree | ||
} | ||
|
||
val builderTree = c.inferImplicitValue(typeOf[MemoCacheBuilder]) match { | ||
case EmptyTree => bringDefaultBuilder | ||
case foundBuilderTree => foundBuilderTree | ||
} | ||
|
||
c.Expr[MemoCacheBuilder](Block(List(), builderTree)) | ||
} | ||
|
||
} |
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