The
RecordEncoder divisible (contravariant) functor allows you
to build an
Encoder for a Dhall record.
For example, let's take the following Haskell data type:
>>> :{
data Project = Project
{ projectName :: Text
, projectDescription :: Text
, projectStars :: Natural
}
:}
And assume that we have the following Dhall record that we would like
to parse as a
Project:
{ name =
"dhall-haskell"
, description =
"A configuration language guaranteed to terminate"
, stars =
289
}
Our encoder has type
Encoder Project, but we can't
build that out of any smaller encoders, as
Encoders cannot be
combined (they are only
Contravariants). However, we can use an
RecordEncoder to build an
Encoder for
Project:
>>> :{
injectProject :: Encoder Project
injectProject =
recordEncoder
( adapt >$< encodeFieldWith "name" inject
>*< encodeFieldWith "description" inject
>*< encodeFieldWith "stars" inject
)
where
adapt (Project{..}) = (projectName, (projectDescription, projectStars))
:}
Or, since we are simply using the
ToDhall instance to inject
each field, we could write
>>> :{
injectProject :: Encoder Project
injectProject =
recordEncoder
( adapt >$< encodeField "name"
>*< encodeField "description"
>*< encodeField "stars"
)
where
adapt (Project{..}) = (projectName, (projectDescription, projectStars))
:}