-
Notifications
You must be signed in to change notification settings - Fork 46
/
Combinators.purs
34 lines (26 loc) · 902 Bytes
/
Combinators.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Data.Argonaut.Decode.Combinators where
import Prelude
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Foreign.Object as FO
getField :: forall a. DecodeJson a => FO.Object Json -> String -> Either String a
getField o s =
maybe
(Left $ "Expected field " <> show s)
decodeJson
(FO.lookup s o)
infix 7 getField as .?
getFieldOptional :: forall a. DecodeJson a => FO.Object Json -> String -> Either String (Maybe a)
getFieldOptional o s =
maybe
(pure Nothing)
decode
(FO.lookup s o)
where
decode json = Just <$> decodeJson json
infix 7 getFieldOptional as .??
defaultField :: forall a. Either String (Maybe a) -> a -> Either String a
defaultField parser default = fromMaybe default <$> parser
infix 6 defaultField as .?=