Skip to content

Commit

Permalink
fix: scala emitter for playground (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrevanveluw authored Nov 7, 2024
1 parent 06f9886 commit 6e7d6d8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package community.flock.wirespec.compiler.core.emit

import community.flock.wirespec.compiler.core.addBackticks
import community.flock.wirespec.compiler.core.emit.common.DEFAULT_GENERATED_PACKAGE_STRING
import community.flock.wirespec.compiler.core.emit.common.DEFAULT_SHARED_PACKAGE_STRING
import community.flock.wirespec.compiler.core.emit.common.DefinitionModelEmitter
import community.flock.wirespec.compiler.core.emit.common.Emitted
import community.flock.wirespec.compiler.core.emit.common.Emitter
Expand All @@ -27,22 +28,37 @@ open class ScalaEmitter(
logger: Logger = noLogger
) : DefinitionModelEmitter, Emitter(logger) {

open val import = """
|
|import $DEFAULT_SHARED_PACKAGE_STRING.scala.Wirespec
|
""".trimMargin()

override fun Definition.emitName(): String = when (this) {
is Endpoint -> "${identifier.emit(Field)}Endpoint"
is Channel -> "${identifier.emit(Field)}Channel"
is Enum -> identifier.emit(Field)
is Refined -> identifier.emit(Field)
is Type -> identifier.emit(Field)
is Union -> identifier.emit(Field)
is Endpoint -> "${identifier.emit(Class)}Endpoint"
is Channel -> "${identifier.emit(Class)}Channel"
is Enum -> identifier.emit(Class)
is Refined -> identifier.emit(Class)
is Type -> identifier.emit(Class)
is Union -> identifier.emit(Class)
}

override fun notYetImplemented() =
"""// TODO("Not yet implemented")
|
""".trimMargin()

override fun emit(ast: AST): List<Emitted> = super.emit(ast)
.map { Emitted(it.typeName, if (packageName.isBlank()) "" else "package $packageName\n\n${it.result}") }
override fun emit(ast: AST): List<Emitted> =
super.emit(ast).map { (typeName, result) ->
Emitted(
typeName = typeName,
result = """
|${if (packageName.isBlank()) "" else "package $packageName"}
|${if (ast.needImports()) import else ""}
|${result}
""".trimMargin().trimStart()
)
}

override fun emit(type: Type, ast: AST) = """
|case class ${type.emitName()}(
Expand Down Expand Up @@ -78,7 +94,7 @@ open class ScalaEmitter(
fun String.sanitize() = replace("-", "_").let { if (it.first().isDigit()) "_$it" else it }
"""
|sealed abstract class ${emitName()}(val label: String)
|object ${identifier.emit(Field)} {
|object ${identifier.emit(Class)} {
|${entries.joinToString("\n") { """${Spacer}final case object ${it.sanitize().uppercase()} extends ${identifier.emit(Class)}(label = "$it")""" }}
|}
|""".trimMargin()
Expand All @@ -91,7 +107,6 @@ open class ScalaEmitter(
|${Spacer}}
|}
|
|
""".trimMargin()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class CompileEnumTest {
val scala = """
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.scala.Wirespec
|
|sealed abstract class MyAwesomeEnum(val label: String)
|object MyAwesomeEnum {
| final case object ONE extends MyAwesomeEnum(label = "ONE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ class CompileFullEndpointTest {
val scala = """
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.scala.Wirespec
|
|// TODO("Not yet implemented")
|
|case class PotentialTodoDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ class CompileMinimalEndpointTest {
val scala = """
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.scala.Wirespec
|
|// TODO("Not yet implemented")
|
|case class TodoDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ class CompileRefinedTest {
val scala = """
|package community.flock.wirespec.generated
|
|import community.flock.wirespec.scala.Wirespec
|
|case class TodoId(val value: String) {
| implicit class TodoIdOps(val that: TodoId) {
| val regex = new scala.util.matching.Regex(""${'"'}^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}${'$'}""${'"'})
| regex.findFirstIn(that.value)
| }
|}
|
|
""".trimMargin()

compiler(ScalaEmitter()) shouldBeRight scala
Expand Down

0 comments on commit 6e7d6d8

Please sign in to comment.