From 09a162a66393c7c0c13a7d6bca57a21d83aa8829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 20 Nov 2021 00:43:17 +0100 Subject: [PATCH] Support effect/abstractEffectType config via settings builder (#1155) --- .../caliban/codegen/CalibanSettings.scala | 51 ++++++++++--------- .../caliban/tools/CalibanCommonSettings.scala | 41 +++++++++------ .../caliban/tools/compiletime/Config.scala | 4 +- .../tools/compiletime/ConfigSpec.scala | 4 +- 4 files changed, 57 insertions(+), 43 deletions(-) diff --git a/codegen-sbt/src/main/scala/caliban/codegen/CalibanSettings.scala b/codegen-sbt/src/main/scala/caliban/codegen/CalibanSettings.scala index d732adb6c..e37f4f152 100644 --- a/codegen-sbt/src/main/scala/caliban/codegen/CalibanSettings.scala +++ b/codegen-sbt/src/main/scala/caliban/codegen/CalibanSettings.scala @@ -6,35 +6,36 @@ import caliban.tools.Codegen.GenType import java.io.File import java.net.URL -sealed trait CalibanSettings +sealed trait CalibanSettings { + type Self <: CalibanSettings + def withSettings(f: CalibanCommonSettings => CalibanCommonSettings): Self -final case class CalibanFileSettings(file: File, settings: CalibanCommonSettings) extends CalibanSettings { + def clientName(value: String): Self = withSettings(_.clientName(value)) + def scalafmtPath(path: String): Self = withSettings(_.scalafmtPath(path)) + def packageName(name: String): Self = withSettings(_.packageName(name)) + def genView(value: Boolean): Self = withSettings(_.genView(value)) + def scalarMapping(mapping: (String, String)*): Self = + withSettings(_.scalarMappings(mapping: _*)) + def imports(values: String*): Self = withSettings(_.imports(values: _*)) + def splitFiles(value: Boolean): Self = withSettings(_.splitFiles(value)) + def enableFmt(value: Boolean): Self = withSettings(_.enableFmt(value)) + def extensibleEnums(value: Boolean): Self = withSettings(_.extensibleEnums(value)) + def genType(genType: GenType): Self = withSettings(_.genType(genType)) + def effect(effect: String): Self = withSettings(_.effect(effect)) + def abstractEffectType(abstractEffectType: Boolean): Self = + withSettings(_.abstractEffectType(abstractEffectType)) +} - def clientName(value: String): CalibanFileSettings = this.copy(settings = this.settings.clientName(value)) - def scalafmtPath(path: String): CalibanFileSettings = this.copy(settings = this.settings.scalafmtPath(path)) - def packageName(name: String): CalibanFileSettings = this.copy(settings = this.settings.packageName(name)) - def genView(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.genView(value)) - def scalarMapping(mapping: (String, String)*): CalibanFileSettings = - this.copy(settings = this.settings.scalarMappings(mapping: _*)) - def imports(values: String*): CalibanFileSettings = this.copy(settings = this.settings.imports(values: _*)) - def splitFiles(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.splitFiles(value)) - def enableFmt(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.enableFmt(value)) - def extensibleEnums(value: Boolean): CalibanFileSettings = this.copy(settings = this.settings.extensibleEnums(value)) - def genType(genType: GenType): CalibanFileSettings = this.copy(settings = this.settings.genType(genType)) +final case class CalibanFileSettings(file: File, settings: CalibanCommonSettings) extends CalibanSettings { + type Self = CalibanFileSettings + def withSettings(f: CalibanCommonSettings => CalibanCommonSettings): Self = + this.copy(settings = f(settings)) } final case class CalibanUrlSettings(url: URL, settings: CalibanCommonSettings) extends CalibanSettings { + type Self = CalibanUrlSettings + def withSettings(f: CalibanCommonSettings => CalibanCommonSettings): Self = + this.copy(settings = f(settings)) - def clientName(value: String): CalibanUrlSettings = this.copy(settings = this.settings.clientName(value)) - def scalafmtPath(path: String): CalibanUrlSettings = this.copy(settings = this.settings.scalafmtPath(path)) - def headers(values: (String, String)*): CalibanUrlSettings = this.copy(settings = this.settings.headers(values: _*)) - def packageName(name: String): CalibanUrlSettings = this.copy(settings = this.settings.packageName(name)) - def genView(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.genView(value)) - def scalarMapping(mapping: (String, String)*): CalibanUrlSettings = - this.copy(settings = this.settings.scalarMappings(mapping: _*)) - def imports(values: String*): CalibanUrlSettings = this.copy(settings = this.settings.imports(values: _*)) - def splitFiles(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.splitFiles(value)) - def enableFmt(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.enableFmt(value)) - def extensibleEnums(value: Boolean): CalibanUrlSettings = this.copy(settings = this.settings.extensibleEnums(value)) - def genType(genType: GenType): CalibanUrlSettings = this.copy(settings = this.settings.genType(genType)) + def headers(values: (String, String)*): CalibanUrlSettings = this.copy(settings = this.settings.headers(values: _*)) } diff --git a/tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala b/tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala index 94d949368..1ee105a0e 100644 --- a/tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala +++ b/tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala @@ -13,7 +13,9 @@ final case class CalibanCommonSettings( splitFiles: Option[Boolean], enableFmt: Option[Boolean], extensibleEnums: Option[Boolean], - genType: GenType + genType: GenType, + effect: Option[String], + abstractEffectType: Option[Boolean] ) { private[caliban] def toOptions(schemaPath: String, toPath: String): Options = @@ -25,10 +27,10 @@ final case class CalibanCommonSettings( packageName = packageName, clientName = clientName, genView = genView, - effect = Option.empty, + effect = effect, scalarMappings = Option(scalarMappings.toMap).filter(_.nonEmpty), imports = Option(imports.toList).filter(_.nonEmpty), - abstractEffectType = Option.empty, + abstractEffectType = abstractEffectType, splitFiles = splitFiles, enableFmt = enableFmt, extensibleEnums = extensibleEnums @@ -46,21 +48,26 @@ final case class CalibanCommonSettings( splitFiles = r.splitFiles.orElse(this.splitFiles), enableFmt = r.enableFmt.orElse(this.enableFmt), extensibleEnums = r.extensibleEnums.orElse(this.extensibleEnums), - genType = r.genType + genType = r.genType, + effect = r.effect.orElse(this.effect), + abstractEffectType = r.abstractEffectType.orElse(this.abstractEffectType) ) - def clientName(value: String): CalibanCommonSettings = this.copy(clientName = Some(value)) - def scalafmtPath(value: String): CalibanCommonSettings = this.copy(scalafmtPath = Some(value)) - def headers(headers: (String, String)*): CalibanCommonSettings = this.copy(headers = this.headers ++ headers) - def packageName(name: String): CalibanCommonSettings = this.copy(packageName = Some(name)) - def genView(value: Boolean): CalibanCommonSettings = this.copy(genView = Some(value)) - def scalarMappings(mapping: (String, String)*): CalibanCommonSettings = + def clientName(value: String): CalibanCommonSettings = this.copy(clientName = Some(value)) + def scalafmtPath(value: String): CalibanCommonSettings = this.copy(scalafmtPath = Some(value)) + def headers(headers: (String, String)*): CalibanCommonSettings = this.copy(headers = this.headers ++ headers) + def packageName(name: String): CalibanCommonSettings = this.copy(packageName = Some(name)) + def genView(value: Boolean): CalibanCommonSettings = this.copy(genView = Some(value)) + def scalarMappings(mapping: (String, String)*): CalibanCommonSettings = this.copy(scalarMappings = this.scalarMappings ++ mapping) - def imports(imports: String*): CalibanCommonSettings = this.copy(imports = this.imports ++ imports) - def splitFiles(value: Boolean): CalibanCommonSettings = this.copy(splitFiles = Some(value)) - def enableFmt(value: Boolean): CalibanCommonSettings = this.copy(enableFmt = Some(value)) - def extensibleEnums(value: Boolean): CalibanCommonSettings = this.copy(extensibleEnums = Some(value)) - def genType(genType: GenType): CalibanCommonSettings = this.copy(genType = genType) + def imports(imports: String*): CalibanCommonSettings = this.copy(imports = this.imports ++ imports) + def splitFiles(value: Boolean): CalibanCommonSettings = this.copy(splitFiles = Some(value)) + def enableFmt(value: Boolean): CalibanCommonSettings = this.copy(enableFmt = Some(value)) + def extensibleEnums(value: Boolean): CalibanCommonSettings = this.copy(extensibleEnums = Some(value)) + def genType(genType: GenType): CalibanCommonSettings = this.copy(genType = genType) + def effect(effect: String): CalibanCommonSettings = this.copy(effect = Some(effect)) + def abstractEffectType(abstractEffectType: Boolean): CalibanCommonSettings = + this.copy(abstractEffectType = Some(abstractEffectType)) } object CalibanCommonSettings { @@ -76,6 +83,8 @@ object CalibanCommonSettings { splitFiles = None, enableFmt = None, extensibleEnums = None, - GenType.Client + genType = GenType.Client, + effect = None, + abstractEffectType = None ) } diff --git a/tools/src/main/scala/caliban/tools/compiletime/Config.scala b/tools/src/main/scala/caliban/tools/compiletime/Config.scala index ee7c01e62..7437848ea 100644 --- a/tools/src/main/scala/caliban/tools/compiletime/Config.scala +++ b/tools/src/main/scala/caliban/tools/compiletime/Config.scala @@ -27,7 +27,9 @@ trait Config { splitFiles = Some(splitFiles), enableFmt = Some(enableFmt), extensibleEnums = Some(extensibleEnums), - GenType.Client + GenType.Client, + effect = None, + abstractEffectType = None ) private[caliban] def asScalaCode: String = { diff --git a/tools/src/test/scala/caliban/tools/compiletime/ConfigSpec.scala b/tools/src/test/scala/caliban/tools/compiletime/ConfigSpec.scala index 6a19e437b..5ffe636bf 100644 --- a/tools/src/test/scala/caliban/tools/compiletime/ConfigSpec.scala +++ b/tools/src/test/scala/caliban/tools/compiletime/ConfigSpec.scala @@ -37,7 +37,9 @@ object ConfigSpec extends DefaultRunnableSpec { imports = List("zio.test._", "caliban.tools.compiletime._"), splitFiles = Some(true), enableFmt = Some(false), - extensibleEnums = Some(true) + extensibleEnums = Some(true), + effect = None, + abstractEffectType = None ) ) )