Skip to content

Instantly share code, notes, and snippets.

@sujeet100
Last active November 14, 2017 04:16
Show Gist options
  • Save sujeet100/74346963bb1281619a08f9960a46e24c to your computer and use it in GitHub Desktop.
Save sujeet100/74346963bb1281619a08f9960a46e24c to your computer and use it in GitHub Desktop.
Quill: Tail recursive liftQuery macro
trait TupleQuery {
this: io.getquill.context.Context[_, _] =>
def liftTuples[T1: Encoder, T2: Encoder](l: List[(T1, T2)]): Quoted[Query[(T1, T2)]] =
liftTuples(l, (t: (T1, T2)) => infix"(${lift(t._1)}, ${lift(t._2)})", null)
private def liftTuples[T, U](l: List[T], f: T => Quoted[Any], acc: Quoted[Query[U]]): Quoted[Query[U]] = {
l match {
case Nil => infix"".as[Query[U]]
case head :: Nil => infix"${f(head)}, $acc".as[Query[U]]
case head :: tail if acc == null => liftTuples(tail, f, infix"${f(head)}".as[Query[U]])
case head :: tail => liftTuples(tail, f, infix"${f(head)}, $acc".as[Query[U]])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment