Skip to content

Commit

Permalink
Implement protobuf codec for case class schemas (zio#46)
Browse files Browse the repository at this point in the history
* Implement protobuf codec for case class schemas

* Add cases for all case class arities

* resolve merge issue

Co-authored-by: thinkharder <thinkharderdev@users.noreply.github.com>
  • Loading branch information
2 people authored and landlockedsurfer committed May 28, 2022
1 parent 3380c66 commit 6b77b1d
Show file tree
Hide file tree
Showing 6 changed files with 1,874 additions and 211 deletions.
53 changes: 0 additions & 53 deletions core/src/main/scala/zio/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -746,59 +746,6 @@ object Schema {
implicit def rightSchema[A, B](implicit schemaB: Schema[B]): Schema[Right[Nothing, B]] =
schemaB.transform(Right(_), _.value)

def caseClassN[A, Z](t1: (String, Schema[A]))(f: A => Z, g: Z => Option[A]): Schema[Z] =
Schema
.record(Map(t1))
.transformOrFail(
{ map =>
val v1 = map(t1._1).asInstanceOf[A]

Right(f(v1))
}, { (z: Z) =>
g(z).map { a =>
Map(t1._1 -> a)
}.toRight("Cannot deconstruct case class")
}
)

def caseClassN[A, B, Z](
t1: (String, Schema[A]),
t2: (String, Schema[B])
)(f: (A, B) => Z, g: Z => Option[(A, B)]): Schema[Z] =
Schema
.record(Map[String, Schema[_]](t1, t2))
.transformOrFail(
{ map =>
val v1 = map(t1._1).asInstanceOf[A]
val v2 = map(t2._1).asInstanceOf[B]

Right(f(v1, v2))
}, { (z: Z) =>
g(z).map { case (a, b) => Map(t1._1 -> a, t2._1 -> b) }
.toRight("Cannot deconstruct case class")
}
)

def caseClassN[A, B, C, Z](
t1: (String, Schema[A]),
t2: (String, Schema[B]),
t3: (String, Schema[C])
)(f: (A, B, C) => Z, g: Z => Option[(A, B, C)]): Schema[Z] =
Schema
.record(Map[String, Schema[_]](t1, t2, t3))
.transformOrFail(
{ map =>
val v1 = map(t1._1).asInstanceOf[A]
val v2 = map(t2._1).asInstanceOf[B]
val v3 = map(t3._1).asInstanceOf[C]

Right(f(v1, v2, v3))
}, { (z: Z) =>
g(z).map { case (a, b, c) => Map(t1._1 -> a, t2._1 -> b, t3._1 -> c) }
.toRight("Cannot deconstruct case class")
}
)

def either[A, B](left: Schema[A], right: Schema[B]): Schema[Either[A, B]] =
EitherSchema(left, right)

Expand Down
Loading

0 comments on commit 6b77b1d

Please sign in to comment.