Skip to content

Commit 2a98b21

Browse files
committed
Added back old decode function operators, but with warning constraint
1 parent c243b2f commit 2a98b21

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ decodeMyTypes json = do
5757
x <- decodeJson json
5858
arr <- x .: "myTypes"
5959
for arr decodeJson
60+
61+
-- create a `EncodeJson` instance
62+
instance encodeJsonMyType :: EncodeJson MyType where
63+
encodeJson (MyType x) =
64+
"foo" := x.foo ~>
65+
"bar" :=? x.bar ~>? -- optional field
66+
"baz" := x.baz ~>
67+
jsonEmptyObject
6068
```
6169

6270
## Contributing

src/Data/Argonaut/Decode.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ module Data.Argonaut.Decode
44
) where
55

66
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
7-
import Data.Argonaut.Decode.Combinators (parseField, (.:), parseFieldOptional, (.:!), parseFieldOptional', (.:?), defaultField, (.!=))
7+
import Data.Argonaut.Decode.Combinators ( getField, getFieldDeprecated, getFieldOptional, getFieldOptionalDeprecated, getFieldOptional', defaultField, defaultFieldDeprecated, (.:), (.?), (.:!), (.:?), (.??), (.!=), (.?=))

src/Data/Argonaut/Decode/Combinators.purs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
module Data.Argonaut.Decode.Combinators
2-
( parseField
3-
, parseFieldOptional
4-
, parseFieldOptional'
2+
( getField
3+
, getFieldDeprecated
4+
, getFieldOptional
5+
, getFieldOptionalDeprecated
6+
, getFieldOptional'
57
, defaultField
8+
, defaultFieldDeprecated
69
, (.:)
10+
, (.?)
711
, (.:!)
812
, (.:?)
13+
, (.??)
914
, (.!=)
15+
, (.?=)
1016
) where
1117

1218
import Prelude
@@ -17,29 +23,40 @@ import Data.Bifunctor (lmap)
1723
import Data.Either (Either(..))
1824
import Data.Maybe (Maybe(..), fromMaybe, maybe)
1925
import Foreign.Object as FO
26+
import Prim.TypeError (class Warn, Text)
2027

2128
-- | Attempt to get the value for a given key on an `Object Json`.
2229
-- |
2330
-- | 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 =
2734
maybe
2835
(Left $ "Expected field " <> show s)
2936
(elaborateFailure s <<< decodeJson)
3037
(FO.lookup s o)
3138

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 .?
3350

3451
-- | Attempt to get the value for a given key on an `Object Json`.
3552
-- |
3653
-- | The result will be `Right Nothing` if the key and value are not present,
3754
-- | or if the key is present and the value is `null`.
3855
-- |
3956
-- | 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 =
4360
maybe
4461
(pure Nothing)
4562
decode
@@ -50,7 +67,7 @@ parseFieldOptional' o s =
5067
then pure Nothing
5168
else Just <$> decodeJson json
5269

53-
infix 7 parseFieldOptional' as .:?
70+
infix 7 getFieldOptional' as .:?
5471

5572
-- | Attempt to get the value for a given key on an `Object Json`.
5673
-- |
@@ -59,17 +76,27 @@ infix 7 parseFieldOptional' as .:?
5976
-- |
6077
-- | This function will treat `null` as a value and attempt to decode it into your desired type.
6178
-- | 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 =
6582
maybe
6683
(pure Nothing)
6784
decode
6885
(FO.lookup s o)
6986
where
7087
decode json = Just <$> (elaborateFailure s <<< decodeJson) json
7188

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 .??
73100

74101
-- | Helper for use in combination with `.:?` to provide default values for optional
75102
-- | `Object Json` fields.
@@ -95,6 +122,13 @@ defaultField parser default = fromMaybe default <$> parser
95122

96123
infix 6 defaultField as .!=
97124

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+
98132
elaborateFailure :: a. String -> Either String a -> Either String a
99133
elaborateFailure s e =
100134
lmap msg e

0 commit comments

Comments
 (0)