Skip to content

Created additional getFieldOptional #36

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

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 16 additions & 1 deletion src/Data/Argonaut/Decode/Combinators.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ getField o s =

infix 7 getField as .?

-- | Will return Nothing if the field isn't present
getFieldOptional :: forall a. DecodeJson a => JObject -> String -> Either String (Maybe a)
getFieldOptional o s =
maybe
Expand All @@ -26,7 +27,21 @@ getFieldOptional o s =
where
decode json = Just <$> decodeJson json

infix 7 getFieldOptional as .??
infix 7 getFieldOptional as .?!

-- | Will return Nothing if the field isn't present or the value is null
getFieldOptional' :: forall a. DecodeJson a => JObject -> String -> Either String (Maybe a)
getFieldOptional' o s =
maybe
(pure Nothing)
decodeIt
(SM.lookup s o)
where decodeIt j =
if isNull j
then pure Nothing
else Just <$> decodeJson j

infix 7 getFieldOptional' as .??

defaultField :: forall a. Either String (Maybe a) -> a -> Either String a
defaultField parser default = fromMaybe default <$> parser
Expand Down