Commit 147302ed authored by Romain Reuillon's avatar Romain Reuillon

[Plugin] enh: support array in csv sampling

parent a8ad6338
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.openmole.core.context package org.openmole.core.context
import org.openmole.core.exception.UserBadDataError
import org.openmole.core.tools.obj.ClassUtils._ import org.openmole.core.tools.obj.ClassUtils._
import org.openmole.core.tools.obj.{ ClassUtils, Id } import org.openmole.core.tools.obj.{ ClassUtils, Id }
import shapeless.{ TypeCase, Typeable } import shapeless.{ TypeCase, Typeable }
...@@ -41,6 +42,12 @@ object ValType { ...@@ -41,6 +42,12 @@ object ValType {
rec(t) rec(t)
} }
def unsecureFromArray(t: ValType[_]): ValType[_] = {
val (res, level) = unArrayify(t)
if (level == 0) throw new UserBadDataError(s"ValType $t is no an array type")
res
}
/** /**
* Decorate ValType for implicit conversions to array type * Decorate ValType for implicit conversions to array type
* @param p * @param p
......
package org.openmole.core package org.openmole.core
import au.com.bytecode.opencsv.CSVReader import au.com.bytecode.opencsv.CSVReader
import org.openmole.core.context.ValType
import scala.reflect.ClassTag
/* /*
* Copyright (C) 2019 Romain Reuillon * Copyright (C) 2019 Romain Reuillon
...@@ -141,23 +144,36 @@ package object csv { ...@@ -141,23 +144,36 @@ package object csv {
Iterator.continually(reader.readNext).takeWhile(_ != null).map { line Iterator.continually(reader.readNext).takeWhile(_ != null).map { line
(columns zip columnsIndexes).map { (columns zip columnsIndexes).map {
case ((_, v), i) Variable.unsecure(v, converter(v)(line(i))) case ((name, v), i) Variable.unsecure(v, matchConverter(v, line(i), name))
} }
} }
} }
val conveters = Map[Class[_], (String _)]( def matchConverter(v: Val[_], s: String, name: String): Any = {
classOf[BigInteger] (new BigInteger(_: String)), def matchArray[T: ClassTag](s: String, convert: String T): Array[T] = {
classOf[BigDecimal] (new BigDecimal(_: String)), val trimed = s.trim
classOf[Double] ((_: String).toDouble), if (!trimed.startsWith("[") || !trimed.endsWith("]")) throw new UserBadDataError(s"Array in CSV files should have the following format [.., .., ..], found $s")
classOf[String] ((_: String).toString), s.drop(1).dropRight(1).split(",").map { s convert(s.trim) }
classOf[Boolean] ((_: String).toBoolean), }
classOf[Int] ((_: String).toInt),
classOf[Float] ((_: String).toFloat), v match {
classOf[Long] ((_: String).toLong) case Val.caseDouble(v) s.toDouble
) case Val.caseString(v) s
case Val.caseBoolean(v) s.toBoolean
def converter[T](p: Val[_]): String _ = case Val.caseInt(v) s.toInt
conveters.getOrElse(p.`type`.runtimeClass, throw new UserBadDataError("Unmanaged type for csv sampling for column binded to prototype " + p)) case Val.caseLong(v) s.toLong
case Val.caseArrayDouble(v) matchArray(s, _.toDouble)
case Val.caseArrayInt(v) matchArray(s, _.toInt)
case Val.caseArrayLong(v) matchArray(s, _.toLong)
case Val.caseArrayString(v) matchArray(s, identity)
case Val.caseArrayBoolean(v) matchArray(s, _.toBoolean)
case Val.caseArrayArrayDouble(v) matchArray(s, matchArray(_, _.toDouble))
case Val.caseArrayArrayInt(v) matchArray(s, matchArray(_, _.toInt))
case Val.caseArrayArrayLong(v) matchArray(s, matchArray(_, _.toLong))
case Val.caseArrayArrayString(v) matchArray(s, matchArray(_, identity))
case Val.caseArrayArrayBoolean(v) matchArray(s, matchArray(_, _.toBoolean))
case _ throw new UserBadDataError(s"Unsupported type in CSV sampling prototype $v mapped to column $name")
}
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment