From d8d3da75ee71fc465c3f49979927a5b5eeefebcb Mon Sep 17 00:00:00 2001 From: XiNiHa Date: Wed, 24 Apr 2024 21:47:31 +0900 Subject: [PATCH] Wrap enableSemanticNonNull option with a config object --- .../caliban/schema/SchemaDerivation.scala | 17 ++++++++++---- .../caliban/schema/SchemaDerivation.scala | 23 ++++++++++++------- .../schema/SemanticNonNullSchemaSpec.scala | 2 +- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala b/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala index eabecc03e..b7526f83a 100644 --- a/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala +++ b/core/src/main/scala-2/caliban/schema/SchemaDerivation.scala @@ -11,13 +11,20 @@ import scala.language.experimental.macros trait CommonSchemaDerivation[R] { + case class DerivationConfig( + /** + * Whether to enable the `SemanticNonNull` feature on derivation. + * It is currently disabled by default since it is not yet stable. + */ + enableSemanticNonNull: Boolean = false + ) + /** - * Enables the `SemanticNonNull` feature on derivation. - * It is currently disabled by default since it is not yet stable. + * Returns a configuration object that can be used to customize the derivation behavior. * - * Override this method and return `true` to enable the feature. + * Override this method to customize the configuration. */ - def enableSemanticNonNull: Boolean = false + def config: DerivationConfig = DerivationConfig() /** * Default naming logic for input types. @@ -105,7 +112,7 @@ trait CommonSchemaDerivation[R] { p.annotations.collectFirst { case GQLDeprecated(reason) => reason }, Option( p.annotations.collect { case GQLDirective(dir) => dir }.toList ++ { - if (enableSemanticNonNull && !isNullable && p.typeclass.canFail) + if (config.enableSemanticNonNull && !isNullable && p.typeclass.canFail) Some(SchemaUtils.SemanticNonNull) else None } diff --git a/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala b/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala index 8efb28385..0f71b78d3 100644 --- a/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala +++ b/core/src/main/scala-3/caliban/schema/SchemaDerivation.scala @@ -23,13 +23,20 @@ object PrintDerived { trait CommonSchemaDerivation { export DerivationUtils.customizeInputTypeName + case class DerivationConfig( + /** + * Whether to enable the `SemanticNonNull` feature on derivation. + * It is currently disabled by default since it is not yet stable. + */ + enableSemanticNonNull: Boolean = false + ) + /** - * Enables the `SemanticNonNull` feature on derivation. - * It is currently disabled by default since it is not yet stable. - * - * Override this method and return `true` to enable the feature. - */ - def enableSemanticNonNull: Boolean = false + * Returns a configuration object that can be used to customize the derivation behavior. + * + * Override this method to customize the configuration. + */ + def config: DerivationConfig = DerivationConfig() inline def recurseSum[R, P, Label, A <: Tuple]( inline types: List[(String, __Type, List[Any])] = Nil, @@ -104,7 +111,7 @@ trait CommonSchemaDerivation { MagnoliaMacro.typeInfo[A], // Workaround until we figure out why the macro uses the parent's annotations when the leaf is a Scala 3 enum inline if (!MagnoliaMacro.isEnum[A]) MagnoliaMacro.anns[A] else Nil, - enableSemanticNonNull + config.enableSemanticNonNull ) case _ if Macros.hasAnnotation[A, GQLValueType] => new ValueTypeSchema[R, A]( @@ -119,7 +126,7 @@ trait CommonSchemaDerivation { MagnoliaMacro.typeInfo[A], MagnoliaMacro.anns[A], MagnoliaMacro.paramAnns[A].toMap, - enableSemanticNonNull + config.enableSemanticNonNull )(using summonInline[ClassTag[A]]) } diff --git a/core/src/test/scala/caliban/schema/SemanticNonNullSchemaSpec.scala b/core/src/test/scala/caliban/schema/SemanticNonNullSchemaSpec.scala index f65237c43..a1c5400f4 100644 --- a/core/src/test/scala/caliban/schema/SemanticNonNullSchemaSpec.scala +++ b/core/src/test/scala/caliban/schema/SemanticNonNullSchemaSpec.scala @@ -9,7 +9,7 @@ import zio.test.Assertion._ import zio.test._ object SemanticNonNullSchema extends SchemaDerivation[Any] { - override def enableSemanticNonNull: Boolean = true + override def config = DerivationConfig(enableSemanticNonNull = true) } object SemanticNonNullSchemaSpec extends ZIOSpecDefault {