From a0580982e0e1e4c4df43f522d454ac80dd572200 Mon Sep 17 00:00:00 2001 From: Soren Date: Mon, 14 Jan 2019 16:04:02 -0800 Subject: [PATCH] Add an implementation of a storage built off of a directory of JSONs --- build.sbt | 49 ++++++++++++++----- .../jsondir/JSONDirectoryStorage.scala | 41 ++++++++++++++++ .../tf/bug/cataquack/jsondir/implicits.scala | 10 ++++ 3 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/JSONDirectoryStorage.scala create mode 100644 cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/implicits.scala diff --git a/build.sbt b/build.sbt index 0f75450..6fed9e3 100644 --- a/build.sbt +++ b/build.sbt @@ -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 diff --git a/cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/JSONDirectoryStorage.scala b/cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/JSONDirectoryStorage.scala new file mode 100644 index 0000000..592f567 --- /dev/null +++ b/cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/JSONDirectoryStorage.scala @@ -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!") + } + } + +} diff --git a/cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/implicits.scala b/cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/implicits.scala new file mode 100644 index 0000000..d11260c --- /dev/null +++ b/cataquackJsonDir/src/main/scala/tf/bug/cataquack/jsondir/implicits.scala @@ -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 + +}