Skip to content

Commit

Permalink
Remove logic for value classes since it's not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr committed May 3, 2021
1 parent b0b85ce commit 04ded6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
9 changes: 2 additions & 7 deletions core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ trait SchemaDerivation[R] {
}
case m: Mirror.ProductOf[A] =>
lazy val fields = recurse[m.MirroredElemLabels, m.MirroredElemTypes]()
lazy val isValueClass = Macros.isValueClass[A]
lazy val isObject = Macros.isObject[A]
lazy val info = Macros.typeInfo[A]
lazy val annotations = Macros.annotations[A]
lazy val paramAnnotations = Macros.paramAnnotations[A].toMap
new Schema[R, A] {
def toType(isInput: Boolean, isSubscription: Boolean): __Type =
if (isValueClass && fields.nonEmpty) fields.head._4.toType_(isInput, isSubscription)
else if (isInput)
if (isInput)
makeInputObject(
Some(annotations.collectFirst { case GQLInputName(suffix) => suffix }
.getOrElse(customizeInputTypeName(getName(annotations, info)))),
Expand Down Expand Up @@ -157,10 +155,7 @@ trait SchemaDerivation[R] {

def resolve(value: A): Step[R] =
if (isObject) PureStep(EnumValue(getName(annotations, info)))
else if (isValueClass && fields.nonEmpty) {
val (_, _, _, schema, index) = fields.head
schema.resolve(value.asInstanceOf[Product].productElement(index))
} else {
else {
val fieldsBuilder = Map.newBuilder[String, Step[R]]
fields.foreach { case (label, _, _, schema, index) =>
val fieldAnnotations = paramAnnotations.getOrElse(label, Nil)
Expand Down
36 changes: 15 additions & 21 deletions core/src/main/scala-3/caliban/schema/macros/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ private[caliban] object Macros {

inline def annotations[T]: List[Any] = ${annotationsImpl[T]}
inline def paramAnnotations[T]: List[(String, List[Any])] = ${paramAnnotationsImpl[T]}
inline def isValueClass[T]: Boolean = ${isValueClassImpl[T]}
inline def typeInfo[T]: TypeInfo = ${typeInfoImpl[T]}
inline def isObject[T]: Boolean = ${isObjectImpl[T]}

Expand All @@ -35,30 +34,25 @@ private[caliban] object Macros {
}
}

def isValueClassImpl[T: Type](using qctx: Quotes): Expr[Boolean] = {
import qctx.reflect.*
Expr(TypeRepr.of[T].baseClasses.contains(Symbol.classSymbol("scala.AnyVal")))
}

def typeInfoImpl[T: Type](using qctx: Quotes): Expr[TypeInfo] = {
import qctx.reflect._
def typeInfoImpl[T: Type](using qctx: Quotes): Expr[TypeInfo] = {
import qctx.reflect._

def normalizedName(s: Symbol): String = if s.flags.is(Flags.Module) then s.name.stripSuffix("$") else s.name
def name(tpe: TypeRepr) : Expr[String] = Expr(normalizedName(tpe.typeSymbol))
def normalizedName(s: Symbol): String = if s.flags.is(Flags.Module) then s.name.stripSuffix("$") else s.name
def name(tpe: TypeRepr) : Expr[String] = Expr(normalizedName(tpe.typeSymbol))

def owner(tpe: TypeRepr): Expr[String] =
if tpe.typeSymbol.maybeOwner.isNoSymbol then Expr("<no owner>")
else if (tpe.typeSymbol.owner == defn.EmptyPackageClass) Expr("")
else Expr(tpe.typeSymbol.owner.name)
def owner(tpe: TypeRepr): Expr[String] =
if tpe.typeSymbol.maybeOwner.isNoSymbol then Expr("<no owner>")
else if (tpe.typeSymbol.owner == defn.EmptyPackageClass) Expr("")
else Expr(tpe.typeSymbol.owner.name)

def typeInfo(tpe: TypeRepr): Expr[TypeInfo] = tpe match
case AppliedType(tpe, args) =>
'{TypeInfo(${owner(tpe)}, ${name(tpe)}, ${Expr.ofList(args.map(typeInfo))})}
case _ =>
'{TypeInfo(${owner(tpe)}, ${name(tpe)}, Nil)}
def typeInfo(tpe: TypeRepr): Expr[TypeInfo] = tpe match
case AppliedType(tpe, args) =>
'{TypeInfo(${owner(tpe)}, ${name(tpe)}, ${Expr.ofList(args.map(typeInfo))})}
case _ =>
'{TypeInfo(${owner(tpe)}, ${name(tpe)}, Nil)}

typeInfo(TypeRepr.of[T])
}
typeInfo(TypeRepr.of[T])
}

def isObjectImpl[T](using qctx: Quotes, tpe: Type[T]): Expr[Boolean] = {
import qctx.reflect.*
Expand Down

0 comments on commit 04ded6d

Please sign in to comment.