Skip to content

Commit 3da3636

Browse files
committed
update api scpecs, handle non-unique endpoint locations
1 parent e723d54 commit 3da3636

File tree

11 files changed

+52513
-36769
lines changed

11 files changed

+52513
-36769
lines changed

.scalafmt.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version = "3.8.3"
1+
version = "3.10.1"
22
runner.dialect = scala3
33
maxColumn = 120

build.sbt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ThisBuild / description := "Google Cloud client code generator"
22
ThisBuild / organization := "dev.rolang"
33
ThisBuild / licenses := Seq(License.MIT)
44
ThisBuild / homepage := Some(url("https://github.com/rolang/google-rest-api-codegen"))
5-
ThisBuild / scalaVersion := "3.7.3"
5+
ThisBuild / scalaVersion := "3.7.4"
66
ThisBuild / version ~= { v => if (v.contains('+')) s"${v.replace('+', '-')}-SNAPSHOT" else v }
77
ThisBuild / versionScheme := Some("early-semver")
88
ThisBuild / scmInfo := Some(
@@ -40,15 +40,17 @@ lazy val noPublish = Seq(
4040
publish / skip := true
4141
)
4242

43-
lazy val sttpClient4Version = "4.0.11"
43+
val sttpClient4Version = "4.0.13"
4444

45-
lazy val zioVersion = "2.1.21"
45+
val zioVersion = "2.1.22"
4646

47-
lazy val zioJsonVersion = "0.7.44"
47+
val zioJsonVersion = "0.7.45"
4848

49-
lazy val jsoniterVersion = "2.38.2"
49+
val jsoniterVersion = "2.38.4"
5050

51-
lazy val munitVersion = "1.2.0"
51+
val munitVersion = "1.2.1"
52+
53+
val upickleVersion = "4.4.1"
5254

5355
addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
5456

@@ -64,7 +66,13 @@ lazy val root = (project in file("."))
6466
// for supporting code inspection / testing of generated code via test_gen.sh script
6567
lazy val testLocal = (project in file("test-local"))
6668
.settings(
67-
libraryDependencies ++= dependencyByConfig("Sttp4", "Jsoniter", "ZioChunk")
69+
libraryDependencies ++= Seq(
70+
"com.softwaremill.sttp.client4" %% "core" % sttpClient4Version,
71+
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-core" % jsoniterVersion,
72+
"com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % jsoniterVersion % "compile-internal",
73+
"dev.zio" %% "zio-json" % zioJsonVersion,
74+
"dev.zio" %% "zio" % zioVersion
75+
)
6876
)
6977
.settings(noPublish)
7078

@@ -77,7 +85,7 @@ lazy val core = crossProject(JVMPlatform, NativePlatform)
7785
)
7886
.settings(
7987
libraryDependencies ++= Seq(
80-
"com.lihaoyi" %%% "upickle" % "4.1.0"
88+
"com.lihaoyi" %%% "upickle" % upickleVersion
8189
)
8290
)
8391

modules/cli/src/main/scala/cli.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// for test runs using scala-cli
22
//> using jvm system
3-
//> using scala 3.7.3
3+
//> using scala 3.7.4
44
//> using file ../../../../core/shared/src/main/scala/codegen.scala
5-
//> using dep com.lihaoyi::upickle:4.3.2
5+
//> using dep com.lihaoyi::upickle:4.4.1
66

77
package gcp.codegen.cli
88

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,18 @@ def generateBySpec(
129129
if specs.endpoints.nonEmpty then "enum Endpoint(val location: String, val url: Uri):" else "",
130130
specs.endpoints
131131
.map(e =>
132+
// for cases where location is not unique we will append numbers
133+
val locationPostfix = specs.endpoints
134+
.collect { case Endpoint(location = e.location, endpointUrl = endpointUrl) =>
135+
endpointUrl
136+
}
137+
.zipWithIndex
138+
.collectFirst { case (e.endpointUrl, idx) if idx > 0 => (idx + 1).toString() }
139+
.getOrElse("")
140+
132141
s"""${toComment(
133142
Some(e.description)
134-
)} case `${e.location}` extends Endpoint("${e.location}", uri"${e.endpointUrl}")""".stripMargin
143+
)} case `${e.location}$locationPostfix` extends Endpoint("${e.location}", uri"${e.endpointUrl}")""".stripMargin
135144
)
136145
.mkString("\n")
137146
).mkString("\n")
@@ -220,7 +229,7 @@ def generateBySpec(
220229
hasProps = p => specs.hasProps(p),
221230
arrType = config.arrayType
222231
) match
223-
case None => Nil
232+
case None => Nil
224233
case Some(codecs) =>
225234
Files.writeString(commonCodecsPath, codecs)
226235
List(commonCodecsPath.toFile())
@@ -301,10 +310,9 @@ def resourceCode(
301310
"\\{(.*?)\\}".r.findAllIn(s).toList match
302311
case Nil => s"PathSegment(\"$s\")"
303312
case v :: Nil if v == s => "PathSegment(" + toScalaName(v.stripPrefix("{").stripSuffix("}")) + ")"
304-
case vars =>
305-
"PathSegment(s\"" + vars.foldLeft(s)((res, v) =>
306-
res.replace(v, "$" + v.stripPrefix("{").stripSuffix("}"))
307-
) + "\")"
313+
case vars =>
314+
"PathSegment(s\"" + vars
315+
.foldLeft(s)((res, v) => res.replace(v, "$" + v.stripPrefix("{").stripSuffix("}"))) + "\")"
308316
)
309317

310318
val req = method.mediaUploads match
@@ -362,15 +370,14 @@ def resourceCode(
362370

363371
val queryParams = "\n val params = " +
364372
(method.scalaQueryParams match
365-
case Nil => "commonQueryParams.value"
373+
case Nil => "commonQueryParams.value"
366374
case qParams =>
367375
qParams
368376
.map {
369377
case (k, p) if p.required => s"""Map("$k" -> $k)"""
370378
case (k, p) => s"""$k.map(p => Map("$k" -> p.toString)).getOrElse(Map.empty)"""
371379
}
372-
.mkString("", " ++ ", " ++ commonQueryParams.value\n")
373-
)
380+
.mkString("", " ++ ", " ++ commonQueryParams.value\n"))
374381

375382
def responseType(t: String) =
376383
httpSource match
@@ -459,7 +466,7 @@ def schemasCode(
459466
s"package $pkg",
460467
"",
461468
jsonCodec match {
462-
case JsonCodec.ZioJson => "import zio.json.*"
469+
case JsonCodec.ZioJson => "import zio.json.*"
463470
case JsonCodec.Jsoniter =>
464471
"""|import com.github.plokhotnyuk.jsoniter_scala.core.*
465472
|import com.github.plokhotnyuk.jsoniter_scala.macros.*""".stripMargin
@@ -492,7 +499,7 @@ def commonSchemaCodecs(
492499
}
493500
)
494501
.distinct match
495-
case Nil => None
502+
case Nil => None
496503
case props =>
497504
Some(
498505
List(
@@ -631,7 +638,7 @@ def readResources(
631638
resources match
632639
case (k, v) :: xs =>
633640
v.obj.remove("resources").map(_.obj) match
634-
case None => readResources(xs, result.updated(k, read[Resource](v)))
641+
case None => readResources(xs, result.updated(k, read[Resource](v)))
635642
case Some(obj) =>
636643
val newRes = obj.map((a, b) => ResourcePath(k, a) -> b).toList ::: xs
637644
Try(read[Resource](v)) match
@@ -727,8 +734,8 @@ enum SchemaType(val optional: Boolean):
727734
case Primitive("number", _, Some("double" | "float")) => toType("Double")
728735
case Primitive("boolean", _, _) => toType("Boolean")
729736
case Ref(ref, _) => toType(ref.scalaName)
730-
case Array(t, _) => toType(arrayType.toScalaType(t.scalaType(arrayType, enumType)))
731-
case Object(t, _) => toType(s"Map[String, ${t.scalaType(arrayType)}]")
737+
case Array(t, _) => toType(arrayType.toScalaType(t.scalaType(arrayType, enumType)))
738+
case Object(t, _) => toType(s"Map[String, ${t.scalaType(arrayType)}]")
732739
case Enum(_, values, _) =>
733740
enumType match
734741
case SchemaType.EnumType.Literal => toType(values.map(v => v.value).mkString("\"", "\" | \"", "\""))
@@ -777,7 +784,7 @@ object SchemaType:
777784
case _ =>
778785
o.value.get("$ref").map(_.str) match
779786
case Some(ref) => SchemaType.Ref(SchemaPath(ref), optional)
780-
case _ =>
787+
case _ =>
781788
if !o.value.keySet.contains("properties") then
782789
if Set("uploadType", "upload_protocol").exists(context.jsonPath.lastOption.contains) then
783790
""""([\w]+)"""".r
@@ -854,7 +861,7 @@ object Schema:
854861
result: Map[SchemaPath, Schema]
855862
): Map[SchemaPath, Schema] =
856863
schemas match {
857-
case Nil => result
864+
case Nil => result
858865
case (name, data) :: xs =>
859866
val schema = readSchema(name, data)
860867

@@ -864,7 +871,7 @@ object Schema:
864871
case None => Nil
865872
case Some(p) => List((p, p.jsonPath.foldLeft(o)(_.apply(_).obj)))
866873
} match {
867-
case Nil => readSchemas(xs, result.updated(name, schema))
874+
case Nil => readSchemas(xs, result.updated(name, schema))
868875
case nested =>
869876
readSchemas(nested ::: xs, result.updated(name, schema))
870877
}

0 commit comments

Comments
 (0)