diff --git a/scautable/src/csv.scala b/scautable/src/csv.scala index 6604e29..294fd54 100644 --- a/scautable/src/csv.scala +++ b/scautable/src/csv.scala @@ -61,14 +61,14 @@ object CSV: // type Result = ReplaceOneType[InputTuple, EmptyTuple, "col1", Boolean] - type ReplaceOneTypeAtName[N <: Tuple, StrConst <: String, T <: Tuple, Head <: Tuple, A] <: Tuple = (T, N) match + type ReplaceOneTypeAtName[N <: Tuple, StrConst <: String, T <: Tuple, A] <: Tuple = (T, N) match case (EmptyTuple, _) => EmptyTuple case (_, EmptyTuple) => EmptyTuple case (nameHead *: nameTail, typeHead *: typeTail) => IsMatch[nameHead, StrConst] match case true => A *: typeTail case false => - typeHead *: ReplaceOneTypeAtName[nameTail, StrConst, typeTail, Head, A] + typeHead *: ReplaceOneTypeAtName[nameTail, StrConst, typeTail, A] // match // case EmptyTuple => T *: A *: StrConst *: EmptyTuple @@ -153,9 +153,11 @@ object CSV: (fct(tup) *: tup.toTuple).withNames[Concat[S, K1]] } + inline def forceColumnType[S <: String, A] = { + itr.map(_.asInstanceOf[NamedTuple[K1, ReplaceOneTypeAtName[K1, S, V1, A]]]) + } - - inline def mapColumn[S <: String, B, A](fct: B => A)(using ev: IsColumn[S, K1] =:= true, s: ValueOf[S])= { + inline def mapColumn[S <: String, B, A](fct: B => A)(using ev: IsColumn[S, K1] =:= true, s: ValueOf[S]): Iterator[NamedTuple[K1, ReplaceOneTypeAtName[K1, S, V1, A]]]= { import scala.compiletime.ops.string.* val headers = constValueTuple[K1].toList.map(_.toString()) type temp = "TEMP_COLUMN" @@ -171,7 +173,7 @@ object CSV: val tup = x.toTuple val mapped = fct(tup(idx).asInstanceOf[B]) val (head, tail) = x.toTuple.splitAt(idx) - (head ++ mapped *: tail.tail).withNames[K1].asInstanceOf[NamedTuple[K1,ReplaceOneTypeAtName[K1, S, V1, EmptyTuple, A]]] + (head ++ mapped *: tail.tail).withNames[K1].asInstanceOf[NamedTuple[K1,ReplaceOneTypeAtName[K1, S, V1, A]]] } } diff --git a/scautable/test/jvm/src/testJvm.scala b/scautable/test/jvm/src/testJvm.scala index 3295c57..393fb61 100644 --- a/scautable/test/jvm/src/testJvm.scala +++ b/scautable/test/jvm/src/testJvm.scala @@ -143,6 +143,16 @@ class CSVSuite extends munit.FunSuite: assertEquals(out.last.col2Renamed, "6") } + test("force column type") { + val csv: CsvIterator[("col1", "col2", "col3")] = CSV.absolutePath(Generated.resourceDir0 + "simple.csv") + + val renamed: Iterator[(col1 : "col1", col2Renamed : String, col3 : "col3")]= csv.drop(1).renameColumn["col2", "col2Renamed"].forceColumnType["col2Renamed", String] + val out = renamed.toArray + assertEquals(out.head.col2Renamed, "2") + assertEquals(out.tail.head.col2Renamed, "4") + assertEquals(out.last.col2Renamed, "6") + } + test("map column") { def csv = CSV.absolutePath(Generated.resourceDir0 + "simple.csv") def csvDrop1 = csv.drop(1)