Skip to content

Commit

Permalink
Pull out Scalaz + Refined interop modules (zio#325)
Browse files Browse the repository at this point in the history
* zioJsonInteropScalaz7x module

* zioJsonInteropRefined module

* Sort modules

* Format build.sbt

* Remove superfluous import
  • Loading branch information
fsvehla authored May 27, 2021
1 parent 9877882 commit 178d630
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 27 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ We can use `.mapOrFail` to take the result of another `JsonDecoder` and try to c
Say we are using the [`refined`](https://github.com/fthomas/refined) library to ensure that a `Person` data type only holds a non-empty string in its `name` field

```scala
import zio.json._
import zio.json.interop.refined._

import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.NonEmpty

Expand All @@ -280,12 +283,14 @@ In fact, we do not need to provide `decodeName` for each `Refined` data type; `z

## Integrations

Integrations are provided for the following external libraries which must be depended upon separately:
Integrations are provided several popular libraries, which are published as separate artifacts:

- [Scalaz 7.x](https://github.com/scalaz/scalaz) with `import zio.json.compat.scalaz._`
- [refined 0.9.x](https://github.com/fthomas/refined) with `import zio.json.compat.refined._`
- Akka Http
- HTTP4s
- Refined
- Scalaz 7

Alternative (binary incompatible) versions are not supported; if you require support for a different version of any of these libraries, just copy the source code into your project and change the package name (and address any API changes).
[Complete list of interop modules](https://zio.github.io/zio-json/docs/interop/interop_index)

# Performance

Expand Down
53 changes: 46 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ addCommandAlias("fixCheck", "scalafixAll --check")
addCommandAlias("fmt", "all scalafmtSbt scalafmtAll")
addCommandAlias("fmtCheck", "all scalafmtSbtCheck scalafmtCheckAll")
addCommandAlias("prepare", "fix; fmt")
addCommandAlias("testJVM", "zioJsonJVM/test; zioJsonYaml/test; zioJsonMacrosJVM/test; zioJsonInteropHttp4s/test")
addCommandAlias(
"testJVM",
"zioJsonJVM/test; zioJsonYaml/test; zioJsonMacrosJVM/test; zioJsonInteropHttp4s/test; zioJsonInteropScalaz7xJVM/test; zioJsonInteropScalaz7xJS/test; zioJsonInteropRefinedJVM/test; zioJsonInteropRefinedJS/test"
)

addCommandAlias("testJS", "zioJsonJS/test")

val zioVersion = "1.0.8"
Expand All @@ -46,7 +50,11 @@ lazy val root = project
zioJsonYaml,
zioJsonMacrosJVM,
zioJsonMacrosJS,
zioJsonInteropHttp4s
zioJsonInteropHttp4s,
zioJsonInteropRefined.js,
zioJsonInteropRefined.jvm,
zioJsonInteropScalaz7x.js,
zioJsonInteropScalaz7x.jvm
)

val circeVersion = "0.13.0"
Expand All @@ -65,8 +73,6 @@ lazy val zioJson = crossProject(JSPlatform, JVMPlatform)
scalacOptions -= "-opt-inline-from:zio.internal.**",
libraryDependencies ++= Seq(
"com.propensive" %%% "magnolia" % "0.17.0",
"org.scalaz" %%% "scalaz-core" % "7.3.3" intransitive (),
"eu.timepit" %%% "refined" % "0.9.25" intransitive (),
"org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided,
"dev.zio" %%% "zio" % zioVersion,
"dev.zio" %%% "zio-streams" % zioVersion,
Expand Down Expand Up @@ -242,9 +248,43 @@ lazy val zioJsonInteropHttp4s = project
)
.dependsOn(zioJsonJVM)

lazy val zioJsonInteropRefined = crossProject(JSPlatform, JVMPlatform)
.in(file("zio-json-interop-refined"))
.jvmConfigure(_.dependsOn(zioJsonJVM))
.jsConfigure(_.dependsOn(zioJsonJS))
.settings(stdSettings("zio-json-interop-refined"))
.settings(buildInfoSettings("zio.json.interop.refined"))
.settings(
libraryDependencies ++= Seq(
"eu.timepit" %%% "refined" % "0.9.25",
"dev.zio" %%% "zio-test" % zioVersion % "test",
"dev.zio" %%% "zio-test-sbt" % zioVersion % "test"
),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
)

lazy val zioJsonInteropScalaz7x = crossProject(JSPlatform, JVMPlatform)
.in(file("zio-json-interop-scalaz7x"))
.jvmConfigure(_.dependsOn(zioJsonJVM))
.jsConfigure(_.dependsOn(zioJsonJS))
.settings(stdSettings("zio-json-interop-scalaz7x"))
.settings(buildInfoSettings("zio.json.interop.scalaz7x"))
.settings(
libraryDependencies ++= Seq(
"org.scalaz" %%% "scalaz-core" % "7.3.3",
"dev.zio" %%% "zio-test" % zioVersion % "test",
"dev.zio" %%% "zio-test-sbt" % zioVersion % "test"
),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
)

lazy val docs = project
.in(file("zio-json-docs"))
.dependsOn(zioJsonJVM)
.dependsOn(
zioJsonJVM,
zioJsonInteropScalaz7x.jvm,
zioJsonInteropRefined.jvm
)
.settings(
skip.in(publish) := true,
mdocVariables := Map(
Expand All @@ -258,8 +298,7 @@ lazy val docs = project
scalacOptions -= "-Yno-imports",
scalacOptions -= "-Xfatal-warnings",
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion,
"eu.timepit" %% "refined" % "0.9.25"
"dev.zio" %% "zio" % zioVersion
),
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(root),
ScalaUnidoc / unidoc / target := (LocalRootProject / baseDirectory).value / "website" / "static" / "api",
Expand Down
2 changes: 2 additions & 0 deletions docs/interop/interop_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ title: "Interop modules"

* [Akka Http](interop_akka_http)
* [HTTP4s](interop_http4s)
* [Refined](interop_refined)
* [Scalaz 7.x](interop_scalaz_7x)
36 changes: 36 additions & 0 deletions docs/interop/interop_refined.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
id: interop_refined
title: "Refined Interop"
---

## Installation

```scala
libraryDependencies ++= Seq(
"@ORG@" % "zio-json-interop-refined" % "@RELEASE_VERSION@"
)
```

## Usage

```scala mdoc
import zio.json._
import zio.json.interop.refined._

import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.NonEmpty

case class Person(name: String Refined NonEmpty)

object Person {
implicit val decoder: JsonDecoder[Person] = DeriveJsonDecoder.gen
}
```

```scala mdoc
"""{ "name": "" }""".fromJson[Person]
```

```scala mdoc
"""{ "name": "Aurora" }""".fromJson[Person]
```
23 changes: 23 additions & 0 deletions docs/interop/interop_scalaz_7x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
id: interop_scalaz_7x
title: "Scalaz 7.x Interop"
---

## Installation

```scala
libraryDependencies ++= Seq(
"@ORG@" % "zio-json-interop-scalaz" % "@RELEASE_VERSION@"
)
```

## Usage

```scala mdoc
import zio.json._
import zio.json.interop.scalaz7x._

import scalaz._

IList(1, 2, 3).toJson
```
5 changes: 4 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
"interop-sidebar": {
"Interop": [
"interop/interop_index",

"interop/interop_akka_http",
"interop/interop_http4s"
"interop/interop_http4s",
"interop/interop_refined",
"interop/interop_scalaz_7x"
]
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package zio.json.compat
package zio.json.interop

import eu.timepit.refined.api.{ Refined, Validate }
import eu.timepit.refined.{ refineV }

import zio.json._

object refined {
package object refined {
implicit def encodeRefined[A: JsonEncoder, B]: JsonEncoder[A Refined B] =
JsonEncoder[A].contramap(_.value)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package testzio.json.compat
package zio.json.interop.refined

import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.collection.NonEmpty

import zio.json._
import zio.json.compat.refined._
import zio.test.Assertion._
import zio.test._

object RefinedSpec extends DefaultRunnableSpec {
def spec: ZSpec[Environment, Failure] =
val spec: ZSpec[Environment, Failure] =
suite("Refined")(
test("Refined") {
assert("""{"name":""}""".fromJson[Person])(isLeft(equalTo(".name(Predicate isEmpty() did not fail.)"))) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package zio.json.compat
package zio.json.interop

import _root_.scalaz._
import scalaz._

import zio.json._

object scalaz {

package object scalaz7x {
implicit def ilistEncoder[A: JsonEncoder]: JsonEncoder[IList[A]] =
JsonEncoder.list[A].contramap(_.toList)

implicit def ilistJsonDecoder[A: JsonDecoder]: JsonDecoder[IList[A]] =
JsonDecoder.list[A].map(IList.fromList(_))

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package testzio.json.compat
package zio.json.interop.scalaz

import _root_.scalaz._
import scalaz._

import zio.json._
import zio.json.compat.scalaz._
import zio.json.interop.scalaz7x._
import zio.test.Assertion._
import zio.test._

object ScalazSpec extends DefaultRunnableSpec {
def spec: ZSpec[Environment, Failure] =
val spec: ZSpec[Environment, Failure] =
suite("Scalaz")(
test("Scalaz") {
test("scalaz.IList[A]") {
assert(IList[Int]().toJson)(equalTo("[]")) &&
assert(IList(1, 2, 3).toJson)(equalTo("[1,2,3]")) &&
assert(IList[Int]().toJsonPretty)(equalTo("[]")) &&
Expand Down

0 comments on commit 178d630

Please sign in to comment.