Revision a810ad21

b/htools/Ganeti/HTools/QC.hs
247 247
-- Split and join should always be idempotent
248 248
prop_Utils_commaSplitJoin s = Utils.commaJoin (Utils.sepSplit ',' s) == s
249 249

  
250
-- | fromObjWithDefault, we test using the Maybe monad and an integer
251
-- value
252
prop_Utils_fromObjWithDefault def_value random_key =
253
    -- a missing key will be returned with the default
254
    Utils.fromObjWithDefault [] random_key def_value == Just def_value &&
255
    -- a found key will be returned as is, not with default
256
    Utils.fromObjWithDefault [(random_key, J.showJSON def_value)]
257
         random_key (def_value+1) == Just def_value
258
        where _types = (def_value :: Integer)
259

  
250 260
testUtils =
251 261
  [ run prop_Utils_commaJoinSplit
252 262
  , run prop_Utils_commaSplitJoin
263
  , run prop_Utils_fromObjWithDefault
253 264
  ]
254 265

  
255 266
-- | Make sure add is idempotent
b/htools/Ganeti/HTools/Utils.hs
32 32
    , readEitherString
33 33
    , loadJSArray
34 34
    , fromObj
35
    , fromObjWithDefault
35 36
    , maybeFromObj
36 37
    , tryFromObj
37 38
    , fromJVal
......
46 47

  
47 48
import Control.Monad (liftM)
48 49
import Data.List
50
import Data.Maybe (fromMaybe)
49 51
import qualified Text.JSON as J
50 52
import Text.Printf (printf)
51 53

  
......
142 144
      Nothing -> return Nothing
143 145
      Just val -> liftM Just (fromKeyValue k val)
144 146

  
147
-- | Reads the value of a key in a JSON object with a default if missing.
148
fromObjWithDefault :: (J.JSON a, Monad m) =>
149
                      [(String, J.JSValue)] -> String -> a -> m a
150
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k
151

  
145 152
-- | Reads a JValue, that originated from an object key
146 153
fromKeyValue :: (J.JSON a, Monad m)
147 154
              => String     -- ^ The key name

Also available in: Unified diff