Skip to content

Commit 9543b51

Browse files
author
Serhii Khoma
authored
Update argonaut-codecs to latest version with typed errors (#27)
1 parent 6d16c09 commit 9543b51

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"license": "MIT",
2323
"dependencies": {
2424
"purescript-argonaut-core": "^5.0.0",
25-
"purescript-argonaut-codecs": "^6.0.0",
25+
"purescript-argonaut-codecs": "^7.0.0",
2626
"purescript-profunctor-lenses": "^6.0.0"
2727
},
2828
"devDependencies": {

src/Data/Argonaut/JCursor.purs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Data.Argonaut.JCursor where
33
import Prelude
44

55
import Data.Argonaut.Core as J
6-
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
6+
import Data.Argonaut.Decode (class DecodeJson, decodeJson, JsonDecodeError(..))
77
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
88
import Data.Array as A
99
import Data.Either (Either(..))
@@ -173,19 +173,19 @@ fromPrims lst = foldl f (inferEmpty <<< fst <$> head lst) lst
173173
f :: Maybe J.Json -> Tuple JCursor JsonPrim -> Maybe J.Json
174174
f j (Tuple c p) = j >>= cursorSet c (primToJson p)
175175

176-
fail :: forall a b. Show a => a -> Either String b
177-
fail x = Left $ "Expected String or Number but found: " <> show x
178-
179176
instance decodeJsonJCursor :: DecodeJson JCursor where
180177
decodeJson j = decodeJson j >>= loop
181178
where
182-
loop :: Array J.Json -> Either String JCursor
179+
loop :: Array J.Json -> Either JsonDecodeError JCursor
183180
loop arr =
184181
maybe (Right JCursorTop) goLoop $ Tuple <$> A.head arr <*> A.tail arr
185-
goLoop :: Tuple J.Json (Array J.Json) -> Either String JCursor
182+
goLoop :: Tuple J.Json (Array J.Json) -> Either JsonDecodeError JCursor
186183
goLoop (Tuple x xs) = do
187184
c <- loop xs
188-
J.caseJson fail fail (goNum c) (Right <<< flip JField c) (map J.stringify >>> fail) (map J.stringify >>> fail) x
189-
goNum :: JCursor -> Number -> Either String JCursor
185+
let
186+
fail :: forall a. Either JsonDecodeError a
187+
fail = Left (Named "Int or String" $ UnexpectedValue x)
188+
J.caseJson (const fail) (const fail) (goNum c) (Right <<< flip JField c) (const fail) (const fail) x
189+
goNum :: JCursor -> Number -> Either JsonDecodeError JCursor
190190
goNum c =
191-
maybe (Left "Not an Int") (Right <<< flip JIndex c) <<< I.fromNumber
191+
maybe (Left $ TypeMismatch "Int") (Right <<< flip JIndex c) <<< I.fromNumber

0 commit comments

Comments
 (0)