Skip to content
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

Add "nullIsNothing" #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Added nullIsDefault as well.
  • Loading branch information
PaulJohnson committed Aug 26, 2021
commit 054cfbd0c87163d24903a05d764519f0b4a5478c
19 changes: 19 additions & 0 deletions src/Data/Aeson/Lens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Data.Aeson.Lens
, _Integral
, nonNull
, nullIsNothing
, nullIsDefault
-- * Primitive
, Primitive(..)
, AsPrimitive(..)
Expand Down Expand Up @@ -304,6 +305,24 @@ nullIsNothing p = withPrism p $ \bk fw ->
in prism bk1 fw1


-- | Treat a 'Null' value as a default
--
-- >>> Number 123 ^? nullIsDefault 0 _Integer
-- Just 123
--
-- >>> Null ^? nullIsDefault 0 _Integer
-- Just 0
--
-- >>> "xyz" ^? nullIsDefault 0 _Integer
-- Nothing
nullIsDefault :: q -> Prism' Value q -> Prism' Value q
nullIsDefault q p = withPrism p $ \bk fw ->
let
fw1 Null = Right q
fw1 x = fw x
in prism bk fw1


------------------------------------------------------------------------------
-- Non-primitive traversals
------------------------------------------------------------------------------
Expand Down