-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
3ee123a
commit 32bb7ac
Showing
3 changed files
with
134 additions
and
1 deletion.
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
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
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.* | ||
|
||
val typeName = TypeRepr.of[T].show | ||
|
||
if (typeName.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" | ||
) | ||
} | ||
|
||
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 annotations: Expr[List[Any]] = magnolia1.Macro.anns[T](summon, quotes) | ||
'{ | ||
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(DerivationUtils.getName(${ annotations }, ${ Expr(typeName) })), | ||
DerivationUtils.getDescription(${ annotations }), | ||
schemaByName.values.map(_.toType_(isInput, isSubscription)).toList, | ||
directives = Option(DerivationUtils.getDirectives(${ annotations })).filter(_.nonEmpty) | ||
) | ||
} | ||
} | ||
} | ||
} |
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