Skip to content

Commit cde4f68

Browse files
authored
add/parametrize endpoints, some refactoring (#8)
* add/parametrize endpoints, some refactoring * fix codegen task in ci * revert package name change * add exit code to command line process
1 parent 901a05f commit cde4f68

File tree

5 files changed

+154
-75
lines changed

5 files changed

+154
-75
lines changed

README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,29 @@
33
![Maven Central Version](https://img.shields.io/maven-central/v/dev.rolang/gcp-codegen_3)
44
[![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.rolang/gcp-codegen_3.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/rolang/gcp-codegen_3/)
55

6-
Generates client code from Google's [disovery document](https://developers.google.com/discovery/v1/using).
7-
Currently generates code for Scala 3.
6+
⚠️ _This project is in an experimental stage (with a messy code base), use with care if you want to give it a try_.
7+
8+
Generates client code from Google's [disovery document](https://developers.google.com/discovery/v1/using) for your Scala (3) tech stack.
9+
Currently it provides following configurations for generated code:
10+
- Http sources: [Sttp4](https://sttp.softwaremill.com/en/latest), [Sttp3](https://sttp.softwaremill.com/en/stable)
11+
- JSON codecs: [Jsoniter](https://github.com/plokhotnyuk/jsoniter-scala), [ZioJson](https://zio.dev/zio-json)
12+
- JSON Array collection type: `List`, `Vector`, `Array`, `ZioChunk`
13+
14+
__NOTE__: Generated code does not include authentication.
15+
16+
If you're using [http4s](https://github.com/http4s/http4s) and [circe](https://github.com/circe/circe) you may also want to check some similar projects:
17+
- [hamnis/google-discovery-scala](https://github.com/hamnis/google-discovery-scala)
18+
- [armanbilge/gcp4s](https://github.com/armanbilge/gcp4s)
819

920
### Usage
1021

22+
The generator can be used with any tool that can perform system calls to a command line executable or add Scala 3 dependencies (e.g. [scala command line](https://scala-cli.virtuslab.org/), [sbt 1](https://www.scala-sbt.org/1.x/docs/), [sbt 2](https://www.scala-sbt.org/2.x/docs/en/index.html), [mill](https://mill-build.org), etc.).
23+
1124
#### Usage via Scala command line example
1225
See example under [example/generate.scala](./example/generate.scala).
1326

1427
```scala
15-
//> using scala 3.6.2
28+
//> using scala 3.6.3
1629
//> using dep dev.rolang::gcp-codegen::0.0.1
1730

1831
import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
@@ -22,8 +35,7 @@ import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
2235
specsInput = SpecsInput.FilePath(Path.of("pubsub_v1.json")),
2336
config = GeneratorConfig(
2437
outDir = Path.of("out"),
25-
resourcesPkg = "example.pubsub.v1.resource",
26-
schemasPkg = "example.pubsub.v1.schemas",
38+
outPkg = "example.pubsub.v1",
2739
httpSource = HttpSource.Sttp4,
2840
jsonCodec = JsonCodec.Jsoniter,
2941
dialect = Dialect.Scala3,
@@ -35,7 +47,7 @@ import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
3547
```
3648
Run example:
3749
```shell
38-
cd example && scala generate.scala
50+
cd example && scala generate.scala && scala fmt ./out
3951
```
4052
See output in `example/out`.
4153

@@ -45,14 +57,13 @@ See output in `example/out`.
4557

4658
| Configuration | Description | Options | Default |
4759
| ------------------- | ---------------- | ------- | --- |
48-
| --out-dir | Ouput directory | | |
49-
| --specs | Can be `stdin` or a path to the JSON file. | | |
50-
| --resources-pkg | Target package for generated resources | | |
51-
| --schemas-pkg | Target package for generated schemas | | |
52-
| --http-source | Generated http source. | [Sttp4](https://sttp.softwaremill.com/en/latest), [Sttp3](https://sttp.softwaremill.com/en/stable) | |
53-
| --json-codec | Generated JSON codec | [Jsoniter](https://github.com/plokhotnyuk/jsoniter-scala), [ZioJson](https://zio.dev/zio-json) | |
54-
| --array-type | Collection type for JSON arrays | `List`, `Vector`, `Array`, `ZioChunk` | `List` |
55-
| --include-resources | Optional resource filter. | | |
60+
| -specs | Can be `stdin` or a path to the JSON file. | | |
61+
| -out-dir | Ouput directory | | |
62+
| -out-pkg | Output package | | |
63+
| -http-source | Generated http source. | [Sttp4](https://sttp.softwaremill.com/en/latest), [Sttp3](https://sttp.softwaremill.com/en/stable) | |
64+
| -json-codec | Generated JSON codec | [Jsoniter](https://github.com/plokhotnyuk/jsoniter-scala), [ZioJson](https://zio.dev/zio-json) | |
65+
| -array-type | Collection type for JSON arrays | `List`, `Vector`, `Array`, `ZioChunk` | `List` |
66+
| -include-resources | Optional resource filter. | | |
5667

5768
##### Examples:
5869

build.sbt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +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.3.4"
6-
ThisBuild / scalafmt := true
7-
ThisBuild / scalafmtSbtCheck := true
5+
ThisBuild / scalaVersion := "3.3.5"
86
ThisBuild / version ~= { v => if (v.contains('+')) s"${v.replace('+', '-')}-SNAPSHOT" else v }
97
ThisBuild / scmInfo := Some(
108
ScmInfo(
@@ -46,7 +44,7 @@ lazy val noPublish = Seq(
4644
publish / skip := true
4745
)
4846

49-
lazy val sttpClient4Version = "4.0.0-M22"
47+
lazy val sttpClient4Version = "4.0.0-RC1"
5048

5149
lazy val sttpClient3Version = "3.10.2"
5250

@@ -78,7 +76,7 @@ lazy val core = crossProject(JVMPlatform, NativePlatform)
7876
)
7977
.settings(
8078
libraryDependencies ++= Seq(
81-
"com.lihaoyi" %%% "upickle" % "4.0.2"
79+
"com.lihaoyi" %%% "upickle" % "4.1.0"
8280
)
8381
)
8482

@@ -90,7 +88,8 @@ lazy val cli = project
9088
.settings(publishSettings)
9189
.settings(
9290
name := "gcp-codegen-cli",
93-
moduleName := "gcp-codegen-cli"
91+
moduleName := "gcp-codegen-cli",
92+
nativeConfig := nativeConfig.value.withMultithreading(false)
9493
)
9594

9695
def dependencyByConfig(httpSource: String, jsonCodec: String, arrayType: String): List[ModuleID] = {
@@ -219,13 +218,12 @@ def codegenTask(
219218
val cmd =
220219
List(
221220
s"./${cliBin.getPath()}",
222-
s"--specs=${resourcePath(s"${apiName}_$apiVersion.json")}",
223-
s"--resources-pkg=$basePkgName.resources",
224-
s"--schemas-pkg=$basePkgName.schemas",
225-
s"--out-dir=${outSrcDir.getPath()}",
226-
s"--http-source=$httpSource",
227-
s"--json-codec=$jsonCodec",
228-
s"--array-type=$arrayType"
221+
s"-specs=${resourcePath(s"${apiName}_$apiVersion.json")}",
222+
s"-out-dir=${outSrcDir.getPath()}",
223+
s"-out-pkg=$basePkgName",
224+
s"-http-source=$httpSource",
225+
s"-json-codec=$jsonCodec",
226+
s"-array-type=$arrayType"
229227
).mkString(" ")
230228

231229
cmd ! ProcessLogger(l => logger.info(l))

example/generate.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//> using scala 3.6.2
2-
//> using dep dev.rolang::gcp-codegen::0.0.1
1+
//> using scala 3.6.3
2+
//> using dep dev.rolang::gcp-codegen::0.0.2
33

44
import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
55

@@ -8,8 +8,7 @@ import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
88
specsInput = SpecsInput.FilePath(Path.of("pubsub_v1.json")),
99
config = GeneratorConfig(
1010
outDir = Path.of("out"),
11-
resourcesPkg = "example.pubsub.v1.resource",
12-
schemasPkg = "example.pubsub.v1.schemas",
11+
outPkg = "example.pubsub.v1",
1312
httpSource = HttpSource.Sttp4,
1413
jsonCodec = JsonCodec.Jsoniter,
1514
dialect = Dialect.Scala3,

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// for test runs using scala-cli
2+
//> using jvm system
3+
//> using scala 3.6.3
4+
//> using file ../../../../core/shared/src/main/scala/codegen.scala
5+
//> using dep com.lihaoyi::upickle:4.1.0
6+
17
package gcp.codegen.cli
28

39
import gcp.codegen.*
@@ -11,13 +17,19 @@ import scala.util.{Failure, Success}
1117

1218
@main def run(args: String*) =
1319
argsToTask(args) match
14-
case Left(err) => Console.err.println(s"Invalid arguments: $err")
20+
case Left(err) =>
21+
Console.err.println(s"Invalid arguments: $err")
22+
sys.exit(1)
1523
case Right(task) =>
1624
Await
1725
.ready(task.run, 30.seconds)
1826
.onComplete {
19-
case Failure(exception) => Console.err.println(s"Failure: ${exception.printStackTrace()}")
20-
case Success(files) => println(s"Generated ${files.length} files")
27+
case Failure(exception) =>
28+
Console.err.println(s"Failure: ${exception.printStackTrace()}")
29+
sys.exit(1)
30+
case Success(files) =>
31+
println(s"Generated ${files.length} files")
32+
sys.exit(0)
2133
}
2234

2335
private def argsToTask(args: Seq[String]): Either[String, Task] =
@@ -31,41 +43,39 @@ private def argsToTask(args: Seq[String]): Either[String, Task] =
3143

3244
for {
3345
outDir <- argsMap
34-
.get("--out-dir")
46+
.get("-out-dir")
3547
.map(p => Path.of(p))
36-
.toRight("Missing --out-dir")
48+
.toRight("Missing -out-dir")
3749
specs <- argsMap
38-
.get("--specs")
50+
.get("-specs")
3951
.map {
4052
case "stdin" => SpecsInput.StdIn
4153
case p => SpecsInput.FilePath(Path.of(p))
4254
}
43-
.toRight("Missing --specs")
44-
resourcesPkg <- argsMap
45-
.get("--resources-pkg")
46-
.toRight("Missing --resources-pkg")
47-
schemasPkg <- argsMap.get("--schemas-pkg").toRight("Missing --schemas-pkg")
55+
.toRight("Missing -specs")
56+
outPkg <- argsMap
57+
.get("-out-pkg")
58+
.toRight("Missing -out-pkg")
4859
httpSource <- argsMap
49-
.get("--http-source")
60+
.get("-http-source")
5061
.flatMap(v => HttpSource.values.find(_.toString().equalsIgnoreCase(v)))
51-
.toRight("Missing or invalid --http-source")
62+
.toRight("Missing or invalid -http-source")
5263
jsonCodec <- argsMap
53-
.get("--json-codec")
64+
.get("-json-codec")
5465
.flatMap(v => JsonCodec.values.find(_.toString().equalsIgnoreCase(v)))
55-
.toRight("Missing or invalid --json-codec")
56-
arrayType <- argsMap.get("--array-type") match
66+
.toRight("Missing or invalid -json-codec")
67+
arrayType <- argsMap.get("-array-type") match
5768
case None => Right(ArrayType.List)
5869
case Some(v) => ArrayType.values.find(_.toString().equalsIgnoreCase(v)).toRight(s"Invalid array-type $v")
5970
incResources = argsMap
60-
.get("--include-resources")
71+
.get("-include-resources")
6172
.toList
6273
.flatMap(_.split(',').toList)
6374
} yield Task(
6475
specsInput = specs,
6576
config = GeneratorConfig(
6677
outDir = outDir,
67-
resourcesPkg = resourcesPkg,
68-
schemasPkg = schemasPkg,
78+
outPkg = outPkg,
6979
httpSource = httpSource,
7080
jsonCodec = jsonCodec,
7181
dialect = Dialect.Scala3,

0 commit comments

Comments
 (0)