Skip to content

Commit

Permalink
Fix bug in (.:/) M.lookup should now be H.lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
basvandijk committed Nov 18, 2011
1 parent 79ad9f3 commit 461d898
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion Data/Aeson/Types/Class.hs
Expand Up @@ -716,7 +716,7 @@ obj .:? key = case H.lookup key obj of
-- default value to assign in that case. If the key and value
-- are mandatory, use '(.:)' instead.
(.:/) :: (FromJSON a) => Object -> (Text, a) -> Parser a
obj .:/ (key, val) = case M.lookup key obj of
obj .:/ (key, val) = case H.lookup key obj of
Nothing -> pure val
Just v -> parseJSON v
{-# INLINE (.:/) #-}
Expand Down

2 comments on commit 461d898

@hvr
Copy link
Member

@hvr hvr commented on 461d898 Nov 26, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jfyi, I've been using the following combinator-like approach throughout my code for the last months. I tried first with a .:/-like approach, but it didn't feel right to me.

-- |Helper for use in combination with '.:?' operator
(.!=) :: Parser (Maybe a) -> a -> Parser a
(.!=) p dfl = fromMaybe dfl <$> p
{-# INLINE (.!=) #-}

This allows code to be written in a ternary-operator style such as:

do
   v1 <- o .:? "opt_field_with_dfl" .!= "default_val"
   v2 <- o .:  "mandatory_field"
   v3 <- o .:? "opt_field2"

   -- whereas the `.:/` operator looks weird in comparision:
   v1' <- o .:/ ("opt_field_with_dfl", "default_val")

@zhensydow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thinks it's a clever solution using it as a Functor.

Please sign in to comment.