Add an implementation of a storage built off of a directory of JSONs

This commit is contained in:
Aly 2019-01-14 16:04:02 -08:00
parent d4a2e22556
commit a0580982e0
No known key found for this signature in database
GPG key ID: 555B7346639DDAC3
3 changed files with 88 additions and 12 deletions

View file

@ -2,22 +2,23 @@ import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
scalaVersion in ThisBuild := "2.12.8"
val sharedSettings = Seq(
val compilerOptions = Seq(
"-unchecked",
"-deprecation",
"-feature",
"-language:higherKinds",
"-Ypartial-unification",
)
val mainSettings = Seq(
name := "cataquack",
organization := "tf.bug",
version := "0.1.0",
scalaVersion := "2.12.8",
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-feature",
"-language:higherKinds",
"-Ypartial-unification",
),
scalacOptions ++= compilerOptions,
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % "1.5.0",
"org.typelevel" %%% "cats-effect" % "1.1.0",
"org.scala-graph" %%% "graph-core" % "1.12.5",
"org.typelevel" %%% "cats-core" % "1.5.0",
"org.typelevel" %%% "cats-effect" % "1.1.0",
),
resolvers += Resolver.sonatypeRepo("releases"),
addCompilerPlugin("org.spire-math" % "kind-projector" % "0.9.8" cross CrossVersion.binary)
@ -26,9 +27,33 @@ val sharedSettings = Seq(
lazy val cataquack =
crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.settings(sharedSettings)
.settings(mainSettings)
.jsSettings(crossScalaVersions := Seq("2.11.12", "2.12.8"))
.jvmSettings(crossScalaVersions := Seq("2.11.12", "2.12.8"))
lazy val cataquackJS = cataquack.js
lazy val cataquackJVM = cataquack.jvm
val jsonDirSettings = Seq(
name := "cataquack-jsondir",
organization := "tf.bug",
version := "0.1.0",
scalaVersion := "2.12.8",
scalacOptions ++= compilerOptions,
libraryDependencies ++= Seq(
"io.circe" %%% "circe-core" % "0.11.0",
"io.circe" %%% "circe-generic" % "0.11.0",
"io.circe" %%% "circe-parser" % "0.11.0",
),
resolvers += Resolver.sonatypeRepo("releases"),
addCompilerPlugin("org.spire-math" % "kind-projector" % "0.9.8" cross CrossVersion.binary)
)
lazy val cataquackJsonDir =
crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.settings(jsonDirSettings)
.jvmSettings(crossScalaVersions := Seq("2.11.12", "2.12.8"))
.dependsOn(cataquack)
lazy val cataquackJsonDirJVM = cataquackJsonDir.jvm

View file

@ -0,0 +1,41 @@
package tf.bug.cataquack.jsondir
import implicits._
import java.nio.file.Path
import cats.effect.IO
import io.circe._
import io.circe.parser._
import tf.bug.cataquack.{IORaw, Raw, Storage}
import scala.io.Source
import scala.util.Try
object JSONDirectoryStorage {
def apply(p: Path): Try[Storage[IORaw[Json, ?], Decoder]] = Try {
if (p.toFile.isDirectory) {
new Storage[IORaw[Json, ?], Decoder] {
override def query[T](key: String): IORaw[Json, T] = IO {
val nf = p.resolve(key).toFile
val s = Source.fromFile(nf).mkString
val j = parse(s).right.get
Raw[Json, T](j)
}
/** Type like List, Id, etc */
override type Output[T] = IO[T]
override def execute[T: Decoder](
f: IORaw[Json, T]
): Output[T] = {
f.map(_.read)
}
}
} else {
throw new IllegalArgumentException("Supplied path must be a directory!")
}
}
}

View file

@ -0,0 +1,10 @@
package tf.bug.cataquack.jsondir
import io.circe.{Decoder, Json}
import io.circe.syntax._
import tf.bug.cataquack.Read
object implicits {
implicit def readDecoder[T: Decoder]: Read[Json, T] = (i: Json) => i.as[T].right.get
}