Skip to content

Commit

Permalink
use zio-config 4.x (#2373)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpdaniels authored Aug 31, 2024
1 parent 35aa78e commit e0715dc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 52 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ val zioVersion = "2.1.9"
val zioInteropCats2Version = "22.0.0.0"
val zioInteropCats3Version = "23.1.0.3"
val zioInteropReactiveVersion = "2.0.2"
val zioConfigVersion = "3.0.7"
val zioConfigVersion = "4.0.2"
val zqueryVersion = "0.7.5"
val zioJsonVersion = "0.7.3"
val zioHttpVersion = "3.0.0-RC9"
Expand Down
122 changes: 71 additions & 51 deletions codegen-sbt/src/main/scala/caliban/codegen/OptionsParser.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package caliban.codegen

import caliban.tools.Options
import zio.config.magnolia.Descriptor
import zio.config.{ read, ConfigDescriptor, ConfigSource }
import zio.{ UIO, ZIO }
import zio.config.magnolia.DeriveConfig
import zio.{ Config, ConfigProvider, UIO, ZIO }
import scala.annotation.tailrec

object OptionsParser {
final private case class RawOptions(
Expand All @@ -27,61 +27,81 @@ object OptionsParser {
supportDeprecatedArgs: Option[Boolean]
)

private object DescriptorUtils {
def from[A](configSource: ConfigSource)(implicit config: Descriptor[A]): ConfigDescriptor[A] =
Descriptor.descriptor[A] from configSource
private object RawOptions {
val config: Config[RawOptions] = DeriveConfig.deriveConfig[RawOptions]
}

def fromArgs(args: List[String]): UIO[Option[Options]] =
args match {
case schemaPath :: toPath :: other =>
val configSource: ConfigSource =
ConfigSource.fromCommandLineArgs(
args = other,
keyDelimiter = Some('.'),
valueDelimiter = Some(',')
)
val configDescriptor: ConfigDescriptor[RawOptions] = DescriptorUtils.from[RawOptions](configSource)

read[RawOptions](configDescriptor).map { rawOpts =>
Options(
schemaPath,
toPath,
rawOpts.scalafmtPath,
rawOpts.headers.map {
_.flatMap { rawHeader =>
rawHeader.split(":").toList match {
case name :: values if values.nonEmpty => Some(Options.Header(name, values.mkString(":")))
case _ => None
}
extractArgs(other) match {
case Some(configProvider) =>
configProvider
.load(RawOptions.config)
.map { rawOpts =>
Options(
schemaPath,
toPath,
rawOpts.scalafmtPath,
rawOpts.headers.map {
_.flatMap { rawHeader =>
rawHeader.split(":").toList match {
case name :: values if values.nonEmpty => Some(Options.Header(name, values.mkString(":")))
case _ => None
}
}
},
rawOpts.packageName,
rawOpts.clientName,
rawOpts.genView,
rawOpts.effect,
rawOpts.scalarMappings.map {
_.flatMap { rawMapping =>
rawMapping.split(":").toList match {
case name :: value :: Nil => Some(name -> value)
case _ => None
}
}.toMap
},
rawOpts.imports,
rawOpts.abstractEffectType,
rawOpts.splitFiles,
rawOpts.enableFmt,
rawOpts.extensibleEnums,
rawOpts.preserveInputNames,
rawOpts.supportIsRepeatable,
rawOpts.addDerives,
rawOpts.envForDerives,
rawOpts.excludeDeprecated,
rawOpts.supportDeprecatedArgs
)
}
},
rawOpts.packageName,
rawOpts.clientName,
rawOpts.genView,
rawOpts.effect,
rawOpts.scalarMappings.map {
_.flatMap { rawMapping =>
rawMapping.split(":").toList match {
case name :: value :: Nil => Some(name -> value)
case _ => None
}
}.toMap
},
rawOpts.imports,
rawOpts.abstractEffectType,
rawOpts.splitFiles,
rawOpts.enableFmt,
rawOpts.extensibleEnums,
rawOpts.preserveInputNames,
rawOpts.supportIsRepeatable,
rawOpts.addDerives,
rawOpts.envForDerives,
rawOpts.excludeDeprecated,
rawOpts.supportDeprecatedArgs
)
}.option
.option
case None => ZIO.none
}
case _ => ZIO.none
}

private def extractArgs(args: List[String]): Option[ConfigProvider] = {
val argMap = Map.newBuilder[String, String]

@tailrec
def loop(stack: List[String]): Option[Map[String, String]] =
stack match {
case key :: value :: rest =>
if (key.startsWith("--")) {
argMap += key.drop(2) -> value
loop(rest)
} else {
None
}
case unmatched :: Nil =>
None
case Nil =>
Some(argMap.result())
}

loop(args).map(ConfigProvider.fromMap(_))
}

}

0 comments on commit e0715dc

Please sign in to comment.