X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=task-reporter.git;a=blobdiff_plain;f=src%2Fmain%2Fscala%2Fjp%2Fymir%2FtaskReporter%2Fcore%2FTSV.scala;fp=src%2Fmain%2Fscala%2Fjp%2Fymir%2FtaskReporter%2Fcore%2FTSV.scala;h=c83594ceeb1ec97c54c3900f3c81aecf10fe15ca;hp=817d950376ab7a3779ff2aa75e7881a373c9dc3b;hb=3b81fbc877a2b1e346d8f09cb215661cfe2dde30;hpb=b098d253574a32e3c77952987033f9e5ce35c572 diff --git a/src/main/scala/jp/ymir/taskReporter/core/TSV.scala b/src/main/scala/jp/ymir/taskReporter/core/TSV.scala index 817d950..c83594c 100644 --- a/src/main/scala/jp/ymir/taskReporter/core/TSV.scala +++ b/src/main/scala/jp/ymir/taskReporter/core/TSV.scala @@ -7,7 +7,7 @@ import scala.language.reflectiveCalls import scalaz._ import Scalaz._ -object TSV { +abstract class TSVTypes { implicit val ParserFunctor = new Functor[Parser] { def map[A, B](m: Parser[A])(f: A => B) = new Parser[B] { @@ -85,6 +85,9 @@ object TSV { def lookup[A: FromField](name: Symbol): Parser[A] = value.get(name).fold(("no field named " + name.name).raiseError[A])(_.parseField[A]) + + def lookup[A: FromField](name: Symbol, default: String): Parser[A] + = value.get(name).getOrElse(Field(default)).parseField[A] } type Failure[ F[_], R] = String => F[R] @@ -122,18 +125,6 @@ object TSV { def this(msg: String, cause: Throwable) = this(new RuntimeException(msg, cause)) } - def decode[A: FromRecord](tsv: StringLike[_]): Seq[A] = { - val lines = tsv.split('\n').filter(!_.isEmpty) - val header = lines.head.split('\t').map(Symbol(_)) - val body = lines.tail.map(line => Record((header zip line.split('\t').map { Field(_) }).toMap)) - return body.map { r => - runParser(r.parseRecord[A]) match { - case Right(a) => a - case Left(e) => throw new DecodeFailedException(e) - } - } - } - // Option[A] implicit def OptionFromField[A: FromField] = new FromField[Option[A]] { def parseField(f: Field): Parser[Option[A]] = { @@ -164,3 +155,19 @@ object TSV { } } } + +trait TSVFunctions extends TSVTypes { + def decode[A: FromRecord](tsv: StringLike[_]): Seq[A] = { + val lines = tsv.split('\n').filter(!_.isEmpty) + val header = lines.head.split('\t').map(Symbol(_)) + val records = lines.tail.map(line => Record((header zip line.split('\t').map { Field(_) }).toMap)) + return records.map { r => + runParser(r.parseRecord[A]) match { + case Right(a) => a + case Left(e) => throw new DecodeFailedException(e) + } + } + } +} + +object TSV extends TSVTypes with TSVFunctions