Skip to content

Commit 5ceb24f

Browse files
committed
Add an operator for supplying a default value to getDefaultField
This PR adds an operator for supplying a default value to `getDefaultField` (`.??`). This is similar to Aeson's [`.!=`](https://www.stackage.org/haddock/lts-8.21/aeson-1.0.2.1/Data-Aeson.html#v:.-33--61-) operator. The fixity needs to be less than the `.??` operator so that they compose cleanly.
1 parent ca9185d commit 5ceb24f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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 (getField, (.?), getFieldOptional, (.??))
7+
import Data.Argonaut.Decode.Combinators (getField, (.?), getFieldOptional, (.??), defaultField, (.?=))

src/Data/Argonaut/Decode/Combinators.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prelude
55
import Data.Argonaut.Core (JObject)
66
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
77
import Data.Either (Either(..))
8-
import Data.Maybe (Maybe(..), maybe)
8+
import Data.Maybe (Maybe(..), fromMaybe, maybe)
99
import Data.StrMap as SM
1010

1111
getField :: forall a. DecodeJson a => JObject -> String -> Either String a
@@ -27,3 +27,8 @@ getFieldOptional o s =
2727
decode json = Just <$> decodeJson json
2828

2929
infix 7 getFieldOptional as .??
30+
31+
defaultField :: forall a. Either String (Maybe a) -> a -> Either String a
32+
defaultField parser default = fromMaybe default <$> parser
33+
34+
infix 6 defaultField as .?=

0 commit comments

Comments
 (0)