Created
April 12, 2021 17:29
-
-
Save deusaquilus/dba1a3b912600398b76d4a8a3bb0e850 to your computer and use it in GitHub Desktop.
Getting printouts when writing macro-level decoders
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
// 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