Skip to content

Instantly share code, notes, and snippets.

@deusaquilus
Created April 12, 2021 17:29
Show Gist options
  • Save deusaquilus/dba1a3b912600398b76d4a8a3bb0e850 to your computer and use it in GitHub Desktop.
Save deusaquilus/dba1a3b912600398b76d4a8a3bb0e850 to your computer and use it in GitHub Desktop.
Getting printouts when writing macro-level decoders
// It's a bit complex because if you throw an error you usually don't get the result
// i.e. the place where the decoder is summoned returns the error because no decoder can be summoned from there
// [error] -- Error: /Users/aleiof/git/dotty_test/quill-sql/src/test/scala/io/getquill/QueryTest.scala:31:26
// [error] 31 | val result = ctx.run(q)
// [error] | ^^^^^^^^^^
// [error] | Decoder could not be summoned during query execution
// if you use report.warning and return something you usually get it
def decode[T: Type, ResultRow: Type](index: Expr[Int], resultRow: Expr[ResultRow])(using Quotes): Expr[T] = {
import quotes.reflect._
// if there is a decoder for the term, just return the term
Expr.summon[Mirror.Of[T]] match
case Some(ev) =>
// Otherwise, recursively summon fields
ev match {
case '{ $m: Mirror.SumOf[T] { type MirroredElemLabels = elementLabels; type MirroredElemTypes = elementTypes }} =>
// First make sure there is a column resolver, otherwise we can't look up fields by name which
// means we can't get specific fields which means we can't decode co-products
Expr.summon[GenericColumnResolver[ResultRow]] match
case None => report.warning(s"Need column resolver for in order to be able to decode a coproduct but none exists for ${Format.TypeOf[ResultRow]}")
case _ =>
// [warn] -- Warning: /Users/aleiof/git/dotty_test/quill-sql/src/test/scala/io/getquill/QueryTest.scala:31:26
// [warn] 31 | val result = ctx.run(q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment