Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic support for records #16

Open
paolino opened this issue Feb 1, 2020 · 0 comments
Open

generic support for records #16

paolino opened this issue Feb 1, 2020 · 0 comments

Comments

@paolino
Copy link
Contributor

paolino commented Feb 1, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant