Skip to content

Commit

Permalink
remove generation of redundant package imports (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjkl authored Feb 9, 2022
1 parent b175146 commit ba0fa86
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 20 deletions.
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,17 @@ lazy val example = projectMatrix
.settings(
Compile / allowedNamespaces := Seq(
"smithy4s.example",
"smithy4s.example.import_test"
"smithy4s.example.import_test",
"smithy4s.example.imp",
"smithy4s.example.error"
),
smithySpecs := Seq(
(ThisBuild / baseDirectory).value / "sampleSpecs" / "example.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "errors.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "streaming.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "operation.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "import.smithy"
(ThisBuild / baseDirectory).value / "sampleSpecs" / "import.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "importerror.smithy"
),
Compile / resourceDirectory := (ThisBuild / baseDirectory).value / "modules" / "example" / "resources",
isCE3 := true,
Expand Down
8 changes: 4 additions & 4 deletions modules/codegen/src/smithy4s/codegen/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
val params = if (op.input != Type.unit) {
s"input: ${op.input.render}"
} else ""

val opName = op.name
val traitName = s"${serviceName}Operation"
val input =
Expand Down Expand Up @@ -400,9 +399,9 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
block(
s"def liftError(throwable: Throwable) : Option[$errorName] = throwable match"
) {
op.errors.collect { case Type.Ref(ns, name) =>
ns -> s"case e: ${name} => Some($errorName.${name}Case(e))"
} ++ List("" -> "case _ => None")
op.errors.collect { case Type.Ref(_, name) =>
s"case e: ${name} => Some($errorName.${name}Case(e))"
} ++ List("case _ => None")
},
block(
s"def unliftError(e: $errorName) : Throwable = e match"
Expand All @@ -425,6 +424,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
altName.dropWhile(_ == '_').capitalize + "Case"
val caseNames = alts.map(_.name).map(caseName)
val imports = alts.foldMap(_.tpe.imports) ++ syntaxImport

lines(
s"sealed trait $name extends scala.Product with scala.Serializable",
obj(name, ext = shapeTag(name))(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
}
}
}
},
"404": {
"description": "NotFoundError 404 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NotFoundErrorResponseContent"
}
}
}
}
}
}
Expand All @@ -27,6 +37,14 @@
"schemas": {
"ImportOperationOutputPayload": {
"type": "string"
},
"NotFoundErrorResponseContent": {
"type": "object",
"properties": {
"error": {
"type": "string"
}
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions modules/example/src/smithy4s/example/error/NotFoundError.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package smithy4s.example.error

import smithy4s.syntax._

case class NotFoundError(error: Option[String] = None) extends Throwable {
}
object NotFoundError extends smithy4s.ShapeTag.Companion[NotFoundError] {
val id: smithy4s.ShapeId = smithy4s.ShapeId("smithy4s.example.error", "NotFoundError")

val hints : smithy4s.Hints = smithy4s.Hints(
id,
smithy.api.Error.CLIENT,
smithy.api.HttpError(404),
)

val schema: smithy4s.Schema[NotFoundError] = struct(
string.optional[NotFoundError]("error", _.error),
){
NotFoundError.apply
}.withHints(hints)
implicit val staticSchema : schematic.Static[smithy4s.Schema[NotFoundError]] = schematic.Static(schema)
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package smithy4s.example
package smithy4s.example.imp

import ImportServiceGen.ImportOperationError
import smithy4s.example.error.NotFoundError
import smithy4s.example.import_test.OpOutput
import smithy4s.syntax._

trait ImportServiceGen[F[_, _, _, _, _]] {
self =>

def importOperation() : F[Unit, Nothing, OpOutput, Nothing, Nothing]
def importOperation() : F[Unit, ImportOperationError, OpOutput, Nothing, Nothing]

def transform[G[_, _, _, _, _]](transformation : smithy4s.Transformation[F, G]) : ImportServiceGen[G] = new Transformed(transformation)
class Transformed[G[_, _, _, _, _]](transformation : smithy4s.Transformation[F, G]) extends ImportServiceGen[G] {
def importOperation() = transformation[Unit, Nothing, OpOutput, Nothing, Nothing](self.importOperation())
def importOperation() = transformation[Unit, ImportOperationError, OpOutput, Nothing, Nothing](self.importOperation())
}
}

object ImportServiceGen extends smithy4s.Service[ImportServiceGen, ImportServiceOperation] {

def apply[F[_]](implicit F: smithy4s.Monadic[ImportServiceGen, F]): F.type = F

val id: smithy4s.ShapeId = smithy4s.ShapeId("smithy4s.example", "ImportService")
val id: smithy4s.ShapeId = smithy4s.ShapeId("smithy4s.example.imp", "ImportService")

val hints : smithy4s.Hints = smithy4s.Hints(
id,
Expand Down Expand Up @@ -48,8 +50,8 @@ object ImportServiceGen extends smithy4s.Service[ImportServiceGen, ImportService
case ImportOperation() => impl.importOperation()
}
}
case class ImportOperation() extends ImportServiceOperation[Unit, Nothing, OpOutput, Nothing, Nothing]
object ImportOperation extends smithy4s.Endpoint[ImportServiceOperation, Unit, Nothing, OpOutput, Nothing, Nothing] {
case class ImportOperation() extends ImportServiceOperation[Unit, ImportOperationError, OpOutput, Nothing, Nothing]
object ImportOperation extends smithy4s.Endpoint[ImportServiceOperation, Unit, ImportOperationError, OpOutput, Nothing, Nothing] with smithy4s.Errorable[ImportOperationError] {
val id: smithy4s.ShapeId = smithy4s.ShapeId("smithy4s.example.import_test", "ImportOperation")
val input: smithy4s.Schema[Unit] = unit.withHints(smithy4s.internals.InputOutput.Input)
val output: smithy4s.Schema[OpOutput] = OpOutput.schema.withHints(smithy4s.internals.InputOutput.Output)
Expand All @@ -60,6 +62,38 @@ object ImportServiceGen extends smithy4s.Service[ImportServiceGen, ImportService
smithy.api.Http(smithy.api.NonEmptyString("GET"), smithy.api.NonEmptyString("/test"), Some(200)),
)
def wrap(input: Unit) = ImportOperation()
override val errorable: Option[smithy4s.Errorable[ImportOperationError]] = Some(this)
val error: smithy4s.errorUnion.Schema[ImportOperationError] = ImportOperationError.schema
def liftError(throwable: Throwable) : Option[ImportOperationError] = throwable match {
case e: NotFoundError => Some(ImportOperationError.NotFoundErrorCase(e))
case _ => None
}
def unliftError(e: ImportOperationError) : Throwable = e match {
case ImportOperationError.NotFoundErrorCase(e) => e
}
}
sealed trait ImportOperationError extends scala.Product with scala.Serializable
object ImportOperationError extends smithy4s.ShapeTag.Companion[ImportOperationError] {
val id: smithy4s.ShapeId = smithy4s.ShapeId("smithy4s.example.imp", "ImportOperationError")

val hints : smithy4s.Hints = smithy4s.Hints(
id,
)

case class NotFoundErrorCase(notFoundError: NotFoundError) extends ImportOperationError

object NotFoundErrorCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[NotFoundErrorCase] = bijection(NotFoundError.schema, NotFoundErrorCase(_), _.notFoundError)
val alt = schema.oneOf[ImportOperationError]("NotFoundError")
}

val schema: smithy4s.errorUnion.Schema[ImportOperationError] = errors(
NotFoundErrorCase.alt,
){
case c : NotFoundErrorCase => NotFoundErrorCase.alt(c)
}
implicit val staticSchema : schematic.Static[smithy4s.Schema[ImportOperationError]] = schematic.Static(schema)
}
}

Expand Down
12 changes: 12 additions & 0 deletions modules/example/src/smithy4s/example/imp/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package smithy4s.example

package object imp {
type ImportService[F[_]] = smithy4s.Monadic[ImportServiceGen, F]
object ImportService extends smithy4s.Service.Provider[ImportServiceGen, ImportServiceOperation] {
def apply[F[_]](implicit F: ImportService[F]): F.type = F
def service : smithy4s.Service[ImportServiceGen, ImportServiceOperation] = ImportServiceGen
val id: smithy4s.ShapeId = service.id
}


}
6 changes: 0 additions & 6 deletions modules/example/src/smithy4s/example/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ package object example {
def service : smithy4s.Service[FooServiceGen, FooServiceOperation] = FooServiceGen
val id: smithy4s.ShapeId = service.id
}
type ImportService[F[_]] = smithy4s.Monadic[ImportServiceGen, F]
object ImportService extends smithy4s.Service.Provider[ImportServiceGen, ImportServiceOperation] {
def apply[F[_]](implicit F: ImportService[F]): F.type = F
def service : smithy4s.Service[ImportServiceGen, ImportServiceOperation] = ImportServiceGen
val id: smithy4s.ShapeId = service.id
}
type ObjectService[F[_]] = smithy4s.Monadic[ObjectServiceGen, F]
object ObjectService extends smithy4s.Service.Provider[ObjectServiceGen, ObjectServiceOperation] {
def apply[F[_]](implicit F: ObjectService[F]): F.type = F
Expand Down
6 changes: 4 additions & 2 deletions sampleSpecs/import.smithy
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace smithy4s.example
namespace smithy4s.example.imp

use smithy4s.api#simpleRestJson
use smithy4s.example.import_test#ImportOperation
use smithy4s.example.error#NotFoundError

@simpleRestJson
service ImportService {
version: "1.0.0",
operations: [ImportOperation]
operations: [ImportOperation],
errors: [NotFoundError],
}

7 changes: 7 additions & 0 deletions sampleSpecs/importerror.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace smithy4s.example.error

@error("client")
@httpError(404)
structure NotFoundError {
error: String,
}

0 comments on commit ba0fa86

Please sign in to comment.