Skip to content

Commit

Permalink
Use custom macro for getting annotations (#1950)
Browse files Browse the repository at this point in the history
* Use custom macro for getting annotations

* Format
  • Loading branch information
ghostdogpr authored Oct 20, 2023
1 parent 6cba759 commit 3c4ab1f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import caliban.Value.*
import caliban.schema.Annotations.GQLDefault
import caliban.schema.Annotations.GQLName
import caliban.schema.macros.Macros
import magnolia1.Macro as MagnoliaMacro

import scala.deriving.Mirror
import scala.compiletime.*
Expand All @@ -22,7 +21,7 @@ trait CommonArgBuilderDerivation {
recurse[P, names, ts](
(
constValue[name].toString,
MagnoliaMacro.anns[t], {
Macros.annotations[t], {
if (Macros.isEnumField[P, t])
if (!Macros.implicitExists[ArgBuilder[t]]) derived[t]
else summonInline[ArgBuilder[t]]
Expand All @@ -43,7 +42,7 @@ trait CommonArgBuilderDerivation {
case m: Mirror.ProductOf[A] =>
makeProductArgBuilder(
recurse[A, m.MirroredElemLabels, m.MirroredElemTypes](),
MagnoliaMacro.paramAnns[A].to(Map)
Macros.paramAnnotations[A].to(Map)
)(m.fromProduct)
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trait CommonSchemaDerivation {
else
(
constValue[name].toString,
MagnoliaMacro.anns[t], {
Macros.annotations[t], {
if (Macros.isEnumField[P, t])
if (!Macros.implicitExists[Schema[R, t]]) derived[R, t]
else summonInline[Schema[R, t]]
Expand All @@ -65,15 +65,15 @@ trait CommonSchemaDerivation {
makeSumSchema[R, A](
recurse[R, A, m.MirroredElemLabels, m.MirroredElemTypes]()(),
MagnoliaMacro.typeInfo[A],
MagnoliaMacro.anns[A]
Macros.annotations[A]
)(m.ordinal)

case m: Mirror.ProductOf[A] =>
makeProductSchema[R, A](
recurse[R, A, m.MirroredElemLabels, m.MirroredElemTypes]()(),
MagnoliaMacro.typeInfo[A],
MagnoliaMacro.anns[A],
MagnoliaMacro.paramAnns[A].toMap
Macros.annotations[A],
Macros.paramAnnotations[A].toMap
)
}

Expand Down
31 changes: 28 additions & 3 deletions core/src/main/scala-3/caliban/schema/macros/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,34 @@ import scala.quoted.*
export magnolia1.TypeInfo

object Macros {
inline def isFieldExcluded[P, T]: Boolean = ${ isFieldExcludedImpl[P, T] }
inline def isEnumField[P, T]: Boolean = ${ isEnumFieldImpl[P, T] }
inline def implicitExists[T]: Boolean = ${ implicitExistsImpl[T] }
inline def annotations[T]: List[Any] = ${ annotationsImpl[T] }
inline def paramAnnotations[T]: List[(String, List[Any])] = ${ paramAnnotationsImpl[T] }
inline def isFieldExcluded[P, T]: Boolean = ${ isFieldExcludedImpl[P, T] }
inline def isEnumField[P, T]: Boolean = ${ isEnumFieldImpl[P, T] }
inline def implicitExists[T]: Boolean = ${ implicitExistsImpl[T] }

private def annotationsImpl[T: Type](using qctx: Quotes): Expr[List[Any]] = {
import qctx.reflect.*
val tpe = TypeRepr.of[T]
Expr.ofList {
tpe.typeSymbol.annotations.filter { a =>
a.tpe.typeSymbol.maybeOwner.isNoSymbol || (a.tpe.typeSymbol.owner.fullName != "scala.annotation.internal" && a.tpe.typeSymbol.owner.fullName != "jdk.internal")
}.map(_.asExpr.asInstanceOf[Expr[Any]])
}
}

private def paramAnnotationsImpl[T: Type](using qctx: Quotes): Expr[List[(String, List[Any])]] = {
import qctx.reflect.*
val tpe = TypeRepr.of[T]
Expr.ofList {
tpe.typeSymbol.primaryConstructor.paramSymss.flatten.map { field =>
Expr(field.name) -> field.annotations.filter { a =>
a.tpe.typeSymbol.maybeOwner.isNoSymbol ||
(a.tpe.typeSymbol.owner.fullName != "scala.annotation.internal" && a.tpe.typeSymbol.owner.fullName != "jdk.internal")
}.map(_.asExpr.asInstanceOf[Expr[Any]])
}.filter(_._2.nonEmpty).map((name, anns) => Expr.ofTuple(name, Expr.ofList(anns)))
}
}

/**
* Tests whether type argument [[FieldT]] in [[Parent]] is annotated with [[GQLExcluded]]
Expand Down

0 comments on commit 3c4ab1f

Please sign in to comment.