Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import gcp.codegen.*, java.nio.file.*, GeneratorConfig.*
outPkg = "example.pubsub.v1",
httpSource = HttpSource.Sttp4,
jsonCodec = JsonCodec.Jsoniter,
dialect = Dialect.Scala3,
arrayType = ArrayType.List,
preprocess = specs => specs
)
Expand Down
1 change: 0 additions & 1 deletion modules/cli/src/main/scala/cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private def argsToTask(args: Seq[String]): Either[String, Task] =
outPkg = outPkg,
httpSource = httpSource,
jsonCodec = jsonCodec,
dialect = Dialect.Scala3,
preprocess = s => {
incResources.partitionMap(s => if s.startsWith("!") then Left(s.stripPrefix("!")) else Right(s)) match
case (Nil, Nil) => s
Expand Down
57 changes: 6 additions & 51 deletions modules/core/shared/src/main/scala/codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ case class GeneratorConfig(
httpSource: HttpSource,
jsonCodec: JsonCodec,
arrayType: ArrayType,
dialect: Dialect,
preprocess: Specs => Specs = s => s
)

object GeneratorConfig:
enum HttpSource:
case Sttp4, Sttp3
case Sttp4

enum JsonCodec:
case ZioJson, Jsoniter
Expand All @@ -43,10 +42,6 @@ object GeneratorConfig:
case ZioChunk => s"zio.Chunk[$t]"
}

// potential dialect options
enum Dialect:
case Scala3

enum SpecsInput:
case StdIn
case FilePath(path: Path)
Expand Down Expand Up @@ -103,12 +98,10 @@ def generateBySpec(
List(
s"${toComment(List(s"${specs.title} ${specs.version}", specs.description, specs.documentationLink))}",
"",
config.dialect match
case Dialect.Scala3 => s"package ${config.outPkg}",
s"package ${config.outPkg}",
"",
config.httpSource match {
case HttpSource.Sttp4 => "import sttp.model.*\nimport sttp.client4.*"
case HttpSource.Sttp3 => "import sttp.model.*\nimport sttp.client3.*"
},
"",
s"""val baseUrl: Uri = uri"${specs.baseUrl}"""",
Expand Down Expand Up @@ -150,24 +143,17 @@ def generateBySpec(
Files.writeString(
path,
List(
config.dialect match
case Dialect.Scala3 => s"package $resourcesPkg",
s"package $resourcesPkg",
"",
config.httpSource match {
case HttpSource.Sttp4 =>
"import sttp.model.*\nimport sttp.client4.*, sttp.client4.ResponseException.{DeserializationException, UnexpectedStatusCode}"
case HttpSource.Sttp3 => "import sttp.model.*\nimport sttp.client3.*"
},
config.jsonCodec match {
case JsonCodec.ZioJson => "import zio.json.*"
case JsonCodec.Jsoniter => "import com.github.plokhotnyuk.jsoniter_scala.core.*"
},
config.dialect match
case Dialect.Scala3 => "",
s"val resourceRequest: ${
if config.httpSource == HttpSource.Sttp3 then "RequestT[Empty, Either[String, String], Any]"
else "PartialRequest[Either[String, String]]"
} = basicRequest.headers(Header.contentType(MediaType.ApplicationJson))",
s"val resourceRequest: PartialRequest[Either[String, String]] = basicRequest.headers(Header.contentType(MediaType.ApplicationJson))",
"",
s"export ${config.outPkg}.QueryParameters",
"",
Expand All @@ -184,29 +170,11 @@ def generateBySpec(
| }
| else Left(UnexpectedStatusCode(String(bytes, java.nio.charset.StandardCharsets.UTF_8), metadata))
| )""".stripMargin
case (HttpSource.Sttp3, JsonCodec.Jsoniter) =>
"""|def asJson[T : JsonValueCodec]: ResponseAs[Either[ResponseException[String, Exception], T], Any] =
| asByteArrayAlways.mapWithMetadata((bytes, metadata) =>
| if metadata.isSuccess then
| try {
| Right(readFromArray[T](bytes))
| } catch {
| case e: Exception =>
| Left(DeserializationException(String(bytes, java.nio.charset.StandardCharsets.UTF_8), e))
| }
| else Left(HttpError(String(bytes, java.nio.charset.StandardCharsets.UTF_8), metadata.code))
| )""".stripMargin
case (HttpSource.Sttp4, JsonCodec.ZioJson) =>
"""|def asJson[T : JsonDecoder]: ResponseAs[Either[ResponseException[String], T]] =
| asStringAlways.mapWithMetadata((body, metadata) =>
| if metadata.isSuccess then body.fromJson[T].left.map(e => DeserializationException(body, Exception(e), metadata))
| else Left(UnexpectedStatusCode(body, metadata))
| )""".stripMargin
case (HttpSource.Sttp3, JsonCodec.ZioJson) =>
"""|def asJson[T : JsonDecoder]: ResponseAs[Either[ResponseException[String, Exception], T], Any] =
| asStringAlways.mapWithMetadata((body, metadata) =>
| if metadata.isSuccess then body.fromJson[T].left.map(e => DeserializationException(body, Exception(e)))
| else Left(HttpError(body, metadata.code))
| )""".stripMargin,
"",
config.httpSource match
Expand All @@ -216,12 +184,6 @@ def generateBySpec(
| if metadata.isSuccess then Right(body)
| else Left(UnexpectedStatusCode(body, metadata))
| )""".stripMargin
case HttpSource.Sttp3 =>
"""|def asEmptyResponse: ResponseAs[Either[ResponseException[String, Exception], String], Any] =
| asStringAlways.mapWithMetadata((body, metadata) =>
| if metadata.isSuccess then Right(body)
| else Left(HttpError(body, metadata.code))
| )""".stripMargin
).mkString("\n")
)
List(path.toFile())
Expand Down Expand Up @@ -272,7 +234,6 @@ def generateBySpec(
schema = schema,
pkg = schemasPkg,
jsonCodec = config.jsonCodec,
dialect = config.dialect,
hasProps = p => specs.hasProps(p),
arrType = config.arrayType,
commonCodecsPkg =
Expand Down Expand Up @@ -320,7 +281,6 @@ def resourceCode(
) =
val sttpClientPkg = httpSource match
case HttpSource.Sttp4 => "sttp.client4"
case HttpSource.Sttp3 => "sttp.client3"

List(
s"package $pkg",
Expand Down Expand Up @@ -414,8 +374,6 @@ def resourceCode(
httpSource match
case HttpSource.Sttp4 =>
s"Request[Either[ResponseException[String], $t]]"
case HttpSource.Sttp3 =>
s"RequestT[Identity, Either[ResponseException[String, Exception], $t], Any]"

val (resType, mapResponse) = method.response match
case Some(r) if r.schemaPath.forall(hasProps) =>
Expand All @@ -440,7 +398,6 @@ def schemasCode(
schema: Schema,
pkg: String,
jsonCodec: JsonCodec,
dialect: Dialect,
hasProps: SchemaPath => Boolean,
arrType: ArrayType,
commonCodecsPkg: Option[String]
Expand Down Expand Up @@ -505,10 +462,8 @@ def schemasCode(
|import com.github.plokhotnyuk.jsoniter_scala.macros.*""".stripMargin
},
commonCodecsPkg match
case Some(codecsPkg) =>
dialect match
case Dialect.Scala3 => s"import $codecsPkg.given"
case _ => "",
case Some(codecsPkg) => s"import $codecsPkg.given"
case _ => "",
toSchemaClass(schema)
).mkString("\n")
}
Expand Down