Skip to content

Commit

Permalink
Having schema rendering hold the imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Baccata committed Jun 27, 2022
1 parent 7324f0b commit c51f650
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
60 changes: 22 additions & 38 deletions modules/codegen/src/smithy4s/codegen/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
line"self =>",
newline,
ops.map { op =>
line"def ${op.methodName}(${op
.renderArgs(hideNewtypeImports = true)}) : F[${op.renderAlgParams}]"
line"def ${op.methodName}(${op.renderArgs}) : F[${op.renderAlgParams}]"

},
newline,
Expand All @@ -165,7 +164,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
) {
ops.map { op =>
val opName = op.methodName
line"def $opName(${op.renderArgs(hideNewtypeImports = true)}) = transformation[${op.renderAlgParams}](self.$opName(${op.renderParams}))"
line"def $opName(${op.renderArgs}) = transformation[${op.renderAlgParams}](self.$opName(${op.renderParams}))"
}
}
),
Expand Down Expand Up @@ -204,15 +203,11 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
block(s"object reified extends $genName[$opTraitName]") {
ops.map {
case op if op.input == Type.unit =>
line"def ${op.methodName}(${op
.renderArgs(hideNewtypeImports = true)}) = ${op.name}()"
line"def ${op.methodName}(${op.renderArgs}) = ${op.name}()"
case op if op.hints.contains(Hint.PackedInputs) =>
line"def ${op.methodName}(${op
.renderArgs(hideNewtypeImports = true)}) = ${op.name}(input)"
line"def ${op.methodName}(${op.renderArgs}) = ${op.name}(input)"
case op =>
line"def ${op.methodName}(${op.renderArgs(hideNewtypeImports =
true
)}) = ${op.name}(${op.input}(${op.renderParams}))"
line"def ${op.methodName}(${op.renderArgs}) = ${op.name}(${op.input}(${op.renderParams}))"
}
},
newline,
Expand Down Expand Up @@ -543,28 +538,17 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
).addImports(imports)
}

private def fieldToRenderLine(
field: Field,
hideNewtypeImports: Boolean
): Line = {
private def fieldToRenderLine(field: Field): Line = {
field match {
case Field(name, _, tpe, required, _) =>
val result = line"$tpe"
line"$tpe"
.modify(line =>
name + ": " + { if (required) line else s"Option[$line] = None" }
)
tpe match {
case _: Type.Alias if hideNewtypeImports =>
result.copy(imports = Set.empty)
case _ => result
}
}
}
private def renderArgs(
fields: List[Field],
hideNewtypeImports: Boolean = false
): Line = fields
.map(fieldToRenderLine(_, hideNewtypeImports))
private def renderArgs(fields: List[Field]): Line = fields
.map(fieldToRenderLine)
.intercalate(Line.comma)

private def renderEnum(
Expand Down Expand Up @@ -620,12 +604,11 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
}

private implicit class OperationExt(op: Operation) {
def renderArgs: Line = renderArgs(hideNewtypeImports = false)
def renderArgs(hideNewtypeImports: Boolean) =
def renderArgs =
if (op.input == Type.unit) Line.empty
else if (op.hints.contains(Hint.PackedInputs)) {
line"input: ${op.input}"
} else self.renderArgs(op.params, hideNewtypeImports)
} else self.renderArgs(op.params)

def renderParams: Line =
if (op.input == Type.unit) Line.empty
Expand All @@ -648,16 +631,17 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
}

implicit class TypeExt(tpe: Type) {
def schemaRef: String = tpe match {
case Type.PrimitiveType(p) => schemaRefP(p)
case Type.List(member) => s"list(${member.schemaRef})"
case Type.Set(member) => s"set(${member.schemaRef})"
case Type.Map(key, value) => s"map(${key.schemaRef}, ${value.schemaRef})"
case Type.Alias(_, name, Type.PrimitiveType(_)) =>
s"$name.schema"
case Type.Alias(_, name, _) =>
s"$name.underlyingSchema"
case Type.Ref(_, name) => s"$name.schema"
def schemaRef: Line = tpe match {
case Type.PrimitiveType(p) => Line(schemaRefP(p))
case Type.List(member) => line"list(${member.schemaRef})"
case Type.Set(member) => line"set(${member.schemaRef})"
case Type.Map(key, value) =>
line"map(${key.schemaRef}, ${value.schemaRef})"
case Type.Alias(ns, name, Type.PrimitiveType(_)) =>
line"$name.schema".addImport(ns + "." + name)
case Type.Alias(ns, name, _) =>
line"$name.underlyingSchema".addImport(ns + "." + name)
case Type.Ref(ns, name) => line"$name.schema".addImport(ns + "." + name)
}

private def schemaRefP(primitive: Primitive): String = primitive match {
Expand Down
5 changes: 3 additions & 2 deletions modules/codegen/src/smithy4s/codegen/ToLine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ object ToLine {
Line(kimports ++ vimports, s"Map[${k.mkString("")}, ${v.mkString("")}]")
case Type.Alias(ns, name, Type.PrimitiveType(_)) =>
Line(Set(s"$ns.$name"), name)
case Type.Alias(ns, name, aliased) =>
case Type.Alias(_, _, aliased) =>
val (imports, t) = render(aliased).tupled
Line(imports + s"$ns.$name", t)
Line(imports, t)
case Type.Ref(namespace, name) =>
val imports = Set(s"$namespace.$name")
Line(imports, name)
Expand Down Expand Up @@ -78,6 +78,7 @@ object ToLine {
case class Line(imports: Set[String], line: String) {
def tupled = (imports, line)
def suffix(suffix: Line) = modify(s => s"$s $suffix")
def addImport(imp: String) = copy(imports = imports + imp)
def modify(f: String => String) = Line(imports, f(line))
def nonEmpty = line.nonEmpty
def toLines = Lines(imports, List(line))
Expand Down

0 comments on commit c51f650

Please sign in to comment.