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 Serializable
trait Product
trait Equals
trait SchemaF[P, F, I]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def hfmap[G[_]](nt: FunctionK[F, G]): OneOfSchema[P, G, I]

HFunctor operation which allows transformation of the functor through which the structure of the schema will be interpreted.

HFunctor operation which allows transformation of the functor through which the structure of the schema will be interpreted.

Defining this operation directly on the SchemaF type rather than in morphling.SchemaF.schemaFHFunctor simplifies type inference.

Attributes

def pmap[Q[_]](nt: FunctionK[P, Q]): OneOfSchema[Q, F, I]

HFunctor operation which allows transformation of the primitive algebra of the schema.

HFunctor operation which allows transformation of the primitive algebra of the schema.

Defining this operation directly on the SchemaF type rather than in morphling.SchemaF.schemaFHFunctor simplifies type inference.

Attributes

Inherited methods

def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product