-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
not-looping-implicit.scala
45 lines (37 loc) · 1.64 KB
/
not-looping-implicit.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//> using options -Xfatal-warnings -deprecation -feature
import scala.deriving.Mirror
import scala.compiletime._
trait Schema[T]
object Schema {
implicit val stringSchema: Schema[String] = new Schema[String] {}
implicit def listSchema[A](implicit ev: Schema[A]): Schema[List[A]] = new Schema[List[A]] {}
implicit def mapSchema[A, B](implicit evA: Schema[A], evB: Schema[B]): Schema[Map[A, B]] =
new Schema[Map[A, B]] {}
inline def recurse[Label, A <: Tuple](index: Int = 0): List[(String, Schema[Any], Int)] =
inline erasedValue[(Label, A)] match {
case (_: (name *: names), _: (t *: ts)) =>
val label = constValue[name].toString
val builder = summonInline[Schema[t]].asInstanceOf[Schema[Any]]
(label, builder, index) :: recurse[names, ts](index + 1)
case (_: EmptyTuple, _) => Nil
}
inline def derived[A]: Schema[A] =
inline summonInline[Mirror.Of[A]] match {
case m: Mirror.SumOf[A] =>
lazy val members = recurse[m.MirroredElemLabels, m.MirroredElemTypes]()
???
case m: Mirror.ProductOf[A] =>
lazy val fields = recurse[m.MirroredElemLabels, m.MirroredElemTypes]()
???
}
inline given gen[A]: Schema[A] = derived[A]
}
sealed trait InputValue
object InputValue {
case class ListValue(values: List[InputValue]) extends InputValue
case class ObjectValue(fields: Map[String, InputValue]) extends InputValue
case class VariableValue(name: String) extends InputValue
}
@main def Test =
implicit lazy val inputValueSchema: Schema[InputValue] = Schema.gen
println(summon[Schema[InputValue]])