Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added back the lookup functions

  • Loading branch information...
commit 068ca76290ecf0ccf887b7b644b8c93616e47c47 1 parent 4cfc360
@snoyberg authored
Showing with 35 additions and 2 deletions.
  1. +34 −1 Data/Object.hs
  2. +1 −1  data-object.cabal
View
35 Data/Object.hs
@@ -52,11 +52,16 @@ module Data.Object
, fromScalar
, fromSequence
, fromMapping
+ -- * Lookups
+ , lookupObject
+ , lookupScalar
+ , lookupSequence
+ , lookupMapping
) where
import Control.Arrow
import Control.Applicative
-import Control.Monad (ap)
+import Control.Monad (ap, (<=<))
import Prelude hiding (mapM, sequence)
@@ -181,6 +186,7 @@ data ObjectExtractError =
ExpectedScalar
| ExpectedSequence
| ExpectedMapping
+ | MissingKey String
deriving (Typeable, Show)
instance Exception ObjectExtractError
@@ -205,3 +211,30 @@ fromMapping :: Failure ObjectExtractError m
-> m [(k, Object k v)]
fromMapping (Mapping m) = return m
fromMapping _ = failure ExpectedMapping
+
+lookupObject :: (Show k, Eq k, Failure ObjectExtractError m)
+ => k
+ -> [(k, Object k v)]
+ -> m (Object k v)
+lookupObject k pairs =
+ case lookup k pairs of
+ Nothing -> failure $ MissingKey $ show k
+ Just v -> return v
+
+lookupScalar :: (Show k, Eq k, Failure ObjectExtractError m)
+ => k
+ -> [(k, Object k v)]
+ -> m v
+lookupScalar k = fromScalar <=< lookupObject k
+
+lookupSequence :: (Show k, Eq k, Failure ObjectExtractError m)
+ => k
+ -> [(k, Object k v)]
+ -> m [Object k v]
+lookupSequence k = fromSequence <=< lookupObject k
+
+lookupMapping :: (Show k, Eq k, Failure ObjectExtractError m)
+ => k
+ -> [(k, Object k v)]
+ -> m [(k, Object k v)]
+lookupMapping k = fromMapping <=< lookupObject k
View
2  data-object.cabal
@@ -1,5 +1,5 @@
name: data-object
-version: 0.3.0
+version: 0.3.1
license: BSD3
license-file: LICENSE
author: Michael Snoyman, Nicolas Pouillard
Please sign in to comment.
Something went wrong with that request. Please try again.