module Data.Generics.Record (
RecordT,
isRecord,
recordT,
fields,
emptyRecord,
recordStructure) where
import Data.Data
import Data.Maybe
data RecordT a = RecordT { constr :: Constr }
isRecord :: forall a . Data a => a -> Bool
isRecord _ = isJust (recordT :: Maybe (RecordT a))
recordT :: forall a. Data a => Maybe (RecordT a)
recordT = if length cs == 1 && length (fields r) > 0 then Just r else Nothing
where cs = dataTypeConstrs . dataTypeOf $ (undefined :: a)
r = RecordT $ head cs :: RecordT a
fields :: forall a. Data a => RecordT a -> [String]
fields = constrFields . constr
emptyRecord :: forall a. Data a => RecordT a -> a
emptyRecord = fromConstr . constr
recordStructure :: forall a. Data a => RecordT a -> [(TypeRep, String)]
recordStructure a = zip (gmapQ typeOf (emptyRecord a)) $ fields a