-
Notifications
You must be signed in to change notification settings - Fork 25
/
TableMacro.scala
125 lines (108 loc) · 4.24 KB
/
TableMacro.scala
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package scalasql.query
import scalasql.core.Sc
import scala.language.experimental.macros
object TableMacros {
type Expr[T] = scalasql.core.Expr[T]
def applyImpl[V[_[_]]](
c: scala.reflect.macros.blackbox.Context
)(implicit caseClassType: c.WeakTypeTag[V[Any]]): c.Expr[Table.Metadata[V]] = {
import c.universe._
val tableRef = TermName(c.freshName("tableRef"))
val constructor = weakTypeOf[V[Any]].members.find(_.isConstructor).head
val constructorParameters = constructor.info.paramLists.head
def isTypeParamType(param: Symbol) = {
param.info.typeSymbol.toString != caseClassType.tpe.typeParams.head.toString
}
val columnParams = for (param <- constructorParameters) yield {
val name = param.name
if (isTypeParamType(param)) {
q"implicitly[scalasql.Table.ImplicitMetadata[${param.info.typeSymbol}]].value.vExpr($tableRef, dialect)"
} else {
q"""
new _root_.scalasql.Column[${param.info.typeArgs.head}](
$tableRef,
_root_.scalasql.Table.columnNameOverride($tableRef.value)(${name.toString})
)
"""
}
}
def subParam(paramInfo: Type, tpe: Type) = {
paramInfo.substituteTypes(
List(constructor.info.resultType.typeArgs.head.typeSymbol),
List(
tpe
.asInstanceOf[ExistentialType]
.underlying
.asInstanceOf[TypeRef]
.sym
.info
)
)
}
val queryables = for (param <- constructorParameters) yield {
val tpe = subParam(param.info, typeOf[Sc[_]])
val tpe2 = subParam(param.info, typeOf[TableMacros.Expr[_]])
q"implicitly[_root_.scalasql.Queryable.Row[$tpe2, $tpe]]"
}
val constructParams = for ((param, i) <- constructorParameters.zipWithIndex) yield {
val tpe = subParam(param.info, typeOf[Sc[_]])
val tpe2 = subParam(param.info, typeOf[TableMacros.Expr[_]])
q"queryable[$tpe2, $tpe]($i).construct(args): _root_.scalasql.Sc[$tpe]"
}
val deconstructParams = for ((param, i) <- constructorParameters.zipWithIndex) yield {
val tpe = subParam(param.info, typeOf[Sc[_]])
val tpe2 = subParam(param.info, typeOf[TableMacros.Expr[_]])
q"queryable[$tpe2, $tpe]($i).deconstruct(r.${TermName(param.name.toString)})"
}
val flattenLists = for (param <- constructorParameters) yield {
if (isTypeParamType(param)) {
q"implicitly[scalasql.Table.ImplicitMetadata[${param.info.typeSymbol}]].value.walkLabels0()"
} else {
val name = param.name
q"_root_.scala.List(${name.toString})"
}
}
val flattenExprs = for ((param, i) <- constructorParameters.zipWithIndex) yield {
val tpe = subParam(param.info, typeOf[Sc[_]])
val tpe2 = subParam(param.info, typeOf[TableMacros.Expr[_]])
q"queryable[$tpe2, $tpe]($i).walkExprs(table.${TermName(param.name.toString)})"
}
import compat._
val typeRef = caseClassType.tpe.resultType.asInstanceOf[TypeRef]
val exprRef = TypeRef(
pre = typeRef.pre,
sym = typeRef.sym,
args = weakTypeOf[V[TableMacros.Expr]].typeArgs
)
val idRef = TypeRef(
pre = typeRef.pre,
sym = typeRef.sym,
args = weakTypeOf[V[Sc]].typeArgs
)
c.Expr[Table.Metadata[V]](q"""{
new _root_.scalasql.query.Table.Metadata(
(dialect, n) => {
import dialect._;
n match{ case ..${queryables.zipWithIndex.map { case (q, i) => cq"$i => $q" }} }
},
() => ${flattenLists.reduceLeft((l, r) => q"$l ++ $r")},
(walkLabels0, dialect, queryable) => {
import dialect._
new _root_.scalasql.query.Table.Internal.TableQueryable(
walkLabels0,
(table: $exprRef) => ${flattenExprs.reduceLeft((l, r) => q"$l ++ $r")},
construct0 = (args: _root_.scalasql.Queryable.ResultSetIterator) => new $caseClassType(..$constructParams),
deconstruct0 = (r: $idRef) => new $caseClassType(..$deconstructParams)
)
},
($tableRef: _root_.scalasql.query.TableRef, dialect, queryable) => {
import dialect._
new $caseClassType(..$columnParams)
}
)
}""")
}
}
trait TableMacros {
implicit def initTableMetadata[V[_[_]]]: Table.Metadata[V] = macro TableMacros.applyImpl[V]
}