Skip to content

Add regression test #11034

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
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
5 changes: 5 additions & 0 deletions tests/run-macros/simple-interpreter.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Some(BYTE)
Some(INT)
Some(OPTION[INT])
Some(OPTION[OPTION[INT]])
Some(MAP[INT,INT])
51 changes: 51 additions & 0 deletions tests/run-macros/simple-interpreter/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

trait Schema[T]
trait SchemaType[T] extends Schema[T]

object Schema {
given intSchema: SchemaType[Int] = new SchemaType { override def toString = "INT" }
given byteSchema: SchemaType[Byte] = new SchemaType { override def toString = "BYTE" }
given optionSchema[T](using t: Schema[T]): Schema[Option[T]] = new Schema { override def toString = s"OPTION[$t]" }
given mapSchema[K, V](using k: Schema[K], v: Schema[V]): Schema[Map[K, V]] = new Schema { override def toString = s"MAP[$k,$v]" }
}

/** Intereter for Schema.
* Assumes that all instances of schema come from `object Schema`
*/
object SchemaInterpreter {
import scala.quoted._

def interpretSchemaType[T: Type](schema: Expr[SchemaType[T]])(using Quotes): Option[SchemaType[T]] =
schema match {
case '{ Schema.intSchema } => Some(Schema.intSchema.asInstanceOf[SchemaType[T]])
case '{ Schema.byteSchema } => Some(Schema.byteSchema.asInstanceOf[SchemaType[T]])
case _ => None
}

def interpretSchema[T: Type](schemaExpr: Expr[Schema[T]])(using Quotes): Option[Schema[T]] =
schemaExpr match {
case '{ $typeSchemaExpr: SchemaType[T] } =>
interpretSchemaType(typeSchemaExpr)
case '{ Schema.optionSchema[t](using $tSchemaExpr) } =>
for tSchema <- interpretSchema(tSchemaExpr)
yield Schema.optionSchema(using tSchema).asInstanceOf[Schema[T]]
case '{ Schema.mapSchema[k, v](using $kSchemaExpr, $vSchemaExpr) } =>
for kSchema <- interpretSchema(kSchemaExpr)
vSchema <- interpretSchema(vSchemaExpr)
yield Schema.mapSchema(using kSchema, vSchema).asInstanceOf[Schema[T]]
case _ =>
None // could also hangle with `quotes.reflect.{error, throwError}`
}
}

object Macro {
import scala.quoted._

inline def useSchema[T](using inline schema: Schema[T]): String =
${ useSchemaExpr[T]('schema) }

private def useSchemaExpr[T: Type](schemaExpr: Expr[Schema[T]])(using Quotes): Expr[String] = {
val schemaOpt: Option[Schema[T]] = SchemaInterpreter.interpretSchema(schemaExpr)
Expr(schemaOpt.toString)
}
}
9 changes: 9 additions & 0 deletions tests/run-macros/simple-interpreter/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Macro.useSchema

@main def Test: Unit = {
println(useSchema[Byte])
println(useSchema[Int])
println(useSchema[Option[Int]])
println(useSchema[Option[Option[Int]]])
println(useSchema[Map[Int, Int]])
}