-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaMapUtilsTestMacro.test.scala
More file actions
58 lines (48 loc) · 1.67 KB
/
JavaMapUtilsTestMacro.test.scala
File metadata and controls
58 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.encalmo.utils
import scala.quoted.*
import JavaMapUtils.*
object JavaMapUtilsTestMacro {
inline def testMaybeVisitJavaMap[K, V](inline value: java.util.Map[K, V]): String = {
${ testMaybeVisitJavaMapImpl[K, V]('{ value }) }
}
def testMaybeVisitJavaMapImpl[K: Type, V: Type](valueExpr: Expr[java.util.Map[K, V]])(using Quotes): Expr[String] = {
given cache: StatementsCache = new StatementsCache
testMaybeVisitJavaMap2Impl[K, V](valueExpr)
}
def testMaybeVisitJavaMap2Impl[K: Type, V: Type](
valueExpr: Expr[java.util.Map[K, V]]
)(using cache: StatementsCache): Expr[String] = {
given cache.quotes.type = cache.quotes
import cache.quotes.reflect.*
val bufferRef = cache.getValueRefOfExpr("buffer", '{ collection.mutable.ListBuffer.empty[String] })
TypeRepr.of[java.util.Map[K, V]] match {
case TypeReprIsJavaMap(keyTpe, valueTpe) =>
cache.put(
buildMapLoop(
TypeNameUtils.valueNameOf(keyTpe),
keyTpe,
valueTpe,
valueExpr.asTerm,
functionOnEntry = { (key, value) =>
bufferRef.methodCall(
"append",
List(
StringUtils.concat(
StringUtils.applyToString(key),
Literal(StringConstant(" -> ")),
StringUtils.applyToString(value)
)
)
)
}
)
)
}
cache.put {
bufferRef.methodCall("mkString", List(Literal(StringConstant(", "))))
}
val result = cache.asTerm
// report.warning(result.show(using Printer.TreeCode))
result.asExprOf[String]
}
}