Skip to content

Commit

Permalink
Generated enums can optionally extend a module
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Sep 23, 2023
1 parent 8a9409e commit 7cd3ea3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,24 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
file,
delimiter,
rowFilter,
asEnum = true
asEnum = true,
extendsFrom = None
)
)

/** Embed the data as a Scala 3 Enum that extends some fully qualified module name. E.g. `com.example.MyData`. */
def asEnum(moduleName: String, file: os.Path, extendsFrom: String): IndigoGenerators =
gens.copy(
sources = sources ++
EmbedData.generate(
outDirectory,
moduleName,
fullyQualifiedPackageName,
file,
delimiter,
rowFilter,
asEnum = true,
extendsFrom = Option(extendsFrom)
)
)

Expand All @@ -316,7 +333,24 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
os.Path(file),
delimiter,
rowFilter,
asEnum = true
asEnum = true,
extendsFrom = None
)
)

/** Embed the data as a Scala 3 Enum that extends some fully qualified module name. E.g. `com.example.MyData`. */
def asEnum(moduleName: String, file: File, extendsFrom: String): IndigoGenerators =
gens.copy(
sources = sources ++
EmbedData.generate(
outDirectory,
moduleName,
fullyQualifiedPackageName,
os.Path(file),
delimiter,
rowFilter,
asEnum = true,
extendsFrom = Option(extendsFrom)
)
)

Expand All @@ -331,7 +365,24 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
os.RelPath(file).resolveFrom(os.pwd),
delimiter,
rowFilter,
asEnum = true
asEnum = true,
extendsFrom = None
)
)

/** Embed the data as a Scala 3 Enum that extends some fully qualified module name. E.g. `com.example.MyData`. */
def asEnum(moduleName: String, file: String, extendsFrom: String): IndigoGenerators =
gens.copy(
sources = sources ++
EmbedData.generate(
outDirectory,
moduleName,
fullyQualifiedPackageName,
os.RelPath(file).resolveFrom(os.pwd),
delimiter,
rowFilter,
asEnum = true,
extendsFrom = Option(extendsFrom)
)
)

Expand All @@ -346,7 +397,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
file,
delimiter,
rowFilter,
asEnum = false
asEnum = false,
None
)
)

Expand All @@ -361,7 +413,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
os.Path(file),
delimiter,
rowFilter,
asEnum = false
asEnum = false,
None
)
)

Expand All @@ -376,7 +429,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa
os.RelPath(file).resolveFrom(os.pwd),
delimiter,
rowFilter,
asEnum = false
asEnum = false,
None
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ object EmbedData {
filePath: os.Path,
delimiter: String,
rowFilter: String => Boolean,
asEnum: Boolean
asEnum: Boolean,
extendsFrom: Option[String]
): Seq[os.Path] = {

val lines =
Expand All @@ -43,7 +44,7 @@ object EmbedData {
s"""package $fullyQualifiedPackage
|
|// DO NOT EDIT: Generated by Indigo.
|${dataFrame.renderEnum(moduleName)}
|${dataFrame.renderEnum(moduleName, extendsFrom)}
|""".stripMargin
} else {
s"""package $fullyQualifiedPackage
Expand Down Expand Up @@ -247,7 +248,7 @@ final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) {
names.zip(types).map { case (n, t) => s"val ${toSafeNameCamel(n)}: $t" }.mkString(", ")
}

def renderEnum(moduleName: String): String = {
def renderEnum(moduleName: String, extendsFrom: Option[String]): String = {
val renderedRows =
rows
.map { r =>
Expand All @@ -257,8 +258,14 @@ final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) {
}
.mkString("\n")

val extFrom = extendsFrom
.map { module =>
s""" extends $module"""
}
.getOrElse("")

s"""
|enum $moduleName(${renderVars}):
|enum $moduleName(${renderVars})$extFrom:
|${renderedRows}
|""".stripMargin
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EmbedDataTests extends munit.FunSuite {
assertEquals(actual.rows.toList.map(_.toList), expectedRows)

val actualEnum =
actual.renderEnum("GameScores")
actual.renderEnum("GameScores", None)

val expectedEnum =
"""
Expand All @@ -56,6 +56,18 @@ class EmbedDataTests extends munit.FunSuite {

assertEquals(actualEnum.trim, expectedEnum.trim)

val actualEnumWithExtends =
actual.renderEnum("GameScores", Option("ScoreData"))

val expectedEnumWithExtends =
"""
|enum GameScores(val game: String, val highscore: Double, val allowed: Boolean) extends ScoreData:
| case Bob extends GameScores("tron", 10000.0, true)
| case Fred extends GameScores("tanks", 476.0, false)
""".stripMargin

assertEquals(actualEnumWithExtends.trim, expectedEnumWithExtends.trim)

val actualMap =
actual.renderMap("GameScores")

Expand Down

0 comments on commit 7cd3ea3

Please sign in to comment.