Skip to content

Commit ad83808

Browse files
committed
fix more indentations and enum type names
1 parent bf5586c commit ad83808

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

modules/core/shared/src/main/scala/codegen.scala

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ def generateBySpec(
255255
pkg = schemasPkg,
256256
objName = commonCodecsObj,
257257
jsonCodec = config.jsonCodec,
258-
dialect = config.dialect,
259258
hasProps = p => specs.hasProps(p),
260259
arrType = config.arrayType
261260
) match
@@ -301,14 +300,11 @@ def generateBySpec(
301300

302301
val scalaKeyWords = Set("type", "import", "val", "object", "enum", "export")
303302

303+
def toScalaTypeName(n: String): String = toScalaName(n.capitalize)
304+
304305
def toScalaName(n: String): String =
305-
n match
306-
// to fix a compiler warning like
307-
// import looks like a language import, but refers to something else: object language in object GoogleCloudAiplatformV1ExecutableCode
308-
case "language" => "Language"
309-
case _ =>
310-
if scalaKeyWords.contains(n) then s"`$n`"
311-
else n.replaceAll("[^a-zA-Z0-9_]", "")
306+
if scalaKeyWords.contains(n) then s"`$n`"
307+
else n.replaceAll("[^a-zA-Z0-9_]", "")
312308

313309
def resourceCode(
314310
rootPkg: String,
@@ -467,16 +463,16 @@ def schemasCode(
467463
if jsonCodec == JsonCodec.Jsoniter then
468464
enums
469465
.map((k, e) =>
470-
s"enum ${toScalaName(k)} {\n${e.values.map(v => s"${toComment(Some(v.enumDescription))} case ${toScalaName(v.value)}").mkString("\n ")}}\n"
466+
s" enum ${toScalaTypeName(k)} {\n${e.values.map(v => s"${toComment(Some(v.enumDescription), " ")} case ${toScalaName(v.value)}").mkString("\n ")}\n }\n"
471467
)
472468
.mkString("\n")
473469
else "",
474470
jsonCodec match
475471
case JsonCodec.ZioJson =>
476-
s"${implicitVal(dialect)} jsonCodec: JsonCodec[$objName] = JsonCodec.derived[$objName]"
472+
s" given jsonCodec: JsonCodec[$objName] = JsonCodec.derived[$objName]"
477473
case JsonCodec.Jsoniter =>
478-
s"""|${implicitVal(dialect)} jsonCodec: JsonValueCodec[$objName] =
479-
| JsonCodecMaker.make(CodecMakerConfig.withAllowRecursiveTypes(true).withDiscriminatorFieldName(None))""".stripMargin,
474+
s"""| given jsonCodec: JsonValueCodec[$objName] =
475+
| JsonCodecMaker.make(CodecMakerConfig.withAllowRecursiveTypes(true).withDiscriminatorFieldName(None))""".stripMargin,
480476
"}"
481477
).mkString("\n")
482478

@@ -488,7 +484,7 @@ def schemasCode(
488484
.map { (n, t) =>
489485
val enumType =
490486
if jsonCodec == JsonCodec.ZioJson then SchemaType.EnumType.Literal
491-
else SchemaType.EnumType.Nominal(s"$scalaName.$n")
487+
else SchemaType.EnumType.Nominal(s"$scalaName.${toScalaTypeName(n)}")
492488
s"${toComment(t.withTypeDescription)} $n: ${
493489
(if (t.optional) s"${t.scalaType(arrType, enumType)} = None" else t.scalaType(arrType, enumType))
494490
}"
@@ -522,7 +518,6 @@ def commonSchemaCodecs(
522518
pkg: String,
523519
objName: String,
524520
jsonCodec: JsonCodec,
525-
dialect: Dialect,
526521
hasProps: SchemaPath => Boolean,
527522
arrType: ArrayType
528523
): Option[String] = {
@@ -534,7 +529,7 @@ def commonSchemaCodecs(
534529
.collect { case (k, Property(_, SchemaType.Array(typ, _), _)) =>
535530
val enumType =
536531
if jsonCodec == JsonCodec.ZioJson then SchemaType.EnumType.Literal
537-
else SchemaType.EnumType.Nominal(s"${sk.lastOption.getOrElse("")}.$k")
532+
else SchemaType.EnumType.Nominal(s"${sk.lastOption.getOrElse("")}.${toScalaTypeName(k)}")
538533
typ.scalaType(arrType, enumType)
539534
}
540535
)
@@ -552,17 +547,17 @@ def commonSchemaCodecs(
552547
s"object $objName {",
553548
props
554549
.map { t =>
555-
val prefix = implicitVal(dialect) + " " + toScalaName(t + "ChunkCodec")
550+
val prefix = " given " + toScalaName(t + "ChunkCodec")
556551
s"""|${prefix}: JsonValueCodec[Chunk[$t]] = new JsonValueCodec[Chunk[$t]] {
557-
| val arrCodec: JsonValueCodec[Array[$t]] = JsonCodecMaker.make
552+
| val arrCodec: JsonValueCodec[Array[$t]] = JsonCodecMaker.make
558553
|
559-
| override val nullValue: Chunk[$t] = Chunk.empty
554+
| override val nullValue: Chunk[$t] = Chunk.empty
560555
|
561-
| override def decodeValue(in: JsonReader, default: Chunk[$t]): Chunk[$t] =
562-
| Chunk.fromArray(arrCodec.decodeValue(in, default.toArray))
556+
| override def decodeValue(in: JsonReader, default: Chunk[$t]): Chunk[$t] =
557+
| Chunk.fromArray(arrCodec.decodeValue(in, default.toArray))
563558
|
564-
| override def encodeValue(x: Chunk[$t], out: JsonWriter): Unit =
565-
| arrCodec.encodeValue(x.toArray, out)
559+
| override def encodeValue(x: Chunk[$t], out: JsonWriter): Unit =
560+
| arrCodec.encodeValue(x.toArray, out)
566561
|}""".stripMargin
567562
}
568563
.mkString("\n\n"),
@@ -691,7 +686,7 @@ case class Parameter(
691686
required: Boolean = false,
692687
pattern: Option[String] = None
693688
) {
694-
def scalaType(arrType: ArrayType) = typ.withOptional(!required).scalaType(arrType)
689+
def scalaType(arrType: ArrayType): String = typ.withOptional(!required).scalaType(arrType)
695690
}
696691

697692
object Parameter:
@@ -842,7 +837,7 @@ object SchemaPath:
842837

843838
extension (s: SchemaPath)
844839
def scalaName: String =
845-
s.filter(!Set("items", "properties").contains(_)).map(_.capitalize).mkString
840+
s.filter(!Set("items", "properties").contains(_)).map(toScalaTypeName(_)).mkString
846841

847842
def add(nested: String): SchemaPath = s.appended(nested)
848843
def hasNested: Boolean = s.size > 1
@@ -927,7 +922,7 @@ object ResourcePath:
927922
def apply(pp: Vector[String], p: String): ResourcePath = pp :+ p
928923
extension (r: ResourcePath)
929924
def add(p: String): ResourcePath = r :+ p
930-
def scalaName: String = r.last.capitalize
925+
def scalaName: String = toScalaTypeName(r.last)
931926
def pkgPath: Vector[String] = r.dropRight(1).map(camelToSnakeCase)
932927
def pkgName(base: String): String = s"$base${if pkgPath.nonEmpty then pkgPath.mkString(".", ".", "") else ""}"
933928
def dirPath(base: Path): Path = base / pkgPath
@@ -967,9 +962,6 @@ def camelToSnakeCase(camelCase: String): String = {
967962
camelCaseRegex.replaceAllIn(camelCase, matched => "_" + matched.group(0).toLowerCase)
968963
}
969964

970-
def implicitVal(dialect: Dialect) = dialect match
971-
case Dialect.Scala3 => "given"
972-
973965
// comment splitted into multipl lines
974966
private def toComment(content: Iterable[String], indent: String = " "): String =
975967
if content.isEmpty then ""

0 commit comments

Comments
 (0)