Given a key, it constructs a lens over a container.
Signed-off-by: Petr Pudlak <[email protected]>
---
src/Ganeti/JSON.hs | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/src/Ganeti/JSON.hs b/src/Ganeti/JSON.hs
index ee1cfbd..6240edf 100644
--- a/src/Ganeti/JSON.hs
+++ b/src/Ganeti/JSON.hs
@@ -49,6 +49,7 @@ module Ganeti.JSON
, optionalJSField
, optFieldsToObj
, lookupContainer
+ , alterContainerL
, readContainer
, mkUsedKeys
, allUsedKeys
@@ -328,6 +329,18 @@ lookupContainer :: (Monad m, Ord a)
=> m b -> a -> GenericContainer a b -> m b
lookupContainer dflt k = maybe dflt return . Map.lookup k . fromContainer
+-- | Updates a value inside a container.
+-- The signature of the function is crafted so that it can be directly
+-- used as a lens.
+alterContainerL :: (Functor f, Ord a)
+ => a
+ -> (Maybe b -> f (Maybe b))
+ -> GenericContainer a b
+ -> f (GenericContainer a b)
+alterContainerL key f (GenericContainer m) =
+ fmap (\v -> GenericContainer $ Map.update (const v) key m)
+ (f $ Map.lookup key m)
+
-- | Container loader.
readContainer :: (Monad m, HasStringRepr a, Ord a, J.JSON b) =>
J.JSObject J.JSValue -> m (GenericContainer a b)
--
2.0.0.526.g5318336