Skip to content

Commit 9b00fcc

Browse files
Standardize style in the library and remove deprecated functions (#82)
1 parent d8f3a9c commit 9b00fcc

File tree

8 files changed

+269
-190
lines changed

8 files changed

+269
-190
lines changed

src/Data/Argonaut/Decode.purs

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ module Data.Argonaut.Decode
66
) where
77

88
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
9-
import Data.Argonaut.Decode.Combinators (getField, getFieldDeprecated, getFieldOptional, getFieldOptionalDeprecated, getFieldOptional', defaultField, defaultFieldDeprecated, (.:), (.?), (.:!), (.:?), (.??), (.!=), (.?=))
9+
import Data.Argonaut.Decode.Combinators
10+
( getField
11+
, getFieldOptional
12+
, getFieldOptional'
13+
, defaultField
14+
, (.:)
15+
, (.:!)
16+
, (.:?)
17+
, (.!=)
18+
)
1019
import Data.Argonaut.Decode.Error (JsonDecodeError(..), printJsonDecodeError)
11-
import Data.Argonaut.Decode.Parser (parseJson)
20+
import Data.Argonaut.Decode.Parser (parseJson)

src/Data/Argonaut/Decode/Class.purs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module Data.Argonaut.Decode.Class where
22

3-
import Prelude (class Ord, Unit, Void, bind, ($), (<<<))
3+
import Data.Argonaut.Decode.Decoders
44

55
import Data.Argonaut.Core (Json, toObject)
66
import Data.Argonaut.Decode.Error (JsonDecodeError(..))
77
import Data.Array.NonEmpty (NonEmptyArray)
8-
import Data.Either (Either(..))
98
import Data.Bifunctor (lmap)
9+
import Data.Either (Either(..))
1010
import Data.Identity (Identity)
1111
import Data.List (List)
1212
import Data.List.NonEmpty (NonEmptyList)
@@ -18,11 +18,11 @@ import Data.String (CodePoint)
1818
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
1919
import Data.Tuple (Tuple)
2020
import Foreign.Object as FO
21+
import Prelude (class Ord, Unit, Void, bind, ($), (<<<))
2122
import Prim.Row as Row
2223
import Prim.RowList as RL
2324
import Record as Record
2425
import Type.Data.RowList (RLProxy(..))
25-
import Data.Argonaut.Decode.Decoders
2626

2727
class DecodeJson a where
2828
decodeJson :: Json -> Either JsonDecodeError a
@@ -98,7 +98,7 @@ instance decodeRecord
9898
decodeJson json =
9999
case toObject json of
100100
Just object -> gDecodeJson object (RLProxy :: RLProxy list)
101-
Nothing -> Left $ TypeMismatch "Object"
101+
Nothing -> Left $ TypeMismatch "Object"
102102

103103
class GDecodeJson (row :: # Type) (list :: RL.RowList) | list -> row where
104104
gDecodeJson :: FO.Object Json -> RLProxy list -> Either JsonDecodeError (Record row)
@@ -114,19 +114,16 @@ instance gDecodeJsonCons
114114
, Row.Lacks field rowTail
115115
)
116116
=> GDecodeJson row (RL.Cons field value tail) where
117-
gDecodeJson object _ =
118-
let
119-
sProxy :: SProxy field
120-
sProxy = SProxy
117+
gDecodeJson object _ = do
118+
let
119+
_field = SProxy :: SProxy field
120+
fieldName = reflectSymbol _field
121121

122-
fieldName = reflectSymbol sProxy
123-
in case FO.lookup fieldName object of
122+
case FO.lookup fieldName object of
124123
Just jsonVal -> do
125124
val <- lmap (AtKey fieldName) <<< decodeJson $ jsonVal
126-
127125
rest <- gDecodeJson object (RLProxy :: RLProxy tail)
128-
129-
Right $ Record.insert sProxy val rest
126+
Right $ Record.insert _field val rest
130127

131128
Nothing ->
132129
Left $ AtKey fieldName MissingValue
+2-36
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
module Data.Argonaut.Decode.Combinators
22
( getField
3-
, getFieldDeprecated
43
, getFieldOptional
5-
, getFieldOptionalDeprecated
64
, getFieldOptional'
75
, defaultField
8-
, defaultFieldDeprecated
96
, (.:)
10-
, (.?)
117
, (.:!)
128
, (.:?)
13-
, (.??)
149
, (.!=)
15-
, (.?=)
1610
) where
1711

18-
import Prelude ((<$>))
12+
import Prelude
1913

2014
import Data.Argonaut.Core (Json)
2115
import Data.Argonaut.Decode.Error (JsonDecodeError)
2216
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
2317
import Data.Either (Either)
2418
import Data.Maybe (Maybe, fromMaybe)
2519
import Foreign.Object as FO
26-
import Prim.TypeError (class Warn, Text)
2720
import Data.Argonaut.Decode.Decoders as Decoders
2821

2922
-- | Attempt to get the value for a given key on an `Object Json`.
@@ -35,16 +28,6 @@ getField = Decoders.getField decodeJson
3528

3629
infix 7 getField as .:
3730

38-
getFieldDeprecated
39-
:: forall a. Warn ( Text "`.?` is deprecated, use `.:` instead" )
40-
=> DecodeJson a
41-
=> FO.Object Json
42-
-> String
43-
-> Either JsonDecodeError a
44-
getFieldDeprecated = getField
45-
46-
infix 7 getFieldDeprecated as .?
47-
4831
-- | Attempt to get the value for a given key on an `Object Json`.
4932
-- |
5033
-- | The result will be `Right Nothing` if the key and value are not present,
@@ -70,21 +53,11 @@ getFieldOptional = Decoders.getFieldOptional decodeJson
7053

7154
infix 7 getFieldOptional as .:!
7255

73-
getFieldOptionalDeprecated
74-
:: forall a. Warn ( Text "`.??` is deprecated, use `.:!` or `.:?` instead" )
75-
=> DecodeJson a
76-
=> FO.Object Json
77-
-> String
78-
-> Either JsonDecodeError (Maybe a)
79-
getFieldOptionalDeprecated = Decoders.getFieldOptional decodeJson
80-
81-
infix 7 getFieldOptionalDeprecated as .??
82-
8356
-- | Helper for use in combination with `.:?` to provide default values for optional
8457
-- | `Object Json` fields.
8558
-- |
8659
-- | Example usage:
87-
-- | ```purescript
60+
-- | ```purs
8861
-- | newtype MyType = MyType
8962
-- | { foo :: String
9063
-- | , bar :: Maybe Int
@@ -103,10 +76,3 @@ defaultField :: forall a. Either JsonDecodeError (Maybe a) -> a -> Either JsonDe
10376
defaultField parser default = fromMaybe default <$> parser
10477

10578
infix 6 defaultField as .!=
106-
107-
defaultFieldDeprecated
108-
:: forall a. Warn ( Text "`.?=` is deprecated, use `.!=` instead" )
109-
=> Either JsonDecodeError (Maybe a) -> a -> Either JsonDecodeError a
110-
defaultFieldDeprecated = defaultField
111-
112-
infix 6 defaultFieldDeprecated as .?=

0 commit comments

Comments
 (0)