OneOfSchema
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
The functor through which the structure of the schema will be interpreted. This will almost always be a fixpoint type such as morphling.HFix.HCofree, which is used to introduce the ability to create recursive (tree-structured) schema.
- I
The type of the Scala value to be produced (or consumed) by an interpreter of the schema. Also known as the "index" type of the schema.
- P
The GADT type constructor for a sum type which defines the set of primitive types used in the schema.