Skip to content

Commit

Permalink
Add excludeDeprecated option to client codegen (#2163)
Browse files Browse the repository at this point in the history
* Add excludeDeprecated option to client codegen

* Fix codeformat

* Fix ConfigSpec

* Maybe fix codegen-sbt
  • Loading branch information
jeejeeone authored Mar 22, 2024
1 parent 064be42 commit ceaad5b
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sealed trait CalibanSettings {
final def preserveInputNames(value: Boolean): Self = withSettings(_.preserveInputNames(value))
final def addDerives(value: Boolean): Self = withSettings(_.addDerives(value))
final def envForDerives(value: String): Self = withSettings(_.envForDerives(value))
final def excludeDeprecated(value: Boolean): Self = withSettings(_.excludeDeprecated(value))
}

final case class CalibanFileSettings(file: File, settings: CalibanCommonSettings) extends CalibanSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ object OptionsParser {
preserveInputNames: Option[Boolean],
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String]
envForDerives: Option[String],
excludeDeprecated: Option[Boolean]
)

private object DescriptorUtils {
Expand Down Expand Up @@ -74,7 +75,8 @@ object OptionsParser {
rawOpts.preserveInputNames,
rawOpts.supportIsRepeatable,
rawOpts.addDerives,
rawOpts.envForDerives
rawOpts.envForDerives,
rawOpts.excludeDeprecated
)
}.option
case _ => ZIO.none
Expand Down
44 changes: 44 additions & 0 deletions codegen-sbt/src/test/scala/caliban/codegen/OptionsParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -59,6 +60,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -89,6 +91,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -137,6 +140,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -167,6 +171,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -197,6 +202,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -227,6 +233,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -257,6 +264,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -287,6 +295,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -317,6 +326,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -347,6 +357,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -377,6 +388,7 @@ object OptionsParserSpec extends ZIOSpecDefault {
Some(true),
None,
None,
None,
None
)
)
Expand Down Expand Up @@ -407,11 +419,43 @@ object OptionsParserSpec extends ZIOSpecDefault {
None,
None,
None,
None,
None
)
)
)
}
},
test("provide excludeDeprecated") {
val input = List("schema", "output", "--excludeDeprecated", "true")
OptionsParser.fromArgs(input).map { result =>
assertTrue(
result ==
Some(
Options(
"schema",
"output",
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Some(true)
)
)
)
}
}
)
}
13 changes: 9 additions & 4 deletions tools/src/main/scala/caliban/tools/CalibanCommonSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ final case class CalibanCommonSettings(
preserveInputNames: Option[Boolean],
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String]
envForDerives: Option[String],
excludeDeprecated: Option[Boolean]
) {

private[caliban] def toOptions(schemaPath: String, toPath: String): Options =
Expand All @@ -41,7 +42,8 @@ final case class CalibanCommonSettings(
preserveInputNames = preserveInputNames,
supportIsRepeatable = supportIsRepeatable,
addDerives = addDerives,
envForDerives = envForDerives
envForDerives = envForDerives,
excludeDeprecated = excludeDeprecated
)

private[caliban] def combine(r: => CalibanCommonSettings): CalibanCommonSettings =
Expand All @@ -62,7 +64,8 @@ final case class CalibanCommonSettings(
preserveInputNames = r.preserveInputNames.orElse(this.preserveInputNames),
supportIsRepeatable = r.supportIsRepeatable.orElse(this.supportIsRepeatable),
addDerives = r.addDerives.orElse(this.addDerives),
envForDerives = r.envForDerives.orElse(this.envForDerives)
envForDerives = r.envForDerives.orElse(this.envForDerives),
excludeDeprecated = r.excludeDeprecated.orElse(this.excludeDeprecated)
)

def clientName(value: String): CalibanCommonSettings = this.copy(clientName = Some(value))
Expand All @@ -86,6 +89,7 @@ final case class CalibanCommonSettings(
this.copy(supportIsRepeatable = Some(supportIsRepeatable))
def addDerives(addDerives: Boolean): CalibanCommonSettings = this.copy(addDerives = Some(addDerives))
def envForDerives(envForDerives: String): CalibanCommonSettings = this.copy(envForDerives = Some(envForDerives))
def excludeDeprecated(value: Boolean): CalibanCommonSettings = this.copy(excludeDeprecated = Some(value))
}

object CalibanCommonSettings {
Expand All @@ -107,6 +111,7 @@ object CalibanCommonSettings {
preserveInputNames = None,
supportIsRepeatable = None,
addDerives = None,
envForDerives = None
envForDerives = None,
excludeDeprecated = None
)
}
73 changes: 46 additions & 27 deletions tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ object ClientWriter {
additionalImports: Option[List[String]] = None,
splitFiles: Boolean = false,
extensibleEnums: Boolean = false,
scalarMappings: Option[Map[String, String]] = None
scalarMappings: Option[Map[String, String]] = None,
excludeDeprecated: Boolean = false
): List[(String, String)] = {
require(packageName.isDefined || !splitFiles, "splitFiles option requires a package name")

Expand Down Expand Up @@ -430,11 +431,16 @@ object ClientWriter {
s"type $objectName"
}

def writeObject(typedef: ObjectTypeDefinition, genView: Boolean): String = {
def writeObject(typedef: ObjectTypeDefinition, genView: Boolean, excludeDeprecated: Boolean): String = {
val allFields =
if (excludeDeprecated)
typedef.fields.filterNot(field => field.directives.find(_.name == "deprecated").isDefined)
else
typedef.fields

val objectName: String = safeTypeName(typedef.name)

val optionalUnionTypeFields = typedef.fields.flatMap { field =>
val optionalUnionTypeFields = allFields.flatMap { field =>
if (isOptionalUnionType(field))
Some(
collectFieldInfo(
Expand All @@ -448,7 +454,7 @@ object ClientWriter {
else None
}

val optionalInterfaceTypeFields = typedef.fields.flatMap { field =>
val optionalInterfaceTypeFields = allFields.flatMap { field =>
if (isOptionalInterfaceType(field))
Vector(
collectFieldInfo(
Expand All @@ -469,15 +475,14 @@ object ClientWriter {
else Vector.empty
}

val fields = typedef.fields.map(
collectFieldInfo(_, objectName, optionalUnion = false, optionalInterface = false, commonInterface = false)
)
val view = if (genView) "\n " + writeView(typedef.name, fields.map(_.typeInfo)) else ""
val fields = allFields
.map(collectFieldInfo(_, objectName, optionalUnion = false, optionalInterface = false, commonInterface = false))
val view = if (genView && fields.nonEmpty) "\n " + writeView(typedef.name, fields.map(_.typeInfo)) else ""

val allFields = fields ++ optionalUnionTypeFields ++ optionalInterfaceTypeFields
val objectFields = fields ++ optionalUnionTypeFields ++ optionalInterfaceTypeFields

s"""object $objectName {$view
| ${allFields.distinct.map(writeFieldInfo).mkString("\n ")}
| ${objectFields.distinct.map(writeFieldInfo).mkString("\n ")}
|}
|""".stripMargin
}
Expand Down Expand Up @@ -690,22 +695,28 @@ object ClientWriter {
.map(v => s"""case ${typedef.name}.${safeEnumValue(v.enumValue)} => __EnumValue("${v.enumValue}")""") ++
(if (extensibleEnums) Some(s"case ${typedef.name}.__Unknown (value) => __EnumValue(value)") else None)

s"""sealed trait $enumName extends scala.Product with scala.Serializable { def value: String }
object $enumName {
${enumCases.mkString("\n")}

implicit val decoder: ScalarDecoder[$enumName] = {
${decoderCases.mkString("\n")}
case other => Left(DecodingError(s"Can't build ${typedef.name} from input $$other"))
}
implicit val encoder: ArgEncoder[${typedef.name}] = {
${encoderCases.mkString("\n")}
}
val enumObject =
if (typedef.enumValuesDefinition.nonEmpty)
s"""object $enumName {
${enumCases.mkString("\n")}

implicit val decoder: ScalarDecoder[$enumName] = {
${decoderCases.mkString("\n")}
case other => Left(DecodingError(s"Can't build ${typedef.name} from input $$other"))
}
implicit val encoder: ArgEncoder[${typedef.name}] = {
${encoderCases.mkString("\n")}
}

val values: scala.collection.immutable.Vector[$enumName] = scala.collection.immutable.Vector(${typedef.enumValuesDefinition
.map(v => safeEnumValue(v.enumValue))
.mkString(", ")})
}"""
else
""

val values: scala.collection.immutable.Vector[$enumName] = scala.collection.immutable.Vector(${typedef.enumValuesDefinition
.map(v => safeEnumValue(v.enumValue))
.mkString(", ")})
}
s"""sealed trait $enumName extends scala.Product with scala.Serializable { def value: String }
$enumObject
"""
}

Expand Down Expand Up @@ -785,7 +796,7 @@ object ClientWriter {
directives = typedef.directives,
fields = typedef.fields
)
val content = writeObject(objDef, genView)
val content = writeObject(objDef, genView, excludeDeprecated)
val fullContent =
if (splitFiles)
s"""import caliban.client.FieldBuilder._
Expand Down Expand Up @@ -820,7 +831,7 @@ object ClientWriter {
schemaDef.exists(_.subscription.getOrElse("Subscription") == obj.name)
)
.map { typedef =>
val content = writeObject(typedef, genView)
val content = writeObject(typedef, genView, excludeDeprecated)
val fullContent =
if (splitFiles)
s"""import caliban.client.FieldBuilder._
Expand Down Expand Up @@ -851,6 +862,14 @@ object ClientWriter {

val enums = schema.enumTypeDefinitions
.filter(e => !scalarMappingsWithDefaults.contains(e.name))
.map {
case typedef if excludeDeprecated =>
val valuesWithoutDeprecated =
typedef.enumValuesDefinition.filterNot(value => value.directives.find(_.name == "deprecated").isDefined)

typedef.copy(enumValuesDefinition = valuesWithoutDeprecated)
case typedef => typedef
}
.map { typedef =>
val content = writeEnum(typedef, extensibleEnums = extensibleEnums)
val fullContent =
Expand Down
4 changes: 3 additions & 1 deletion tools/src/main/scala/caliban/tools/Codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Codegen {
splitFiles = arguments.splitFiles.getOrElse(false)
enableFmt = arguments.enableFmt.getOrElse(true)
extensibleEnums = arguments.extensibleEnums.getOrElse(false)
excludeDeprecated = arguments.excludeDeprecated.getOrElse(false)
code = genType match {
case GenType.Schema =>
List(
Expand All @@ -58,7 +59,8 @@ object Codegen {
arguments.imports,
splitFiles,
extensibleEnums,
scalarMappings
scalarMappings,
excludeDeprecated
)
}
formatted <- if (enableFmt) Formatter.format(code, arguments.fmtPath) else ZIO.succeed(code)
Expand Down
3 changes: 2 additions & 1 deletion tools/src/main/scala/caliban/tools/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ final case class Options(
preserveInputNames: Option[Boolean],
supportIsRepeatable: Option[Boolean],
addDerives: Option[Boolean],
envForDerives: Option[String]
envForDerives: Option[String],
excludeDeprecated: Option[Boolean]
)

object Options {
Expand Down
Loading

0 comments on commit ceaad5b

Please sign in to comment.