1
1
module Data.Argonaut.Decode.Combinators
2
- ( parseField
3
- , parseFieldOptional
4
- , parseFieldOptional'
2
+ ( getField
3
+ , getFieldDeprecated
4
+ , getFieldOptional
5
+ , getFieldOptionalDeprecated
6
+ , getFieldOptional'
5
7
, defaultField
8
+ , defaultFieldDeprecated
6
9
, (.:)
10
+ , (.?)
7
11
, (.:!)
8
12
, (.:?)
13
+ , (.??)
9
14
, (.!=)
15
+ , (.?=)
10
16
) where
11
17
12
18
import Prelude
@@ -17,29 +23,40 @@ import Data.Bifunctor (lmap)
17
23
import Data.Either (Either (..))
18
24
import Data.Maybe (Maybe (..), fromMaybe , maybe )
19
25
import Foreign.Object as FO
26
+ import Prim.TypeError (class Warn , Text )
20
27
21
28
-- | Attempt to get the value for a given key on an `Object Json`.
22
29
-- |
23
30
-- | Use this accessor if the key and value *must* be present in your object.
24
- -- | If the key and value are optional, use `parseFieldOptional '` (`.:?`) instead.
25
- parseField :: forall a . DecodeJson a => FO.Object Json -> String -> Either String a
26
- parseField o s =
31
+ -- | If the key and value are optional, use `getFieldOptional '` (`.:?`) instead.
32
+ getField :: forall a . DecodeJson a => FO.Object Json -> String -> Either String a
33
+ getField o s =
27
34
maybe
28
35
(Left $ " Expected field " <> show s)
29
36
(elaborateFailure s <<< decodeJson)
30
37
(FO .lookup s o)
31
38
32
- infix 7 parseField as .:
39
+ infix 7 getField as .:
40
+
41
+ getFieldDeprecated
42
+ :: forall a . Warn ( Text " `.?` is deprecated, use `.:` instead" )
43
+ => DecodeJson a
44
+ => FO.Object Json
45
+ -> String
46
+ -> Either String a
47
+ getFieldDeprecated = getField
48
+
49
+ infix 7 getFieldDeprecated as .?
33
50
34
51
-- | Attempt to get the value for a given key on an `Object Json`.
35
52
-- |
36
53
-- | The result will be `Right Nothing` if the key and value are not present,
37
54
-- | or if the key is present and the value is `null`.
38
55
-- |
39
56
-- | Use this accessor if the key and value are optional in your object.
40
- -- | If the key and value are mandatory, use `parseField ` (`.:`) instead.
41
- parseFieldOptional ' :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
42
- parseFieldOptional ' o s =
57
+ -- | If the key and value are mandatory, use `getField ` (`.:`) instead.
58
+ getFieldOptional ' :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
59
+ getFieldOptional ' o s =
43
60
maybe
44
61
(pure Nothing )
45
62
decode
@@ -50,7 +67,7 @@ parseFieldOptional' o s =
50
67
then pure Nothing
51
68
else Just <$> decodeJson json
52
69
53
- infix 7 parseFieldOptional ' as .:?
70
+ infix 7 getFieldOptional ' as .:?
54
71
55
72
-- | Attempt to get the value for a given key on an `Object Json`.
56
73
-- |
@@ -59,17 +76,27 @@ infix 7 parseFieldOptional' as .:?
59
76
-- |
60
77
-- | This function will treat `null` as a value and attempt to decode it into your desired type.
61
78
-- | If you would like to treat `null` values the same as absent values, use
62
- -- | `parseFieldOptional ` (`.:?`) instead.
63
- parseFieldOptional :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
64
- parseFieldOptional o s =
79
+ -- | `getFieldOptional ` (`.:?`) instead.
80
+ getFieldOptional :: forall a . DecodeJson a => FO.Object Json -> String -> Either String (Maybe a )
81
+ getFieldOptional o s =
65
82
maybe
66
83
(pure Nothing )
67
84
decode
68
85
(FO .lookup s o)
69
86
where
70
87
decode json = Just <$> (elaborateFailure s <<< decodeJson) json
71
88
72
- infix 7 parseFieldOptional as .:!
89
+ infix 7 getFieldOptional as .:!
90
+
91
+ getFieldOptionalDeprecated
92
+ :: forall a . Warn ( Text " `.??` is deprecated, use `.:!` or `.:?` instead" )
93
+ => DecodeJson a
94
+ => FO.Object Json
95
+ -> String
96
+ -> Either String (Maybe a )
97
+ getFieldOptionalDeprecated = getFieldOptional
98
+
99
+ infix 7 getFieldOptionalDeprecated as .??
73
100
74
101
-- | Helper for use in combination with `.:?` to provide default values for optional
75
102
-- | `Object Json` fields.
@@ -95,6 +122,13 @@ defaultField parser default = fromMaybe default <$> parser
95
122
96
123
infix 6 defaultField as .!=
97
124
125
+ defaultFieldDeprecated
126
+ :: forall a . Warn ( Text " `.?=` is deprecated, use `.!=` instead" )
127
+ => Either String (Maybe a ) -> a -> Either String a
128
+ defaultFieldDeprecated = defaultField
129
+
130
+ infix 6 defaultFieldDeprecated as .?=
131
+
98
132
elaborateFailure :: ∀ a . String -> Either String a -> Either String a
99
133
elaborateFailure s e =
100
134
lmap msg e
0 commit comments