Skip to content

Standardize style in the library and remove deprecated functions #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Data/Argonaut/Decode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ module Data.Argonaut.Decode
) where

import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
import Data.Argonaut.Decode.Combinators (getField, getFieldDeprecated, getFieldOptional, getFieldOptionalDeprecated, getFieldOptional', defaultField, defaultFieldDeprecated, (.:), (.?), (.:!), (.:?), (.??), (.!=), (.?=))
import Data.Argonaut.Decode.Combinators
( getField
, getFieldOptional
, getFieldOptional'
, defaultField
, (.:)
, (.:!)
, (.:?)
, (.!=)
)
import Data.Argonaut.Decode.Error (JsonDecodeError(..), printJsonDecodeError)
import Data.Argonaut.Decode.Parser (parseJson)
import Data.Argonaut.Decode.Parser (parseJson)
23 changes: 10 additions & 13 deletions src/Data/Argonaut/Decode/Class.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Data.Argonaut.Decode.Class where

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

import Data.Argonaut.Core (Json, toObject)
import Data.Argonaut.Decode.Error (JsonDecodeError(..))
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Either (Either(..))
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Identity (Identity)
import Data.List (List)
import Data.List.NonEmpty (NonEmptyList)
Expand All @@ -18,11 +18,11 @@ import Data.String (CodePoint)
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Tuple (Tuple)
import Foreign.Object as FO
import Prelude (class Ord, Unit, Void, bind, ($), (<<<))
import Prim.Row as Row
import Prim.RowList as RL
import Record as Record
import Type.Data.RowList (RLProxy(..))
import Data.Argonaut.Decode.Decoders

class DecodeJson a where
decodeJson :: Json -> Either JsonDecodeError a
Expand Down Expand Up @@ -98,7 +98,7 @@ instance decodeRecord
decodeJson json =
case toObject json of
Just object -> gDecodeJson object (RLProxy :: RLProxy list)
Nothing -> Left $ TypeMismatch "Object"
Nothing -> Left $ TypeMismatch "Object"

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

fieldName = reflectSymbol sProxy
in case FO.lookup fieldName object of
case FO.lookup fieldName object of
Just jsonVal -> do
val <- lmap (AtKey fieldName) <<< decodeJson $ jsonVal

rest <- gDecodeJson object (RLProxy :: RLProxy tail)

Right $ Record.insert sProxy val rest
Right $ Record.insert _field val rest

Nothing ->
Left $ AtKey fieldName MissingValue
38 changes: 2 additions & 36 deletions src/Data/Argonaut/Decode/Combinators.purs
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
module Data.Argonaut.Decode.Combinators
( getField
, getFieldDeprecated
, getFieldOptional
, getFieldOptionalDeprecated
, getFieldOptional'
, defaultField
, defaultFieldDeprecated
, (.:)
, (.?)
, (.:!)
, (.:?)
, (.??)
, (.!=)
, (.?=)
) where

import Prelude ((<$>))
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 Prim.TypeError (class Warn, Text)
import Data.Argonaut.Decode.Decoders as Decoders

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

infix 7 getField as .:

getFieldDeprecated
:: forall a. Warn ( Text "`.?` is deprecated, use `.:` instead" )
=> DecodeJson a
=> FO.Object Json
-> String
-> Either JsonDecodeError a
getFieldDeprecated = getField

infix 7 getFieldDeprecated 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,
Expand All @@ -70,21 +53,11 @@ getFieldOptional = Decoders.getFieldOptional decodeJson

infix 7 getFieldOptional as .:!

getFieldOptionalDeprecated
:: forall a. Warn ( Text "`.??` is deprecated, use `.:!` or `.:?` instead" )
=> DecodeJson a
=> FO.Object Json
-> String
-> Either JsonDecodeError (Maybe a)
getFieldOptionalDeprecated = Decoders.getFieldOptional decodeJson

infix 7 getFieldOptionalDeprecated as .??

-- | Helper for use in combination with `.:?` to provide default values for optional
-- | `Object Json` fields.
-- |
-- | Example usage:
-- | ```purescript
-- | ```purs
-- | newtype MyType = MyType
-- | { foo :: String
-- | , bar :: Maybe Int
Expand All @@ -103,10 +76,3 @@ defaultField :: forall a. Either JsonDecodeError (Maybe a) -> a -> Either JsonDe
defaultField parser default = fromMaybe default <$> parser

infix 6 defaultField as .!=

defaultFieldDeprecated
:: forall a. Warn ( Text "`.?=` is deprecated, use `.!=` instead" )
=> Either JsonDecodeError (Maybe a) -> a -> Either JsonDecodeError a
defaultFieldDeprecated = defaultField

infix 6 defaultFieldDeprecated as .?=
Loading