Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- class Label (structure :: Type) where
- labelsWhere :: forall structure f. (Label structure, ApplicativeB (HKD structure), TraversableB (HKD structure)) => (forall a. f a -> Bool) -> HKD structure f -> [String]
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"}
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"]