diff --git a/openmole/core/org.openmole.core.context/src/main/scala/org/openmole/core/context/Val.scala b/openmole/core/org.openmole.core.context/src/main/scala/org/openmole/core/context/Val.scala index 9b00e13e129b600396859ace31de1b52d8f67ea4..2c182686890743e51339f4775952b5b0461c4027 100644 --- a/openmole/core/org.openmole.core.context/src/main/scala/org/openmole/core/context/Val.scala +++ b/openmole/core/org.openmole.core.context/src/main/scala/org/openmole/core/context/Val.scala @@ -17,6 +17,7 @@ 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, Id } import shapeless.{ TypeCase, Typeable } @@ -41,6 +42,12 @@ object ValType { 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 * @param p diff --git a/openmole/core/org.openmole.core.csv/src/main/scala/org/openmole/core/csv/package.scala b/openmole/core/org.openmole.core.csv/src/main/scala/org/openmole/core/csv/package.scala index 1f1b2341317794db66177955da2a34c11137e80d..eb945fb120c18148f4a5efe67a67377b1df0c29d 100644 --- a/openmole/core/org.openmole.core.csv/src/main/scala/org/openmole/core/csv/package.scala +++ b/openmole/core/org.openmole.core.csv/src/main/scala/org/openmole/core/csv/package.scala @@ -1,6 +1,9 @@ package org.openmole.core import au.com.bytecode.opencsv.CSVReader +import org.openmole.core.context.ValType + +import scala.reflect.ClassTag /* * Copyright (C) 2019 Romain Reuillon @@ -141,23 +144,36 @@ package object csv { Iterator.continually(reader.readNext).takeWhile(_ != null).map { line ⇒ (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 ⇒ _)]( - classOf[BigInteger] → (new BigInteger(_: String)), - classOf[BigDecimal] → (new BigDecimal(_: String)), - classOf[Double] → ((_: String).toDouble), - classOf[String] → ((_: String).toString), - classOf[Boolean] → ((_: String).toBoolean), - classOf[Int] → ((_: String).toInt), - classOf[Float] → ((_: String).toFloat), - classOf[Long] → ((_: String).toLong) - ) - - def converter[T](p: Val[_]): String ⇒ _ = - conveters.getOrElse(p.`type`.runtimeClass, throw new UserBadDataError("Unmanaged type for csv sampling for column binded to prototype " + p)) + def matchConverter(v: Val[_], s: String, name: String): Any = { + def matchArray[T: ClassTag](s: String, convert: String ⇒ T): Array[T] = { + val trimed = s.trim + if (!trimed.startsWith("[") || !trimed.endsWith("]")) throw new UserBadDataError(s"Array in CSV files should have the following format [.., .., ..], found $s") + s.drop(1).dropRight(1).split(",").map { s ⇒ convert(s.trim) } + } + + v match { + case Val.caseDouble(v) ⇒ s.toDouble + case Val.caseString(v) ⇒ s + case Val.caseBoolean(v) ⇒ s.toBoolean + case Val.caseInt(v) ⇒ s.toInt + 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") + } + } }