-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add zio-json and zio-schema integrations
- Loading branch information
1 parent
bd4d300
commit c49b086
Showing
7 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package derevo.zio.json | ||
|
||
import derevo.{Derevo, Derivation, NewTypeDerivation, delegating} | ||
import zio.json._ | ||
|
||
@delegating("zio.json.DeriveJsonDecoder.gen") | ||
object jsonDecoder extends Derivation[JsonDecoder] with NewTypeDerivation[JsonDecoder] { | ||
def instance[A]: JsonDecoder[A] = macro Derevo.delegate[JsonDecoder, A] | ||
} | ||
|
||
@delegating("zio.json.DeriveJsonEncoder.gen") | ||
object jsonEncoder extends Derivation[JsonEncoder] with NewTypeDerivation[JsonEncoder] { | ||
def instance[A]: JsonEncoder[A] = macro Derevo.delegate[JsonEncoder, A] | ||
} |
50 changes: 50 additions & 0 deletions
50
modules/zioJson/src/test/scala/derevo/zio/json/ZioJsonSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package derevo.zio.json | ||
|
||
import derevo.derive | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import zio.json.JsonCodecConfiguration.SumTypeHandling | ||
import zio.json._ | ||
|
||
@derive(jsonEncoder) | ||
sealed trait Choice | ||
|
||
object Choice { | ||
implicit val Configuration: JsonCodecConfiguration = | ||
JsonCodecConfiguration(SumTypeHandling.DiscriminatorField("type")) | ||
} | ||
|
||
@derive(jsonEncoder) | ||
final case class First(a: Int, b: String) extends Choice | ||
|
||
@derive(jsonEncoder) | ||
final case class Second(c: Boolean, f: Option[Foo]) extends Choice | ||
|
||
@derive(jsonEncoder) | ||
final case class P[A, B](a: A, b: List[B]) | ||
|
||
@derive(jsonEncoder, jsonDecoder) | ||
case class Foo(d: Int, e: String) | ||
|
||
@derive(jsonEncoder) | ||
case class ChoiceList(list: List[Choice], amount: Int) | ||
|
||
class ZioJsonSpec extends AnyFlatSpec with Matchers { | ||
"Encoder derivation for ADT" should "work correctly" in { | ||
val choices = ChoiceList(List(First(1, "lol"), Second(c = true, Some(Foo(1, "kek")))), 2) | ||
|
||
choices.toJson shouldBe """{"list":[{"type":"First","a":1,"b":"lol"},{"type":"Second","c":true,"f":{"d":1,"e":"kek"}}],"amount":2}""" | ||
} | ||
|
||
"Decoder derivation for case class" should "work correctly" in { | ||
val foo = Foo(100, "kek") | ||
|
||
"""{"d":100,"e":"kek"}""".fromJson[Foo] shouldBe Right(foo) | ||
} | ||
|
||
"Parametric derivation for case class" should "work correctly" in { | ||
val p = P(123.0, List(1, 2, 3)) | ||
|
||
p.toJson shouldBe """{"a":123.0,"b":[1,2,3]}""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package derevo.zio.schema | ||
|
||
import derevo.{Derevo, Derivation, NewTypeDerivation, delegating} | ||
import zio.schema.Schema | ||
|
||
@delegating("zio.schema.DeriveSchema.gen") | ||
object schema extends Derivation[Schema] with NewTypeDerivation[Schema] { | ||
def instance[A]: Schema[A] = macro Derevo.delegate[Schema, A] | ||
} |
25 changes: 25 additions & 0 deletions
25
modules/zioSchema/src/test/scala/derevo/zio/json/ZioSchemaSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package derevo.zio.json | ||
|
||
import derevo.derive | ||
import derevo.zio.schema.schema | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import zio.schema._ | ||
|
||
@derive(schema) | ||
final case class Person(name: String, age: Int) | ||
|
||
class ZioSchemaSpec extends AnyFlatSpec with Matchers { | ||
"Schema derivation" should "work correctly" in { | ||
val person1 = Person("Gabriel", 45) | ||
val person2 = Person("Gabi", 54) | ||
|
||
val patch: Patch[Person] = Schema[Person].diff(person1, person2) | ||
val inverted: Patch[Person] = patch.invert | ||
|
||
val result1: Either[String, Person] = patch.patch(person1) | ||
val result2: Either[String, Person] = result1.flatMap(inverted.patch) | ||
|
||
result2 shouldBe Right(person1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.7.1 | ||
sbt.version=1.9.9 |