]> gitweb @ CieloNegro.org - task-reporter.git/blobdiff - src/main/scala/jp/ymir/taskReporter/core/TSV.scala
adopt the new format
[task-reporter.git] / src / main / scala / jp / ymir / taskReporter / core / TSV.scala
index 817d950376ab7a3779ff2aa75e7881a373c9dc3b..c83594ceeb1ec97c54c3900f3c81aecf10fe15ca 100644 (file)
@@ -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