Commit 7b5f1645 authored by Petr Pudlak's avatar Petr Pudlak

Add a function for looking up a value in a container

If a key has no value, it returns a supplied monadic default value.
Signed-off-by: default avatarPetr Pudlak <pudlak@google.com>
Reviewed-by: default avatarKlaus Aehlig <aehlig@google.com>
parent 21591cc0
......@@ -48,6 +48,7 @@ module Ganeti.JSON
, toArray
, optionalJSField
, optFieldsToObj
, lookupContainer
, readContainer
, DictObject(..)
, ArrayObject(..)
......@@ -313,6 +314,14 @@ instance F.Traversable (GenericContainer a) where
-- | Type alias for string keys.
type Container = GenericContainer String
-- | Looks up a value in a container with a default value.
-- If a key has no value, a given monadic default is returned.
-- This allows simple error handling, as the default can be
-- 'mzero', 'failError' etc.
lookupContainer :: (Monad m, Ord a)
=> m b -> a -> GenericContainer a b -> m b
lookupContainer dflt k = maybe dflt return . Map.lookup k . fromContainer
-- | Container loader.
readContainer :: (Monad m, HasStringRepr a, Ord a, J.JSON b) =>
J.JSObject J.JSValue -> m (GenericContainer a b)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment