问题
An external system returns Seq[String] (kind of DB, output like CSV/json), it's wrap of base types: string/numbers. I'd rather work with my own model.
object Converter {
type Output = (Int, String, Double) // for instance
def convert(values: List[String]): Output
}
Obviously, I do not want to implement convert-method every time.
Seems like I need something simpler than http://nrinaudo.github.io/tabulate/tut/parsing.html
Is it possible to use HList here? Like conversion sized HList (String:: String:: String:: HNil) into model by defining explicit only Output type.
回答1:
First of all, the output of convert
method would have to be Option[Output]
, or some monad of Output
(Try
, Either
, scalaz.\/
, scalaz.Validation
, etc.) in case the contents of the Seq[String]
can't be converted to Output
(wrong length of Seq
, errors in parsing Int
s or Doubles
, etc.)
A possible implementation with shapeless
would have a typeclass to convert String
to its parameter type, and an auxiliary typeclass to convert an HList
of String
to the HList
representation of Output
with the first typeclass.
Here is a sample implementation:
import shapeless._
import shapeless.syntax.std.traversable._
import shapeless.ops.traversable._
trait Parse[Out] {
def apply(value: String): Option[Out]
}
object Parse {
implicit object convertToInt extends Parse[Int] {
def apply(value: String) = Try(value.toInt).toOption
}
implicit object convertToString extends Parse[String] {
def apply(value: String) = Some(value)
}
implicit object convertToDouble extends Parse[Double] {
def apply(value: String) = Try(value.toDouble).toOption
}
}
trait ParseAll[Out] {
type In <: HList
def apply(values: In): Option[Out]
}
object ParseAll {
type Aux[I, O] = ParseAll[O] { type In = I }
implicit object convertHNil extends ParseAll[HNil] {
type In = HNil
def apply(value: HNil) = Some(HNil)
}
implicit def convertHList[T, HO <: HList](implicit
cv: Parse[T],
cl: ParseAll[HO]
) = new ParseAll[T :: HO] {
type In = String :: cl.In
def apply(value: In) = value match {
case x :: xs => for {
t <- cv(x)
h0 <- cl(xs)
} yield t :: h0
}
}
}
trait Converter {
type Output
def convert[S <: HList, H <: HList](values: List[String])(implicit
gen: Generic.Aux[Output, H], // Compute HList representation `H` of Output
parse: ParseAll.Aux[S, H], // Generate parser of Hlist of String `S` to HList `H`
ft: FromTraversable[S] // Generate converter of `List[String]` to HList of Strings `S`
): Option[Output] =
values.toHList[S].flatMap(parse.apply).map(gen.from)
}
It's simple to upgrade this implementation to return your error monad of choice (or to throw exceptions) instead of returning Option
And here is how you can use it:
scala> object ConverterISD extends Converter {
type Output = (Int, String, Double)
}
defined object ConverterISD
scala> ConverterISD.convert(List("1", "foo", "2.34"))
res0: Option[ConverterISD.Output] = Some((1,foo,2.34))
scala> ConverterISD.convert(List("1", "foo", "2.34", "5"))
res1: Option[ConverterISD.Output] = None
scala> ConverterISD.convert(List("1", "foo", "bar"))
res2: Option[ConverterISD.Output] = None
It also works with case classes instead of tuples:
scala> case class Model(i: Int, d: Double)
defined class Model
scala> object ConverterModel extends Converter {
type Output = Model
}
defined object ConverterModel
scala> ConverterModel.convert(List("1", "2.34"))
res0: Option[ConverterModel.Output] = Some(Model(1,2.34))
scala> ConverterModel.convert(List("1"))
res1: Option[ConverterModel.Output] = None
scala> ConverterModel.convert(List("1", "foo"))
res2: Option[ConverterModel.Output] = None
来源:https://stackoverflow.com/questions/34982555/convert-scala-liststring-listobject-into-model-hlist-tuple