Skip to content

Commit

Permalink
Support effect/abstractEffectType config via settings builder (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Nov 19, 2021
1 parent 1c8bd15 commit 09a162a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
51 changes: 26 additions & 25 deletions codegen-sbt/src/main/scala/caliban/codegen/CalibanSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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: _*))
}
41 changes: 25 additions & 16 deletions tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -76,6 +83,8 @@ object CalibanCommonSettings {
splitFiles = None,
enableFmt = None,
extensibleEnums = None,
GenType.Client
genType = GenType.Client,
effect = None,
abstractEffectType = None
)
}
4 changes: 3 additions & 1 deletion tools/src/main/scala/caliban/tools/compiletime/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
)
Expand Down

0 comments on commit 09a162a

Please sign in to comment.