@@ -43,10 +43,12 @@ data Value
43-- DECODERS
43-- DECODERS
46+-- TODO switch to CPS if possible
46newtype Decoder e a =
48newtype Decoder e a =
48− { _run :: (Error e -> Error e) -> Value -> Either (Error e) a
50+ _run :: Value -> Either (Error e) a
52data Error e
54data Error e
@@ -67,6 +69,13 @@ data Type
67 | TArrayWith Int Int
69 | TArrayWith Int Int
72+mapLeft :: (e -> e') -> Either e a -> Either e' a
76+ Left e -> Left (func e)
71-- PRIMITIVES
80-- PRIMITIVES
@@ -78,18 +87,18 @@ string =
79text :: Decoder e Text
88text :: Decoder e Text
81− Decoder $ \mkError value ->
90+ Decoder $ \value ->
82 case value of
91 case value of
83 String txt ->
92 String txt ->
87− Left (mkError (Expecting value TString))
96+ Left (Expecting value TString)
90bool :: Decoder e Bool
99bool :: Decoder e Bool
92− Decoder $ \mkError value ->
101+ Decoder $ \value ->
93 case value of
102 case value of
95 Right True
104 Right True
@@ -98,18 +107,18 @@ bool =
98 Right False
107 Right False
101− Left (mkError (Expecting value TBool))
110+ Left (Expecting value TBool)
104int :: Decoder e Int
113int :: Decoder e Int
106− Decoder $ \mkError value ->
115+ Decoder $ \value ->
107 case value of
116 case value of
108 Integer integer ->
117 Integer integer ->
109 Right integer
118 Right integer
112− Left (mkError (Expecting value TInt))
121+ Left (Expecting value TInt)
@@ -118,67 +127,80 @@ int =
119list :: Decoder e a -> Decoder e [a]
128list :: Decoder e a -> Decoder e [a]
120list (Decoder run) =
129list (Decoder run) =
121− Decoder $ \mkError value ->
130+ Decoder $ \value ->
122 case value of
131 case value of
123 Array vector ->
132 Array vector ->
135+ mapLeft (Index i) (run v)
124− Vector.toList <$>
137+ Vector.toList <$> Vector.imapM runHelp vector
125− Vector.imapM (\i v -> run (mkError . Index i) v) vector
128− Left (mkError (Expecting value TArray))
140+ Left (Expecting value TArray)
131dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
143dict :: Decoder e a -> Decoder e (HashMap.HashMap Text a)
132dict (Decoder run) =
144dict (Decoder run) =
133− Decoder $ \mkError value ->
145+ Decoder $ \value ->
134 case value of
146 case value of
135 Object _ hashMap ->
147 Object _ hashMap ->
150+ mapLeft (Field k) (run v)
136− HashMap.traverseWithKey (\k v -> run (mkError . Field k) v) hashMap
152+ HashMap.traverseWithKey runHelp hashMap
139− Left (mkError (Expecting value TObject))
155+ Left (Expecting value TObject)
142pairs :: Decoder e a -> Decoder e [(Text, a)]
158pairs :: Decoder e a -> Decoder e [(Text, a)]
143pairs (Decoder run) =
159pairs (Decoder run) =
144− Decoder $ \mkError value ->
160+ Decoder $ \value ->
145 case value of
161 case value of
146 Object kvs _ ->
162 Object kvs _ ->
147− traverse (\(k,v) -> (,) k <$> run (mkError . Field k) v) kvs
163+ let
166+ Right a -> Right (k, a)
167+ Left e -> Left (Field k e)
169+ traverse runHelp kvs
150− Left (mkError (Expecting value TObject))
172+ Left (Expecting value TObject)
153maybe :: Decoder e a -> Decoder e (Maybe a)
175maybe :: Decoder e a -> Decoder e (Maybe a)
154maybe (Decoder run) =
176maybe (Decoder run) =
155− Decoder $ \mkError value ->
177+ Decoder $ \value ->
156− case run mkError value of
178+ case run value of
160 Right a ->
179 Right a ->
161 Right (Just a)
180 Right (Just a)
165-- OBJECT PRIMITIVES
187-- OBJECT PRIMITIVES
168field :: Text -> Decoder e a -> Decoder e a
190field :: Text -> Decoder e a -> Decoder e a
169−field name (Decoder run) =
191+field k (Decoder run) =
170− Decoder $ \mkError value ->
192+ Decoder $ \value ->
171 case value of
193 case value of
172 Object _ hashMap ->
194 Object _ hashMap ->
173− case HashMap.lookup name hashMap of
195+ case HashMap.lookup k hashMap of
174 Just v ->
196 Just v ->
175− run (mkError . Field name) v
197+ mapLeft (Field k) (run v)
177 Nothing ->
199 Nothing ->
178− Left $ mkError $ Expecting value (ObjectWith name)
200+ Left (Expecting value (TObjectWith k))
181− Left (mkError (Expecting value TObject))
203+ Left (Expecting value TObject)
184at :: [Text] -> Decoder e a -> Decoder e a
206at :: [Text] -> Decoder e a -> Decoder e a
@@ -192,18 +214,18 @@ at names decoder =
193index :: Int -> Decoder e a -> Decoder e a
215index :: Int -> Decoder e a -> Decoder e a
194index i (Decoder run) =
216index i (Decoder run) =
195− Decoder $ \mkError value ->
217+ Decoder $ \value ->
196 case value of
218 case value of
197 Array vector ->
219 Array vector ->
198 case vector !? i of
220 case vector !? i of
199 Just v ->
221 Just v ->
200− run (mkError . Index i) v
222+ mapLeft (Index i) (run v)
202 Nothing ->
224 Nothing ->
203− Left $ mkError $ Expecting value $ ArrayWith i (Vector.length vector)
225+ Left (Expecting value (TArrayWith i (Vector.length vector)))
206− Left (mkError (Expecting value TArray))
228+ Left (Expecting value TArray)
@@ -212,22 +234,23 @@ index i (Decoder run) =
213map :: (a -> value) -> Decoder e a -> Decoder e value
235map :: (a -> value) -> Decoder e a -> Decoder e value
214map func (Decoder run) =
236map func (Decoder run) =
215− Decoder $ \mkError value ->
237+ Decoder $ \value ->
216− func <$> run mkError value
238+ func <$> run value
219map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
242map2 :: (a -> b -> value) -> Decoder e a -> Decoder e b -> Decoder e value
220map2 func (Decoder runA) (Decoder runB) =
243map2 func (Decoder runA) (Decoder runB) =
221− Decoder $ \mkError value ->
244+ Decoder $ \value ->
223− <$> runA mkError value
246+ <$> runA value
224− <*> runB mkError value
247+ <*> runB value
227mapError :: (e -> e') -> Decoder e a -> Decoder e' a
250mapError :: (e -> e') -> Decoder e a -> Decoder e' a
228mapError func (Decoder run) =
251mapError func (Decoder run) =
229− Decoder $ \mkError value ->
252+ Decoder $ \value ->
230− run (mapErrorHelp func . mkError) value
253+ mapLeft (mapErrorHelp func) (run value)
233mapErrorHelp :: (e -> e') -> Error e -> Error e'
256mapErrorHelp :: (e -> e') -> Error e -> Error e'
@@ -235,16 +258,16 @@ mapErrorHelp func err =
235 case err of
258 case err of
236 Field f subErr -> Field f (mapErrorHelp func subErr)
259 Field f subErr -> Field f (mapErrorHelp func subErr)
237 Index i subErr -> Index i (mapErrorHelp func subErr)
260 Index i subErr -> Index i (mapErrorHelp func subErr)
238− OneOf errors -> OneOf (map (mapErrorHelp func) errors)
261+ OneOf errors -> OneOf (fmap (mapErrorHelp func) errors)
239 Failure v e -> Failure v (func e)
262 Failure v e -> Failure v (func e)
240 Expecting v e -> Expecting v e
263 Expecting v e -> Expecting v e
243apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
266apply :: Decoder e (a -> b) -> Decoder e a -> Decoder e b
244apply (Decoder runFunc) (Decoder runArg) =
267apply (Decoder runFunc) (Decoder runArg) =
245− Decoder $ \mkError value ->
268+ Decoder $ \value ->
246− do func <- runFunc mkError value
269+ do func <- runFunc value
247− arg <- runArg mkError value
270+ arg <- runArg value
248 return (func arg)
271 return (func arg)
@@ -275,21 +298,21 @@ instance Monad (Decoder e) where
276succeed :: a -> Decoder e a
299succeed :: a -> Decoder e a
277succeed a =
300succeed a =
278− Decoder $ \_ _ -> Right a
301+ Decoder $ \_ -> Right a
281fail :: e -> Decoder e a
304fail :: e -> Decoder e a
282fail err =
305fail err =
283− Decoder $ \mkError value ->
306+ Decoder $ \value ->
284− Left (mkError (Failure value err))
307+ Left (Failure value err)
287andThen :: (a -> Decoder e b) -> Decoder e a -> Decoder e b
310andThen :: (a -> Decoder e b) -> Decoder e a -> Decoder e b
288andThen callback (Decoder runA) =
311andThen callback (Decoder runA) =
289− Decoder $ \mkError value ->
312+ Decoder $ \value ->
290− do a <- runA mkError value
313+ do a <- runA value
291 let (Decoder runB) = callback a
314 let (Decoder runB) = callback a
292− runB mkError value
315+ runB value
@@ -301,16 +324,16 @@ oneOf decoders =
301 Decoder (oneOfHelp decoders [])
324 Decoder (oneOfHelp decoders [])
304−oneOfHelp :: [Decoder e a] -> [Error e] -> (Error e -> Error e) -> Value -> Either (Error e) a
327+oneOfHelp :: [Decoder e a] -> [Error e] -> Value -> Either (Error e) a
305−oneOfHelp decoders errors mkError value =
328+oneOfHelp decoders errors value =
306 case decoders of
329 case decoders of
308− Left (mkError (OneOf (reverse errors)))
331+ Left (OneOf (reverse errors))
310 Decoder run : otherDecoders ->
333 Decoder run : otherDecoders ->
311− case run mkError value of
334+ case run value of
312 Right a ->
335 Right a ->
315 Left err ->
338 Left err ->
316− oneOfHelp otherDecoders (err:errors) mkError value
339+ oneOfHelp otherDecoders (err:errors) value