Generates client code from Google's disovery document for your Scala (3) tech stack.
Currently it provides following configurations for generated code:
- Http sources: Sttp4, Sttp3
- JSON codecs: Jsoniter, ZioJson
- JSON Array collection type:
List
,Vector
,Array
,ZioChunk
NOTE: Generated code does not include authentication.
If you're using http4s and circe you may also want to check some similar projects:
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, sbt 1, sbt 2, mill, etc.).
See example under example/generate.scala.
//> using scala 3.6.3
//> using dep dev.rolang::gcp-codegen::0.0.1
import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
@main def run =
val files = Task(
specsInput = SpecsInput.FilePath(Path.of("pubsub_v1.json")),
config = GeneratorConfig(
outDir = Path.of("out"),
outPkg = "example.pubsub.v1",
httpSource = HttpSource.Sttp4,
jsonCodec = JsonCodec.Jsoniter,
dialect = Dialect.Scala3,
arrayType = ArrayType.List,
preprocess = specs => specs
)
).runAwait()
println(s"Generated ${files.length} files")
Run example:
cd example && scala generate.scala && scala fmt ./out
See output in example/out
.
Configuration | Description | Options | Default |
---|---|---|---|
-specs | Can be stdin or a path to the JSON file. |
||
-out-dir | Ouput directory | ||
-out-pkg | Output package | ||
-http-source | Generated http source. | Sttp4, Sttp3 | |
-json-codec | Generated JSON codec | Jsoniter, ZioJson | |
-array-type | Collection type for JSON arrays | List , Vector , Array , ZioChunk |
List |
-include-resources | Optional resource filter. |
Command line binaries are not published (not yet).
A command line binary can be built from the source via sbt buildCliBinary
.
The output directory is modules/cli/target/bin
.
E.g. on Linux the output file will be like modules/cli/target/bin/gcp-codegen-x86_64-linux
.
curl 'https://pubsub.googleapis.com/$discovery/rest?version=v1' > pubsub_v1.json
# the path to the executable may be different
./modules/cli/target/bin/gcp-codegen-x86_64-linux \
--out-dir=src/scala/main/generated \
--specs=./pubsub_v1.json \
--resources-pkg=gcp.pubsub.v1.resources \
--schemas-pkg=gcp.pubsub.v1.schemas \
--http-source=sttp4 \
--json-codec=jsoniter \
--include-resources='projects.*,!projects.snapshots' # optional filters
Generate, compile and run tests for generated code
sbt buildCliBinary test