Skip to content

generic support for records #16

@paolino

Description

@paolino

I've managed to get it via generics-sop, but I don't have much experience in this stuff.

data T a = T
    { field_reps   :: Int
    , field_what   :: Text
    , field_ignore :: Char
    , field_inside :: a
    }
    deriving (Show, Eq)

deriveGeneric ''T -- SOP

-- relies on unjsonDef for all fields
unjT :: (Typeable a, Unjson a) => UnjsonDef (T a)
unjT = gunjson
        do T.stripPrefix "field_"
        do [  "number of repetitions"
                , "string to repeat"
                , "char to ignore"
                , "inside the T"
                ] 
        do T

-- specify unjsonDef per field
unjUT :: (Typeable a, Unjson a) => UnjsonDef (T a)
unjUT = gunjsonU
        do T.stripPrefix "field_"
        do C "number of repetitions" unjsonDef
                :* C "string to repeat" 
                    do invmap T.reverse T.reverse unjsonDef
                :* C "char to ignore"   unjsonDef
                :* C "inside the T"     unjsonDef
                :* Nil
        do T



instance (Typeable a, Unjson a) => Unjson (T a)
    where unjsonDef = unjUT

testV = T 54 "mamma" 'a' (T 32 "piero" 'p' (128 :: Int))

roundTest :: forall a.  (Unjson a, Eq a) => a -> Bool
roundTest x =  (parse unjsonDef . traceShowId . unjsonToJSON unjsonDef) x == Result x []

If you think it can be good to have, I can file a MR

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions