-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7c802b
commit 29b37f1
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
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
76 changes: 76 additions & 0 deletions
76
core/src/main/scala-3/caliban/schema/TypeUnionDerivation.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package caliban.schema | ||
|
||
import caliban.introspection.adt.__Type | ||
|
||
import scala.quoted.* | ||
|
||
object TypeUnionDerivation { | ||
inline def derived[R, T]: Schema[R, T] = ${ typeUnionSchema[R, T] } | ||
|
||
def typeUnionSchema[R: Type, T: Type](using quotes: Quotes): Expr[Schema[R, T]] = { | ||
import quotes.reflect.* | ||
|
||
class TypeAndSchema[A](val typeRef: String, val schema: Expr[Schema[R, A]], val tpe: Type[A]) | ||
|
||
def rec[A](using tpe: Type[A]): List[TypeAndSchema[?]] = | ||
TypeRepr.of(using tpe).dealias match { | ||
case OrType(l, r) => | ||
rec(using l.asType.asInstanceOf[Type[Any]]) ++ rec(using r.asType.asInstanceOf[Type[Any]]) | ||
case otherRepr => | ||
val otherString: String = otherRepr.show | ||
val expr: TypeAndSchema[A] = | ||
Expr.summon[Schema[R, A]] match { | ||
case Some(foundSchema) => | ||
TypeAndSchema[A](otherString, foundSchema, otherRepr.asType.asInstanceOf[Type[A]]) | ||
case None => | ||
quotes.reflect.report.errorAndAbort(s"Couldn't resolve Schema[Any, $otherString]") | ||
} | ||
|
||
List(expr) | ||
} | ||
|
||
val typeAndSchemas: List[TypeAndSchema[?]] = rec[T] | ||
|
||
val schemaByTypeNameList: Expr[List[(String, Schema[R, Any])]] = Expr.ofList( | ||
typeAndSchemas.map { case (tas: TypeAndSchema[a]) => | ||
given Type[a] = tas.tpe | ||
'{ (${ Expr(tas.typeRef) }, ${ tas.schema }.asInstanceOf[Schema[R, Any]]) } | ||
} | ||
) | ||
val name = TypeRepr.of[T].show | ||
|
||
if (name.contains("|")) { | ||
report.error( | ||
s"You must explicitly add type parameter to derive Schema for a union type in order to capture the name of the type alias" | ||
) | ||
} | ||
|
||
'{ | ||
val schemaByName: Map[String, Schema[R, Any]] = ${ schemaByTypeNameList }.toMap | ||
new Schema[R, T] { | ||
|
||
def resolve(value: T): Step[R] = { | ||
var ret: Step[R] = null | ||
${ | ||
Expr.block( | ||
typeAndSchemas.map { case (tas: TypeAndSchema[a]) => | ||
given Type[a] = tas.tpe | ||
|
||
'{ if value.isInstanceOf[a] then ret = schemaByName(${ Expr(tas.typeRef) }).resolve(value) } | ||
}, | ||
'{ require(ret != null, s"no schema for ${value}") } | ||
) | ||
} | ||
ret | ||
} | ||
|
||
def toType(isInput: Boolean, isSubscription: Boolean): __Type = | ||
Types.makeUnion( | ||
Some(${ Expr(name) }), | ||
None, | ||
schemaByName.values.map(_.toType_(isInput, isSubscription)).toList | ||
) | ||
} | ||
} | ||
} | ||
} |
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