Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interop for io.estatico.newtype #1111

Open
wants to merge 5 commits into
base: series/2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ addCommandAlias(

addCommandAlias(
"testScala2JVM",
"zioJsonMacrosJVM/test; zioJsonInteropRefinedJVM/test"
"zioJsonMacrosJVM/test; zioJsonInteropRefinedJVM/test; zioJsonInteropNewtypeJVM/test"
)

addCommandAlias(
"testScala2JS",
"zioJsonMacrosJS/test; zioJsonInteropRefinedJS/test"
"zioJsonMacrosJS/test; zioJsonInteropRefinedJS/test; zioJsonInteropNewtypeJS/test"
)

addCommandAlias(
Expand Down Expand Up @@ -79,6 +79,8 @@ lazy val zioJsonRoot = project
zioJsonInteropScalaz7x.js,
zioJsonInteropScalaz7x.jvm,
zioJsonInteropScalaz7x.native,
zioJsonInteropNewtype.js,
zioJsonInteropNewtype.jvm,
zioJsonGolden
)

Expand Down Expand Up @@ -357,6 +359,23 @@ lazy val zioJsonInteropRefined = crossProject(JSPlatform, JVMPlatform, NativePla
)
.enablePlugins(BuildInfoPlugin)

lazy val zioJsonInteropNewtype = crossProject(JSPlatform, JVMPlatform)
.in(file("zio-json-interop-newtype"))
.dependsOn(zioJson)
.settings(stdSettings("zio-json-interop-newtype"))
.settings(buildInfoSettings("zio.json.interop.newtype"))
.settings(macroExpansionSettings)
.settings(
libraryDependencies ++= Seq(
"io.estatico" %%% "newtype" % "0.4.4",
"dev.zio" %%% "zio-test" % zioVersion % "test",
"dev.zio" %%% "zio-test-sbt" % zioVersion % "test"
),
scalacOptions += "-language:implicitConversions",
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
)
.enablePlugins(BuildInfoPlugin)

lazy val zioJsonInteropScalaz7x = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.in(file("zio-json-interop-scalaz7x"))
.dependsOn(zioJson)
Expand All @@ -382,7 +401,8 @@ lazy val docs = project
zioJsonMacrosJVM,
zioJsonInteropHttp4s,
zioJsonInteropRefined.jvm,
zioJsonInteropScalaz7x.jvm
zioJsonInteropScalaz7x.jvm,
zioJsonInteropNewtype.jvm
)
.settings(
crossScalaVersions -= ScalaDotty,
Expand All @@ -398,6 +418,7 @@ lazy val docs = project
zioJsonInteropHttp4s,
zioJsonInteropRefined.jvm,
zioJsonInteropScalaz7x.jvm,
zioJsonInteropNewtype.jvm,
zioJsonGolden
),
readmeAcknowledgement :=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package zio.json.interop

import io.estatico.newtype.Coercible
import io.estatico.newtype.ops._
import zio.json.{ JsonDecoder, JsonEncoder, JsonFieldDecoder, JsonFieldEncoder }

package object newtype {
implicit def coercibleDecoder[B: JsonDecoder, A](implicit coerce: Coercible[B, A]): JsonDecoder[A] =
JsonDecoder[B].map(_.coerce[A])

implicit def coercibleEncoder[B: JsonEncoder, A](implicit coerce: Coercible[A, B]): JsonEncoder[A] =
JsonEncoder[B].contramap(_.coerce[B])

implicit def coercibleKeyDecoder[B: JsonFieldDecoder, A](implicit coerce: Coercible[B, A]): JsonFieldDecoder[A] =
JsonFieldDecoder[B].map(_.coerce[A])

implicit def coercibleKeyEncoder[B: JsonFieldEncoder, A](implicit coerce: Coercible[A, B]): JsonFieldEncoder[A] =
JsonFieldEncoder[B].contramap(_.coerce[B])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package zio.json.interop.newtype

import io.estatico.newtype.macros.newtype
import zio.json.{ DecoderOps, DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder }
import zio.test.Assertion.{ equalTo, isRight }
import zio.test.{ Spec, ZIOSpecDefault, assert, assertTrue }

object NewtypeSpec extends ZIOSpecDefault {
override def spec: Spec[Environment, Any] = suite("newtype")(
test("newtype") {
assert("""{"name":"fommil"}""".fromJson[Person])(isRight(equalTo(Person(Name("fommil"))))) &&
assertTrue(Person(Name("fommil")).toJson == """{"name":"fommil"}""")
}
)

@newtype case class Name(value: String)
case class Person(name: Name)
object Person {
implicit val encoder: JsonEncoder[Person] = DeriveJsonEncoder.gen[Person]
implicit val decoder: JsonDecoder[Person] = DeriveJsonDecoder.gen[Person]
}
}
Loading