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

Implement protobuf codec for case class schemas #46

Merged
merged 4 commits into from
Apr 27, 2021
Merged
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
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