OneOfSchema
morphling.OneOfSchema
final case class OneOfSchema[P[_], F[_], I](alts: NonEmptyList[Alt[F, I, _]], discriminator: Option[String]) extends SchemaF[P, F, I]
Constructor that enables creation of schema for sum types.
Each constructor of the sum type I
is represented as a member of the list of alternatives. Each alternative defines a prism between a single constructor of the sum type, and an underlying type describing the arguments demanded by that constructor.
Consider the following sum type. The first constructor takes no arguments; the second takes two.
sealed trait Role
case object User extends Role
case class Administrator(department: String, subordinateCount: Int) extends Role
A schema value for this type looks like:
val roleSchema = oneOf(
alt[Unit, Prim, Role, Unit](
"user",
Schema.empty,
(_: Unit) => User,
{
case User => Some(Unit)
case _ => None
}
) ::
alt[Unit, Prim, Role, Administrator](
"administrator",
rec[Prim, Administrator](
(
required("department", Prim.str, (_: Administrator).department),
required("subordinateCount", Prim.int, (_: Administrator).subordinateCount)
).mapN(Administrator.apply)
),
identity,
{
case a @ Administrator(_, _) => Some(a)
case _ => None
}
) :: Nil
)
Type parameters
- F
-
$FDefn
- I
-
$IDefn
- P
-
$PDefn
Attributes
- Graph
-
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass Any
Members list
In this article