From 0fca5df77630e448cd53bcf9eed193dfa469f77a Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Sun, 19 Nov 2023 09:11:40 +0000 Subject: [PATCH] Handle negative ints and doubles --- .../indigoplugin/generators/EmbedData.scala | 17 +++++++----- .../generators/EmbedDataTests.scala | 27 +++++++++++++++++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala index 2fea2b1f0..ee3b73a86 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala @@ -202,8 +202,8 @@ object DataType { case object NullData extends DataType private val isBoolean: Regex = """^(true|false)$""".r - private val isDouble: Regex = """^([0-9]+?)\.([0-9]+)$""".r - private val isInt: Regex = """^([0-9]+)$""".r + private val isInt: Regex = """^(\-?[0-9]+)$""".r + private val isDouble: Regex = """^(\-?[0-9]+?)\.([0-9]+)$""".r private val isNull: Regex = """^$""".r def decideType: String => DataType = { @@ -276,12 +276,17 @@ final case class DataFrame(data: Array[Array[DataType]], columnCount: Int) { data.tail def alignColumnTypes: DataFrame = { - val transposed = rows.transpose - val stringKeys: Array[DataType] = transposed.head.map(_.toStringData) - val typeRows: Array[Array[DataType]] = transposed.tail + val columns = + rows.transpose + + val stringKeys: Array[DataType] = + columns.head.map(_.toStringData) + + val typedColumns: Array[Array[DataType]] = columns.tail .map(d => DataType.convertToBestType(d.toList).toArray) + val cleanedRows: Array[Array[DataType]] = - (stringKeys +: typeRows).transpose + (stringKeys +: typedColumns).transpose this.copy( data = headers.asInstanceOf[Array[DataType]] +: cleanedRows diff --git a/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/EmbedDataTests.scala b/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/EmbedDataTests.scala index 254373936..6dbd46ab3 100644 --- a/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/EmbedDataTests.scala +++ b/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/EmbedDataTests.scala @@ -8,7 +8,8 @@ class EmbedDataTests extends munit.FunSuite { List( EmbedData.extractRowData("name,game,highscore,allowed", ","), EmbedData.extractRowData("bob,tron,10000.00,true", ","), - EmbedData.extractRowData("Fred,tanks,476,false", ",") + EmbedData.extractRowData("Fred,tanks,476,false", ","), + EmbedData.extractRowData("Stan,,-2,true", ",") ) val actual = @@ -35,6 +36,12 @@ class EmbedDataTests extends munit.FunSuite { DataType.StringData("tanks"), DataType.DoubleData(476.0), DataType.BooleanData(false) + ), + List( + DataType.StringData("Stan"), + DataType.NullData, + DataType.DoubleData(-2), + DataType.BooleanData(true) ) ) @@ -49,6 +56,7 @@ class EmbedDataTests extends munit.FunSuite { |enum GameScores(val game: String, val highscore: Double, val allowed: Boolean): | case Bob extends GameScores("tron", 10000.0, true) | case Fred extends GameScores("tanks", 476.0, false) + | case Stan extends GameScores(null, -2.0, true) """.stripMargin assertEquals(actualEnum.trim, expectedEnum.trim) @@ -61,6 +69,7 @@ class EmbedDataTests extends munit.FunSuite { |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) + | case Stan extends GameScores(null, -2.0, true) """.stripMargin assertEquals(actualEnumWithExtends.trim, expectedEnumWithExtends.trim) @@ -75,7 +84,8 @@ class EmbedDataTests extends munit.FunSuite { | val data: Map[String, GameScores] = | Map( | "bob" -> GameScores("tron", 10000.0, true), - | "Fred" -> GameScores("tanks", 476.0, false) + | "Fred" -> GameScores("tanks", 476.0, false), + | "Stan" -> GameScores(null, -2.0, true) | ) """.stripMargin @@ -175,4 +185,17 @@ class EmbedDataTests extends munit.FunSuite { assertEquals(actual, expected) } + test("decideType - int") { + assertEquals(DataType.decideType("10"), DataType.IntData(10)) + assertEquals(DataType.decideType("-10"), DataType.IntData(-10)) + } + + test("decideType - double") { + assertEquals(DataType.decideType("10.0"), DataType.DoubleData(10.0)) + assertEquals(DataType.decideType("-10.0"), DataType.DoubleData(-10.0)) + assertEquals(DataType.decideType("-10."), DataType.StringData("-10.")) + assertEquals(DataType.decideType(".0"), DataType.StringData(".0")) + assertEquals(DataType.decideType("."), DataType.StringData(".")) + } + }