Skip to content

Commit

Permalink
Tests pass with optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 19, 2023
1 parent 321672c commit daed9f7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ object EmbedData {
os.read.lines(filePath).filter(rowFilter)
}

val rows =
lines.map(row => extractRowData(row, delimiter)).toList

val dataFrame =
DataFrame.fromRows(
lines.map(row => extractRowData(row, delimiter)).toList
)
DataFrame.fromRows(rows)

val wd = outDir / Generators.OutputDirName

Expand Down Expand Up @@ -201,7 +202,7 @@ sealed trait DataType {
case _: DataType.DoubleData => "Double"
case d: DataType.IntData if d.nullable => "Option[Int]"
case _: DataType.IntData => "Int"
case DataType.NullData => "Null"
case DataType.NullData => "Option[Any]"
}

}
Expand Down Expand Up @@ -307,6 +308,26 @@ object DataType {
}
}

def matchHeaderRowLength(rows: Array[Array[DataType]]): Array[Array[DataType]] =
rows.toList match {
case Nil =>
rows

case headers :: data =>
val l = headers.length
val res =
headers :: data.map { r =>
val diff = l - r.length
if (diff > 0) {
r ++ List.fill(diff)(DataType.NullData)
} else {
r
}
}

res.toArray
}

}

final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) {
Expand All @@ -318,7 +339,7 @@ final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) {

def alignColumnTypes: DataFrame = {
val columns =
rows.transpose
DataType.matchHeaderRowLength(rows).transpose

val stringKeys: Array[DataType] =
columns.head.map(_.toStringData)
Expand Down Expand Up @@ -425,15 +446,13 @@ object DataFrame {
case _ :: Nil =>
throw new Exception("Only one row of data found. " + standardMessage)

case h :: t =>
case h :: _ =>
val len = h.length

if (len == 0) {
throw new Exception("No data to create. " + standardMessage)
} else if (len == 1) {
throw new Exception("Only one column of data. " + standardMessage)
} else if (!t.forall(_.length == len)) {
throw new Exception(s"All rows must be the same length. Header row had '$len' columns. " + standardMessage)
} else {
DataFrame(rows.map(_.toArray).toArray, len).alignColumnTypes
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,49 @@ class EmbedDataTests extends munit.FunSuite {
assertEquals(DataType.decideType("."), DataType.StringData("."))
}

test("matchHeaderRowLength") {

val rows =
List(
EmbedData.extractRowData("name,game,highscore,allowed", ","),
EmbedData.extractRowData("bob", ","),
EmbedData.extractRowData("Fred,tanks,476,false", ","),
EmbedData.extractRowData("Stan,,-2", ",")
)

val actual =
DataType.matchHeaderRowLength(rows.map(_.toArray).toArray).map(_.toList).toList

val expected =
List(
List(
DataType.StringData("name", false),
DataType.StringData("game", false),
DataType.StringData("highscore", false),
DataType.StringData("allowed", false)
),
List(
DataType.StringData("bob", false),
DataType.NullData,
DataType.NullData,
DataType.NullData
),
List(
DataType.StringData("Fred", false),
DataType.StringData("tanks", false),
DataType.IntData(476, false),
DataType.BooleanData(false, false)
),
List(
DataType.StringData("Stan", false),
DataType.NullData,
DataType.IntData(-2, false),
DataType.NullData
)
)

assertEquals(actual, expected)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class GeneratorAcceptanceTests extends munit.FunSuite {
|
|// DO NOT EDIT: Generated by Indigo.
|
|enum StatsEnum(val level: Int, val bonus: Int):
| case Intelligence extends StatsEnum(2, 4)
| case Strength extends StatsEnum(10, 0)
| case Fortitude extends StatsEnum(4, 1)
|enum StatsEnum(val level: Int, val bonus: Int, val stackable: Option[Boolean]):
| case Intelligence extends StatsEnum(2, 4, Some(true))
| case Strength extends StatsEnum(10, 0, None)
| case Fortitude extends StatsEnum(4, 1, Some(false))
|""".stripMargin

assertEquals(actual.trim, expected.trim)
Expand Down Expand Up @@ -72,13 +72,13 @@ class GeneratorAcceptanceTests extends munit.FunSuite {
|
|// DO NOT EDIT: Generated by Indigo.
|
|final case class StatsMap(level: Int, bonus: Int, code: String)
|final case class StatsMap(level: Int, bonus: Int, code: Option[String])
|object StatsMap:
| val data: Map[String, StatsMap] =
| Map(
| "intelligence" -> StatsMap(2, 4, "i"),
| "strength" -> StatsMap(10, 0, "st"),
| "fortitude" -> StatsMap(4, 1, "_frt")
| "intelligence" -> StatsMap(2, 4, Some("i")),
| "strength" -> StatsMap(10, 0, None),
| "fortitude" -> StatsMap(4, 1, Some("_frt"))
| )
|""".stripMargin

Expand Down Expand Up @@ -144,9 +144,9 @@ class GeneratorAcceptanceTests extends munit.FunSuite {
|// DO NOT EDIT: Generated by Indigo.
|/*
|"name": String,"level": String,"bonus": String,"code": String
|"intelligence": String,2: Int,4: Int,"i": String
|"strength": String,10: Int,0: Int,"st": String
|"fortitude": String,4: Int,1: Int,"_frt": String
|"intelligence": String,2: Int,4: Int,Some("i"): Option[String]
|"strength": String,10: Int,0: Int,None: Option[Any]
|"fortitude": String,4: Int,1: Int,Some("_frt"): Option[String]
|*/
|""".stripMargin

Expand Down
2 changes: 1 addition & 1 deletion indigo-plugin/test-assets/data/stats.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
| name | level | bonus | code |
| ------------ | ----- | ----- | ---- |
| intelligence | 2 | 4 | i |
| strength | 10 | 0 | st |
| strength | 10 | 0 | |
| fortitude | 4 | 1 | _frt |

0 comments on commit daed9f7

Please sign in to comment.