Skip to content

Commit f83ad3d

Browse files
committed
Added documentation comments to decode combinators
1 parent 9e0aa34 commit f83ad3d

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

src/Data/Argonaut/Decode/Combinators.purs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import Data.Either (Either(..))
1818
import Data.Maybe (Maybe(..), fromMaybe, maybe)
1919
import Foreign.Object as FO
2020

21+
-- | Attempt to get the value for a given key on an `Object Json`.
22+
-- |
23+
-- | Use this accessor if the key and value *must* be present in your object.
24+
-- | If the key and value are optional, use `getFieldOptional'` (`.:?`) instead.
2125
getField :: forall a. DecodeJson a => FO.Object Json -> String -> Either String a
2226
getField o s =
2327
maybe
@@ -27,17 +31,13 @@ getField o s =
2731

2832
infix 7 getField as .:
2933

30-
getFieldOptional :: forall a. DecodeJson a => FO.Object Json -> String -> Either String (Maybe a)
31-
getFieldOptional o s =
32-
maybe
33-
(pure Nothing)
34-
decode
35-
(FO.lookup s o)
36-
where
37-
decode json = Just <$> (elaborateFailure s <<< decodeJson) json
38-
39-
infix 7 getFieldOptional as .:!
40-
34+
-- | Attempt to get the value for a given key on an `Object Json`.
35+
-- |
36+
-- | The result will be `Right Nothing` if the key and value are not present,
37+
-- | or if the key is present and the value is `null`.
38+
-- |
39+
-- | Use this accessor if the key and value are optional in your object.
40+
-- | If the key and value are mandatory, use `getField` (`.:`) instead.
4141
getFieldOptional' :: forall a. DecodeJson a => FO.Object Json -> String -> Either String (Maybe a)
4242
getFieldOptional' o s =
4343
maybe
@@ -52,6 +52,44 @@ getFieldOptional' o s =
5252

5353
infix 7 getFieldOptional' as .:?
5454

55+
-- | Attempt to get the value for a given key on an `Object Json`.
56+
-- |
57+
-- | The result will be `Right Nothing` if the key and value are not present,
58+
-- | but will fail if the key is present but the value cannot be converted to the right type.
59+
-- |
60+
-- | This function will treat `null` as a value and attempt to decode it into your desired type.
61+
-- | If you would like to treat `null` values the same as absent values, use
62+
-- | `getFieldOptional` (`.:?`) instead.
63+
getFieldOptional :: forall a. DecodeJson a => FO.Object Json -> String -> Either String (Maybe a)
64+
getFieldOptional o s =
65+
maybe
66+
(pure Nothing)
67+
decode
68+
(FO.lookup s o)
69+
where
70+
decode json = Just <$> (elaborateFailure s <<< decodeJson) json
71+
72+
infix 7 getFieldOptional as .:!
73+
74+
-- | Helper for use in combination with `.:?` to provide default values for optional
75+
-- | `Object Json` fields.
76+
-- |
77+
-- | Example usage:
78+
-- | ```purescript
79+
-- | newtype MyType = MyType
80+
-- | { foo :: String
81+
-- | , bar :: Maybe Int
82+
-- | , baz :: Boolean
83+
-- | }
84+
-- |
85+
-- | instance decodeJsonMyType :: DecodeJson MyType where
86+
-- | decodeJson json = do
87+
-- | x <- decodeJson json
88+
-- | foo <- x .: "foo" -- mandatory field
89+
-- | bar <- x .:? "bar" -- optional field
90+
-- | baz <- x .:? "baz" .!= false -- optional field with default value of `false`
91+
-- | pure $ MyType { foo, bar, baz }
92+
-- | ```
5593
defaultField :: forall a. Either String (Maybe a) -> a -> Either String a
5694
defaultField parser default = fromMaybe default <$> parser
5795

0 commit comments

Comments
 (0)