higgledy-0.4.1.1: Partial types as a type constructor.
Safe HaskellNone
LanguageHaskell2010

Data.Generic.HKD.Labels

Synopsis

Documentation

class Label (structure :: Type) where Source #

For any record type, we can extract the labels generically using the Const functor.

>>> import Data.Generic.HKD
>>> import Data.Functor.Identity (Identity (..))
>>> data User = User { name :: String, age :: Int } deriving Generic
>>> label @User
User {name = Const "name", age = Const "age"}

Methods

label :: HKD structure (Const String) Source #

Instances

Instances details
(Generic structure, GLabels (Rep structure)) => Label structure Source # 
Instance details

Defined in Data.Generic.HKD.Labels

Methods

label :: HKD structure (Const String) Source #

labelsWhere :: forall structure f. (Label structure, ApplicativeB (HKD structure), TraversableB (HKD structure)) => (forall a. f a -> Bool) -> HKD structure f -> [String] Source #

Because all HKD types are valid barbies, and we have the above mechanism for extracting field names, we can ask some pretty interesting questions.

>>> import Control.Lens
>>> import Data.Maybe (isNothing)
>>> import Data.Monoid (Last (..))
>>> import Data.Generic.HKD

Let's imagine, for example, that we're half way through filling in a user's details:

>>> data User = User { name :: String, age :: Int } deriving Generic
>>> test = mempty @(HKD User Last) & field @"name" .~ pure "Tom"

We want to send a JSON response back to the client containing the fields that have yet to be finished. All we need to do is pick the fields where the values are Last Nothing:

>>> labelsWhere (isNothing . getLast) test
["age"]