@@ -10,16 +10,13 @@ module Data.Schematic
10 , decodeAndValidateJson
10 , decodeAndValidateJson
11 , parseAndValidateJson
11 , parseAndValidateJson
12 , parseAndValidateJsonBy
12 , parseAndValidateJsonBy
13− , parseAndValidateVersionedJson
14 , parseAndValidateTopVersionJson
13 , parseAndValidateTopVersionJson
15− , decodeAndValidateVersionedJson
16 , parseAndValidateWithMList
14 , parseAndValidateWithMList
17 , decodeAndValidateVersionedWithMList
15 , decodeAndValidateVersionedWithMList
19 , isDecodingError
17 , isDecodingError
20 , isValidationError
18 , isValidationError
21 , ParseResult(..)
19 , ParseResult(..)
25import Control.Monad.Validation
22import Control.Monad.Validation
@@ -78,44 +75,22 @@ parseAndValidateTopVersionJson _ v =
78 Left em -> ValidationError em
75 Left em -> ValidationError em
79 Right () -> Valid jsonRepr
76 Right () -> Valid jsonRepr
81−class Migratable (revisions :: [(Revision, Schema)]) where
85− -> ParseResult (JsonRepr (Snd (Head revisions)))
89− ( TopLevel (Snd rev), SingI (Snd rev) )
90− => Migratable '[rev] where
91− mparse _ = parseAndValidateJson
93−instance {-# OVERLAPPABLE #-}
94− ( Migratable (Tail revisions)
95− , MigrateSchema (Snd (Head (Tail revisions))) (Snd (Head revisions))
96− , SingI (Snd (Head revisions)))
97− => Migratable revisions where
98− mparse s v = case parseEither parseJSON v of
100− migrate <$> (mparse (sTail s) v :: ParseResult (JsonRepr (Snd (Head (Tail revisions)))))
103−parseAndValidateVersionedJson
104− :: forall proxy v. (SingI (AllVersions v), Migratable (AllVersions v))
107− -> ParseResult (JsonRepr (Snd (Head (AllVersions v))))
108−parseAndValidateVersionedJson _ v = mparse (sing :: Sing (AllVersions v)) v
110parseAndValidateWithMList
78parseAndValidateWithMList
111− :: MList revisions
80+ => MList m revisions
112 -> J.Value
81 -> J.Value
113− -> ParseResult (JsonRepr (Head revisions))
82+ -> m (ParseResult (JsonRepr (Head revisions)))
114−parseAndValidateWithMList MNil v = parseAndValidateJson v
83+parseAndValidateWithMList MNil v = pure $ parseAndValidateJson v
115parseAndValidateWithMList ((:&&) p f tl) v = case parseAndValidateJsonBy p v of
84parseAndValidateWithMList ((:&&) p f tl) v = case parseAndValidateJsonBy p v of
116− Valid a -> Valid a
85+ Valid a -> pure $ Valid a
86+ DecodingError _ -> do
117− DecodingError _ -> f <$> parseAndValidateWithMList tl v
87+ pr <- parseAndValidateWithMList tl v
90+ ValidationError _ -> do
118− ValidationError _ -> f <$> parseAndValidateWithMList tl v
91+ pr <- parseAndValidateWithMList tl v
120decodeAndValidateJson
95decodeAndValidateJson
121 :: forall schema
96 :: forall schema
@@ -126,24 +101,16 @@ decodeAndValidateJson bs = case decode bs of
126 Nothing -> DecodingError "malformed json"
101 Nothing -> DecodingError "malformed json"
127 Just x -> parseAndValidateJson x
102 Just x -> parseAndValidateJson x
129−decodeAndValidateVersionedJson
130− :: (Migratable (AllVersions versioned), SingI (AllVersions versioned))
133− -> ParseResult (JsonRepr (Snd (Head (AllVersions versioned))))
134−decodeAndValidateVersionedJson vp bs = case decode bs of
135− Nothing -> DecodingError "malformed json"
136− Just x -> parseAndValidateVersionedJson vp x
138type family MapSnd (l :: [(a,k)]) = (r :: [k]) where
104type family MapSnd (l :: [(a,k)]) = (r :: [k]) where
139 MapSnd '[] = '[]
105 MapSnd '[] = '[]
140 MapSnd ( '(a, b) ': tl) = b ': MapSnd tl
106 MapSnd ( '(a, b) ': tl) = b ': MapSnd tl
142decodeAndValidateVersionedWithMList
108decodeAndValidateVersionedWithMList
143− :: proxy versioned
110+ => proxy versioned
144− -> MList (MapSnd (AllVersions versioned))
111+ -> MList m (MapSnd (AllVersions versioned))
145 -> BL.ByteString
112 -> BL.ByteString
146− -> ParseResult (JsonRepr (Head (MapSnd (AllVersions versioned))))
113+ -> m (ParseResult (JsonRepr (Head (MapSnd (AllVersions versioned)))))
147decodeAndValidateVersionedWithMList _ mlist bs = case decode bs of
114decodeAndValidateVersionedWithMList _ mlist bs = case decode bs of
148− Nothing -> DecodingError "malformed json"
115+ Nothing -> pure $ DecodingError "malformed json"
149 Just x -> parseAndValidateWithMList mlist x
116 Just x -> parseAndValidateWithMList mlist x