module ZM.Dynamic(
decodeAbsTypeModel
,typeDecoder
,typeDecoderMap
,MapTypeDecoder
) where
import qualified Data.ByteString as B
import Data.Flat
import qualified Data.Map as M
import Data.Model
import ZM.Transform
import ZM.Types
decodeAbsTypeModel :: AbsTypeModel -> B.ByteString -> Decoded Value
decodeAbsTypeModel = unflatWith . typeDecoder
typeDecoder :: AbsTypeModel -> Get Value
typeDecoder tm = solve (typeName tm) (typeDecoderMap tm)
type MapTypeDecoder = M.Map (Type AbsRef) (Get Value)
typeDecoderMap :: AbsTypeModel -> MapTypeDecoder
typeDecoderMap tm =
let denv = M.mapWithKey (\t ct -> conDecoder denv t [] ct) (typeTree tm)
in denv
conDecoder :: (Convertible name String) => MapTypeDecoder -> AbsType -> [Bool] -> ConTree name AbsRef -> Get Value
conDecoder env t bs (ConTree l r) = do
tag :: Bool <- decode
conDecoder env t (tag:bs) (if tag then r else l)
conDecoder env t bs (Con cn cs) = Value t (convert cn) (reverse bs) <$> mapM (`solve` env) (fieldsTypes cs)