Skip to content

Fix #7853: Add regression test #7925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/pos-macros/i7853/JsonEncoder_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import scala.deriving._
import scala.compiletime.erasedValue

trait JsonEncoder[T] {
def encode(elem: T): String
}

object JsonEncoder {
import scala.compiletime.{erasedValue, summonFrom}
import compiletime._
import scala.deriving._

inline def encodeElem[T](elem: T): String = summonFrom {
case encoder: JsonEncoder[T] => encoder.encode(elem)
}

inline def encodeElems[Elems <: Tuple](idx: Int)(value: Any): List[String] =
inline erasedValue[Elems] match {
case _: (elem *: elems1) =>
encodeElem[elem](productElement[elem](value, idx)) :: encodeElems[elems1](idx + 1)(value)
case _ => Nil
}

inline def derived[T](implicit ev: Mirror.Of[T]): JsonEncoder[T] = new JsonEncoder[T] {
def encode(value: T): String =
inline ev match {
case m: Mirror.SumOf[T] =>
"not supporting this case yet"
case m: Mirror.ProductOf[T] =>
val elems = encodeElems[m.MirroredElemTypes](0)(value)
val labels = value.asInstanceOf[Product].productElementNames
val keyValues = labels.zip(elems).map((k, v) => s"$k: $v")
"{" + (keyValues).mkString(", ") + "}"
case other =>
throw new RuntimeException("mirror was an invalid value: " + other)
}
}

given listEncoder[T]: (encoder: JsonEncoder[T]) => JsonEncoder[List[T]] {
def encode(list: List[T]) = s"[${ list.map(v => encoder.encode(v)).mkString(", ") }]"
}

given intEncoder: JsonEncoder[Int] {
def encode(value: Int) = value + ""
}

given stringEncoder: JsonEncoder[String] {
def encode(value: String) = value
}
}
24 changes: 24 additions & 0 deletions tests/pos-macros/i7853/SummonJsonEncoderTest_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.deriving._
import scala.quoted._
import scala.quoted.matching._
import scala.compiletime.{erasedValue, summonFrom}
import JsonEncoder.{given, _}

object SummonJsonEncoderTest {

inline def encodeAndMessAroundType[T](value: =>T): String = ${ encodeAndMessAroundTypeImpl('value) }

def encodeAndMessAroundTypeImpl[T: Type](value: Expr[T])(given qctx: QuoteContext): Expr[String] = {
import qctx.tasty._

val mirrorExpr = summonExpr[Mirror.Of[T]] match {
case Some(mirror) => mirror
}

'{
given JsonEncoder[T] = JsonEncoder.derived($mirrorExpr)
val encoder = summon[JsonEncoder[T]]
encoder.encode($value)
}
}
}
11 changes: 11 additions & 0 deletions tests/pos-macros/i7853/Test_3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SummonJsonEncoderTest._

case class PersonSimple(name:String, age:Int)

object Test {
val stuff = PersonSimple("Joe", 123)

def main(args: Array[String]):Unit = {
println(SummonJsonEncoderTest.encodeAndMessAroundType(stuff) )
}
}