Skip to content

Commit

Permalink
case class wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsimpson committed Mar 25, 2024
1 parent 3832f0c commit e5e0f1c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,24 @@ object BasicGenerator {
headTag -> taggedObj
}

val maybeSpecificationExtensionKeys = doc.paths
val specificationExtensions = doc.paths
.flatMap { p =>
p.specificationExtensions.toSeq ++ p.methods.flatMap(_.specificationExtensions.toSeq)
}
.groupBy(_._1)
val specificationExtensionWrapper = if (specificationExtensions.isEmpty) "" else "case class XSpecificationExtension[T](value: T)\n"
val maybeSpecificationExtensionKeys = specificationExtensions
.map { case (keyName, pairs) =>
val values = pairs.map(_._2)
val `type` = SpecificationExtensionRenderer.renderCombinedType(values)
val name = strippedToCamelCase(keyName)
val uncapitalisedName = name.head.toLower + name.tail
val capitalisedName = name.head.toUpper + name.tail
s"""type ${capitalisedName}Extension = ${`type`}
|val ${uncapitalisedName}ExtensionKey = new sttp.tapir.AttributeKey[${capitalisedName}Extension]("$packagePath.$objName.${capitalisedName}Extension")
s"""type ${capitalisedName}X = ${`type`}
|val ${uncapitalisedName}XKey = new sttp.tapir.AttributeKey[XSpecificationExtension[${capitalisedName}X]]("$packagePath.$objName.XSpecificationExtension[$packagePath.$objName.${capitalisedName}X]")
|""".stripMargin
}
.mkString("\n")
.mkString(specificationExtensionWrapper, "\n", "")

val mainObj = s"""|
|package $packagePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ class EndpointGenerator {
.map { case (k, v) =>
val camelCaseK = strippedToCamelCase(k)
val uncapitalisedName = camelCaseK.head.toLower + camelCaseK.tail
s""".attribute[${camelCaseK.capitalize}Extension](${uncapitalisedName}ExtensionKey, ${SpecificationExtensionRenderer.renderValue(v)})"""
val extensionType = s"${camelCaseK.capitalize}X"
val wrappedValue = s"XSpecificationExtension[$extensionType](${SpecificationExtensionRenderer.renderValue(v)})"
s""".attribute[XSpecificationExtension[$extensionType]](${uncapitalisedName}XKey, $wrappedValue)"""
}
.mkString("\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,25 @@ class EndpointGeneratorSpec extends CompileCheckTestBase {
)("TapirGeneratedEndpoints")
generatedCode shouldCompile ()
val expectedAttrDecls = Seq(
""".attribute[CustomStringExtensionOnPathExtension](customStringExtensionOnPathExtensionKey, "another string")""",
""".attribute[CustomStringExtensionOnOperationExtension](customStringExtensionOnOperationExtensionKey, "bazquux")""",
""".attribute[CustomListExtensionOnOperationExtension](customListExtensionOnOperationExtensionKey, Vector("baz", "quux"))""",
""".attribute[CustomMapExtensionOnPathExtension](customMapExtensionOnPathExtensionKey, Map("bazkey" -> "bazval", "quuxkey" -> Vector("quux1", "quux2"))""",
""".attribute[CustomStringExtensionOnPathDoubleTypeExtension](customStringExtensionOnPathDoubleTypeExtensionKey, 123L)"""
""".attribute[XSpecificationExtension[CustomStringExtensionOnPathX]](customStringExtensionOnPathXKey, XSpecificationExtension[CustomStringExtensionOnPathX]("another string"))""",
""".attribute[XSpecificationExtension[CustomStringExtensionOnOperationX]](customStringExtensionOnOperationXKey, XSpecificationExtension[CustomStringExtensionOnOperationX]("bazquux"))""",
""".attribute[XSpecificationExtension[CustomListExtensionOnOperationX]](customListExtensionOnOperationXKey, XSpecificationExtension[CustomListExtensionOnOperationX](Vector("baz", "quux")))""",
""".attribute[XSpecificationExtension[CustomMapExtensionOnPathX]](customMapExtensionOnPathXKey, XSpecificationExtension[CustomMapExtensionOnPathX](Map("bazkey" -> "bazval", "quuxkey" -> Vector("quux1", "quux2")))""",
""".attribute[XSpecificationExtension[CustomStringExtensionOnPathDoubleTypeX]](customStringExtensionOnPathDoubleTypeXKey, XSpecificationExtension[CustomStringExtensionOnPathDoubleTypeX](123L))"""
)
expectedAttrDecls foreach (decl => generatedCode should include(decl))
generatedCode should include(
"""val customMapExtensionOnOperationExtensionKey = new sttp.tapir.AttributeKey[CustomMapExtensionOnOperationExtension]("sttp.tapir.generated.TapirGeneratedEndpoints.CustomMapExtensionOnOperationExtension")""".stripMargin
"""val customMapExtensionOnOperationXKey = new sttp.tapir.AttributeKey[XSpecificationExtension[CustomMapExtensionOnOperationX]]("sttp.tapir.generated.TapirGeneratedEndpoints.XSpecificationExtension[sttp.tapir.generated.TapirGeneratedEndpoints.CustomMapExtensionOnOperationX]")""".stripMargin
)
val expectedKeyDeclarations = Seq(
"""type CustomMapExtensionOnOperationExtension = Map[String, Any]""",
"""type CustomListExtensionOnPathAnyTypeExtension = Seq[Any]""",
"""type CustomMapExtensionOnPathSingleValueTypeExtension = Map[String, String]""",
"""type CustomListExtensionOnOperationExtension = Seq[String]""",
"""type CustomStringExtensionOnPathAnyTypeExtension = Any""",
"""type CustomStringExtensionOnPathDoubleTypeExtension = Double""",
"""type CustomListExtensionOnPathExtension = Seq[String]""",
"""type CustomStringExtensionOnPathExtension = String"""
"""type CustomMapExtensionOnOperationX = Map[String, Any]""",
"""type CustomListExtensionOnPathAnyTypeX = Seq[Any]""",
"""type CustomMapExtensionOnPathSingleValueTypeX = Map[String, String]""",
"""type CustomListExtensionOnOperationX = Seq[String]""",
"""type CustomStringExtensionOnPathAnyTypeX = Any""",
"""type CustomStringExtensionOnPathDoubleTypeX = Double""",
"""type CustomListExtensionOnPathX = Seq[String]""",
"""type CustomStringExtensionOnPathX = String"""
)
expectedKeyDeclarations foreach (decl => generatedCode should include(decl))
}
Expand Down

0 comments on commit e5e0f1c

Please sign in to comment.