-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathCombinators.purs
More file actions
78 lines (68 loc) · 2.9 KB
/
Combinators.purs
File metadata and controls
78 lines (68 loc) · 2.9 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module Data.Argonaut.Decode.Combinators
( getField
, getFieldOptional
, getFieldOptional'
, defaultField
, (.:)
, (.:!)
, (.:?)
, (.!=)
) where
import Prelude
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode.Error (JsonDecodeError')
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
import Data.Either (Either)
import Data.Maybe (Maybe, fromMaybe)
import Foreign.Object as FO
import Data.Argonaut.Decode.Decoders as Decoders
-- | Attempt to get the value for a given key on an `Object Json`.
-- |
-- | Use this accessor if the key and value *must* be present in your object.
-- | If the key and value are optional, use `getFieldOptional'` (`.:?`) instead.
getField :: forall customErr a. DecodeJson a => FO.Object Json -> String -> Either (JsonDecodeError' customErr) a
getField = Decoders.getField decodeJson
infix 7 getField as .:
-- | Attempt to get the value for a given key on an `Object Json`.
-- |
-- | The result will be `Right Nothing` if the key and value are not present,
-- | or if the key is present and the value is `null`.
-- |
-- | Use this accessor if the key and value are optional in your object.
-- | If the key and value are mandatory, use `getField` (`.:`) instead.
getFieldOptional' :: forall customErr a. DecodeJson a => FO.Object Json -> String -> Either (JsonDecodeError' customErr) (Maybe a)
getFieldOptional' = Decoders.getFieldOptional' decodeJson
infix 7 getFieldOptional' as .:?
-- | Attempt to get the value for a given key on an `Object Json`.
-- |
-- | The result will be `Right Nothing` if the key and value are not present,
-- | but will fail if the key is present but the value cannot be converted to the right type.
-- |
-- | This function will treat `null` as a value and attempt to decode it into your desired type.
-- | If you would like to treat `null` values the same as absent values, use
-- | `getFieldOptional'` (`.:?`) instead.
getFieldOptional :: forall customErr a. DecodeJson a => FO.Object Json -> String -> Either (JsonDecodeError' customErr) (Maybe a)
getFieldOptional = Decoders.getFieldOptional decodeJson
infix 7 getFieldOptional as .:!
-- | Helper for use in combination with `.:?` to provide default values for optional
-- | `Object Json` fields.
-- |
-- | Example usage:
-- | ```purs
-- | newtype MyType = MyType
-- | { foo :: String
-- | , bar :: Maybe Int
-- | , baz :: Boolean
-- | }
-- |
-- | instance decodeJsonMyType :: DecodeJson MyType where
-- | decodeJson json = do
-- | x <- decodeJson json
-- | foo <- x .: "foo" -- mandatory field
-- | bar <- x .:? "bar" -- optional field
-- | baz <- x .:? "baz" .!= false -- optional field with default value of `false`
-- | pure $ MyType { foo, bar, baz }
-- | ```
defaultField :: forall customErr a. Either (JsonDecodeError' customErr) (Maybe a) -> a -> Either (JsonDecodeError' customErr) a
defaultField parser default = fromMaybe default <$> parser
infix 6 defaultField as .!=