Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- makeDecoder :: Name -> ExpQ
- makeConstructorsDecoder :: Name -> [Con] -> ExpQ
- makeMatchClause :: Name -> [Type] -> Con -> MatchQ
- makeErrorClause :: Name -> MatchQ
- applyDecoder :: Name -> Name -> [Int] -> ExpQ
Documentation
makeDecoder :: Name -> ExpQ Source #
Make a Decoder for a given data type Usage: $(makeDecoder ''MyDataType <: otherDecoders)
makeConstructorsDecoder :: Name -> [Con] -> ExpQ Source #
Make a decoder for a given data type by extracting just enough metadata about the data type in order to be able to parse a Value
For example for the data type:
data T = T1 {f1::Int, f2::Int} | T2 Int Int
we add this function to the registry:
opts d1 d2 d3 -> Decoder $ v -> decodeFromDefinitions opts v $ case ToConstructor T1 [v1, v2]-> T1 $ d1 v1 * d2 v2 ... ToConstructor T2 [v1, v2]-> T2 $ d1 v1 * d3 v2 ... other -> Left ("cannot decode " <> valueToText v)
The case function is the only one which needs to be generated in order to match the exact shape of the constructors to instantiate
makeErrorClause :: Name -> MatchQ Source #
Return an error the json value cannot be decoded with a constructor name and some values