scala/fs2json/fs2json.sc
author Tomas Zeman <tomas@functionals.cz>
Tue, 15 Dec 2020 09:22:21 +0100
changeset 60 4267602e8494
permissions -rw-r--r--
fs2json: directory structure -> json object converter. E.g. to be used instead of erica / py-Couchapp (interaction with couchdb is left to the user).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     1
import ammonite.ops._
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     2
import os.FileType.{Dir, File}
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     3
import ujson.{Obj, Str, Value}
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     4
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     5
import scala.collection.mutable
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     6
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     7
def fs2json(p: Path): Obj = {
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     8
  val l: Seq[(String, Value)] = ls(p) flatMap { f => f.fileType match {
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     9
    case Dir => Some(f.last -> fs2json(f))
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    10
    case File if f.last endsWith ".json" =>
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    11
      Some(f.last.dropRight(5) -> ujson.read(os.read(f)))
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    12
    case File =>
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    13
      val n = f.last.split("\\.").toList match {
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    14
        case v :: Nil => v
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    15
        case v => v dropRight 1 mkString "."
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    16
      }
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    17
      Some(n -> Str(os.read(f).stripLineEnd))
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    18
    case _ => None
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    19
  }}
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    20
  Obj(mutable.LinkedHashMap.from(l))
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    21
}
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    22
4267602e8494 fs2json: directory structure -> json object converter.
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    23
// vim: et sw=2 ts=2 syn=scala