Skip to content

Commit

Permalink
Reduce codegen size for Scala 3 (#2270)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou authored Jun 5, 2024
1 parent 66f49fe commit c5897bb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.deriving.Mirror
import scala.util.NotGiven

trait CommonArgBuilderDerivation {
inline def recurseSum[P, Label, A <: Tuple](
transparent inline def recurseSum[P, Label <: Tuple, A <: Tuple](
inline values: List[(String, List[Any], ArgBuilder[Any])] = Nil
): List[(String, List[Any], ArgBuilder[Any])] =
inline erasedValue[(Label, A)] match {
Expand All @@ -36,7 +36,7 @@ trait CommonArgBuilderDerivation {
}
}

inline def recurseProduct[P, Label, A <: Tuple](
transparent inline def recurseProduct[P, Label <: Tuple, A <: Tuple](
inline values: List[(String, ArgBuilder[Any])] = Nil
): List[(String, ArgBuilder[Any])] =
inline erasedValue[(Label, A)] match {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala-3/caliban/schema/ObjectSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.annotation.threadUnsafe
import scala.reflect.ClassTag

final private class ObjectSchema[R, A](
_constructorFields: => List[(String, Schema[R, Any], Int)],
_constructorFields: => List[ProductFieldInfo[R]],
_methodFields: => List[(String, List[Any], Schema[R, ?])],
info: TypeInfo,
anns: List[Any],
Expand All @@ -19,7 +19,7 @@ final private class ObjectSchema[R, A](

@threadUnsafe
private lazy val fields = {
val fromConstructor = _constructorFields.view.map { (label, schema, index) =>
val fromConstructor = _constructorFields.view.map { case ProductFieldInfo(label, schema, index) =>
val fieldAnns = paramAnnotations.getOrElse(label, Nil)
((getName(fieldAnns, label), fieldAnns, schema), Left(index))
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala-3/caliban/schema/ProductFieldInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package caliban.schema

private final case class ProductFieldInfo[R](
name: String,
schema: Schema[R, Any],
index: Int
)
12 changes: 6 additions & 6 deletions core/src/main/scala-3/caliban/schema/SchemaDerivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait CommonSchemaDerivation {
*/
def config: DerivationConfig = DerivationConfig()

inline def recurseSum[R, P, Label, A <: Tuple](
transparent inline def recurseSum[R, P, Label <: Tuple, A <: Tuple](
inline types: List[(String, __Type, List[Any])] = Nil,
inline schemas: List[Schema[R, Any]] = Nil
): (
Expand Down Expand Up @@ -72,24 +72,24 @@ trait CommonSchemaDerivation {

}

inline def recurseProduct[R, P, Label, A <: Tuple](
inline values: List[(String, Schema[R, Any], Int)] = Nil
)(inline index: Int = 0): List[(String, Schema[R, Any], Int)] =
transparent inline def recurseProduct[R, P, Label <: Tuple, A <: Tuple](
inline values: List[ProductFieldInfo[R]] = Nil
)(inline index: Int = 0): List[ProductFieldInfo[R]] =
inline erasedValue[(Label, A)] match {
case (_: EmptyTuple, _) => values.reverse
case (_: (name *: names), _: (t *: ts)) =>
recurseProduct[R, P, names, ts] {
inline if (Macros.isFieldExcluded[P, name]) values
else
(
ProductFieldInfo[R](
constValue[name].toString,
summonInline[Schema[R, t]].asInstanceOf[Schema[R, Any]],
index
) :: values
}(index + 1)
}

inline def valueTypeSchema[R, Label, A <: Tuple]: Schema[R, Any] =
transparent inline def valueTypeSchema[R, Label <: Tuple, A <: Tuple]: Schema[R, Any] =
inline erasedValue[(Label, A)] match {
case (_: EmptyTuple, _) => error("GQLValueType case classes must have at least one field")
case (_, _: (t *: _)) => summonInline[Schema[R, t]].asInstanceOf[Schema[R, Any]]
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala-3/caliban/schema/macros/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ object Macros {
inline def implicitExists[T]: Boolean = ${ implicitExistsImpl[T] }
inline def hasAnnotation[T, Ann]: Boolean = ${ hasAnnotationImpl[T, Ann] }

inline def fieldsFromMethods[R, T]: List[(String, List[Any], Schema[R, ?])] = ${ fieldsFromMethodsImpl[R, T] }
transparent inline def fieldsFromMethods[R, T]: List[(String, List[Any], Schema[R, ?])] =
${ fieldsFromMethodsImpl[R, T] }

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

0 comments on commit c5897bb

Please sign in to comment.